当前位置:网站首页>【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

边栏推荐
- numpy.concatenate
- Development system selection route
- MITRE ATT&CK超详细学习笔记-02(大量案例)
- cookie_ session
- Injectfix principle learning (to realize the heat of repair addition)
- django.db.utils. OperationalError: (2002, “Can‘t connect to local MySQL server through socket ‘/var/r
- 全国职业院校技能大赛网络安全B模块 缓冲区溢出漏洞
- 拉普拉斯(Laplace)分布
- Introduction to C language II. Functions
- Sword finger offer special assault edition day 8
猜你喜欢

System integration project management engineer (soft test intermediate) key knowledge, recitation version
![[line test] Figure finding regular questions](/img/61/d1c2cd399cf0d808e4fa25cd5fe681.png)
[line test] Figure finding regular questions

Requests crawler implements a simple web page collector

Oauth2==sso three protocols. Oauth2 four modes

XSS漏洞学习

JS_ Realize the separation of multiple lines of text into an array according to the newline

QoS quality of service 4 traffic regulation of QoS boundary behavior

Filter filter
![[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management](/img/a9/7cdab9264902b1e2947a43463f6b32.png)
[hiflow] Tencent cloud hiflow scene connector realizes intelligent campus information management

win10声音图标有个没有声音
随机推荐
Vulnhub DC1
Li Kou, niuke.com - > linked list related topics (Article 1) (C language)
Unity中使用深度和法线纹理
OpenCascade笔记:gp包
Development system selection route
[word] how to generate the index directory on the left
项目上线就炸,这谁受得了
二维平面多段线Y轴最短距离
24.全局事件总线
[PTA] group programming ladder competition - Summary of exercises L3 (incomplete)
FPGA realizes reading and writing of axi4 bus
Network security B module windows operating system penetration test of national vocational college skills competition
UNI-APP_小程序或h5页面背景音乐的播放与暂停
Fopen, fwrite, fseek, fTell, FREAD use demo
Oauth2==SSO三种协议。Oauth2四种模式
游戏三子棋
Wild pointer, null pointer, invalid pointer
FlinkSQL-UDF自定义数据源
[FreeRTOS] 11 software timer
Seminar 2022.07.22 -- Comparative learning