当前位置:网站首页>pytorch的Dataset的使用
pytorch的Dataset的使用
2022-07-06 09:11:00 【一曲无痕奈何】
from torch.utils.data import Dataset
from PIL import Image
import os
class MyData():
def __init__(self, root_dir, label_dir):
#使用self相当于创建一个全局变量给后面的也可以使用
self.root_dir = root_dir
self.label_dir = label_dir
self.path = os.path.join(self.root_dir, self.label_dir)
self.img_path = os.listdir(self.path)
#获取其中每一个图片
def __getitem__(self, index):
#通过index获取图片地址
img_name = self.img_path[index] #每个图片的路径
img_item_path = os.path.join(self.root_dir, self.label_dir, img_name)
print(img_item_path)
#读取图片
img = Image.open(img_item_path)
img.show
label = self.label_dir
return img, label
def __len__(self): #数据集有多长
return len(self.img_path)
root_dir = "D:\\数据集\\hymenoptera_data\\train"
ant_lable_dair = "ants"
bees_label_dir = "bees"
ant_dataset = MyData(root_dir, ant_lable_dair)
bees_dataset = MyData(root_dir,bees_label_dir)
ant_dataset.__getitem__(0)
for i in range(1,100):
bees_dataset.__getitem__(i)
img, label = ant_dataset[0]
img.show()
边栏推荐
- C miscellaneous lecture continued
- Simple solution to phpjm encryption problem free phpjm decryption tool
- Introduction tutorial of typescript (dark horse programmer of station B)
- Chrome浏览器端跨域不能访问问题处理办法
- UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd0 in position 0成功解决
- 西南大学:胡航-关于学习行为和学习效果分析
- AI的路线和资源
- Jar runs with error no main manifest attribute
- Hugo blog graphical writing tool -- QT practice
- MySQL的存储引擎
猜你喜欢
Cmooc Internet + education
If someone asks you about the consistency of database cache, send this article directly to him
MySQL storage engine
Some thoughts on the study of 51 single chip microcomputer
The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
在CANoe中通過Panel面板控制Test Module 運行(初級)
Control the operation of the test module through the panel in canoe (primary)
Download and installation of QT Creator
In fact, the implementation of current limiting is not complicated
Introduction tutorial of typescript (dark horse programmer of station B)
随机推荐
Vscode common instructions
宝塔的安装和flask项目部署
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
[Julia] exit notes - Serial
安装OpenCV时遇到的几种错误
Flash operation and maintenance script (running for a long time)
实现微信公众号H5消息推送的超级详细步骤
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
Pointer learning
13 医疗挂号系统_【 微信登录】
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
A necessary soft skill for Software Test Engineers: structured thinking
MySQL实战优化高手05 生产经验:真实生产环境下的数据库机器配置如何规划?
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
Download and installation of QT Creator
Export virtual machines from esxi 6.7 using OVF tool
Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
C杂讲 文件 续讲
17 医疗挂号系统_【微信支付】