当前位置:网站首页>【Pytorch】Dataset_DataLoader
【Pytorch】Dataset_DataLoader
2022-07-24 05:15:00 【rejudge】
- 示例代码
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

边栏推荐
- Mysq Database Constraints
- Token of space renewable energy
- Context encoders: feature learning by painting paper notes
- 网NN计算能主机系统资e提供的NTCP
- BeanShell built-in variable CTX
- finally和return的执行顺序
- PXE efficient batch network installation
- SSH service
- Pointer learning diary (V) classic abstract data types and standard function libraries
- 手写orm框架
猜你喜欢

Ia notes 2

jdbc封装一个父类减少代码重复

1、基于增量式生成遮挡与对抗抑制的行人再识别
![Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]](/img/c1/320e0349e2edda0eb420ed018aa831.png)
Codeforce:d2. remove the substring (hard version) [greedy string + subsequence]

SSH service

一文带你深入浅出C字符串函数和内存函数

FRP intranet penetration service usage

MGRE and OSPF comprehensive experiment

postgresql:在Docker中运行PostgreSQL + pgAdmin 4

Wang Qing, director of cloud infrastructure software research and development of Intel (China): Intel's technology development and prospects in cloud native
随机推荐
泛型和注解
Hcip-- review the homework for the next day
[postgraduate entrance examination vocabulary training camp] day 10 - capital, expand, force, adapt, depand
JDBC MySQL basic operations
MS simulated written test
web开发
Learning some contents of vector and iterator
Un7.23: how to install MySQL on linix?
好的程序员与不好的程序员
酒店IPTV数字电视系统解决方案
Crazy God redis notes 09
NLP learning roadmap (mind map) is very comprehensive and clear!
The difference between compiled language and interpreted language
FRP intranet penetration service usage
)To feed back to the application layer or into multiple format documents:
Theoretical basis of machine learning
[advanced mathematics] the difference between differentiable and differentiable functions
Data annotation learning summary
HCIA NAT experiment
股票价格走势的行业关联性