当前位置:网站首页>【Pytorch】Dataset_ DataLoader
【Pytorch】Dataset_ DataLoader
2022-07-24 07:33:00 【rejudge】
- Sample code
import numpy as np
from PIL import Image
from torch.utils.data import Dataset, DataLoader
import os
class TextDataset(Dataset):
def __init__(self, data_path):
data = np.loadtxt(data_path, delimiter=',')
self._x = data[:, :2]
self._y = data[:, -1]
self.len = len(data)
def __getitem__(self, idx):
return self._x[idx], self._y[idx]
def __len__(self):
return self.len
class ImgDateset(Dataset):
def __init__(self, root_dir):
self.root_dir = root_dir
self.img_path_list = os.listdir(self.root_dir)
def __getitem__(self, idx):
img_path = os.path.join(self.root_dir, self.img_path_list[idx])
img = Image.open(img_path)
# img.show()
return img_path
def __len__(self):
return len(self.img_path_list)
if __name__ == '__main__':
i = 1
if i:
text_dataset = TextDataset('./example_data.txt')
TextDataLoader = DataLoader(text_dataset, batch_size=3, shuffle=True, drop_last=False)
iteration = iter(TextDataLoader)
while True:
try:
xs, ys = next(iteration)
print('Value :', xs)
print('Table:', ys)
except StopIteration:
break
else:
img_dataset = ImgDateset('./img_data')
img = img_dataset.__getitem__(2)
print(img_dataset)
ImgDataLoader = DataLoader(dataset=img_dataset, batch_size=3, shuffle=False, drop_last=False)
print(len(ImgDataLoader))
for img_path in ImgDataLoader:
print(img_path)
- example_data.txt
1.0,12.0,12.0
2.0,23.4,23.5
3.0,34.5,34.6
4.0,45.6,45.7
5.0,56.7,56.8
6.0,67.8,67.9
7.0,78.9,78.0
8.0,89.0,89.1
- img_data

边栏推荐
- MySQL statement
- China trichlorosilane Market Forecast and Strategic Research Report (2022 Edition)
- Introduction to C language II. Functions
- Problems encountered in inserting large quantities of data into the database in the project
- QoS quality of service three DiffServ Model message marking and PHB
- [leetcode simple] 20. Valid brackets stack
- stdafx. H introduction and function
- 项目上线就炸,这谁受得了
- Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
- 爬虫学习-概述
猜你喜欢

AMD64(x86_64)架构abi文档:上

Mitre att & CK ultra detailed learning notes-02 (a large number of cases)

Introduction to C language I. branch and loop statements

win10声音图标有个没有声音

Chapter007 FPGA learning IIC bus EEPROM reading

DOM operation of JS -- style operation

Jenkins detailed deployment

从CIA看常见网络攻击(爆破,PE,流量攻击)

Compilation and debugging (GCC, g++, GDB)

Source code analysis of Nacos configuration center
随机推荐
Jenkins detailed deployment
Source code analysis of Nacos configuration center
[introduction to C language] zzulioj 1011-1015
给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
php 转义字符串
MySQL statement
Vulnhub DC1
Bookkeeping app: xiaoha bookkeeping 2 - production of registration page
Advanced part of Nacos
全国职业院校技能大赛网络安全B模块 缓冲区溢出漏洞
觉维设计响应式布局
oracle中有A,B连个表,这两个表需要第三个表C关联,那怎么将A表中的字段MJ1更新为B表中MJ2的值
numpy.cumsum
mysql查询当前节点的所有父级
Win10 sound icon has no sound
Decompress the anchor and enjoy 4000w+ playback, adding a new wind to the Kwai food track?
Vulnhub DC1
Using bidirectional linked list to realize stack (c)
使用堡垒机(跳板机)登录服务器
Sword finger offer special assault edition day 8