当前位置:网站首页>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()
边栏推荐
- 再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
- Implement sending post request with form data parameter
- 13 医疗挂号系统_【 微信登录】
- MySQL實戰優化高手08 生產經驗:在數據庫的壓測過程中,如何360度無死角觀察機器性能?
- Implement context manager through with
- MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
- NLP routes and resources
- Some thoughts on the study of 51 single chip microcomputer
- 百度百科数据爬取及内容分类识别
- Typescript入门教程(B站黑马程序员)
猜你喜欢
Hugo blog graphical writing tool -- QT practice
Implement sending post request with form data parameter
在CANoe中通過Panel面板控制Test Module 運行(初級)
The replay block of canoe still needs to be combined with CAPL script to make it clear
再有人问你数据库缓存一致性的问题,直接把这篇文章发给他
jar运行报错no main manifest attribute
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
Introduction tutorial of typescript (dark horse programmer of station B)
C杂讲 浅拷贝 与 深拷贝
随机推荐
Tianmu MVC audit I
Control the operation of the test module through the panel in canoe (primary)
Tianmu MVC audit II
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
Typescript入门教程(B站黑马程序员)
CAPL script pair High level operation of INI configuration file
Sichuan cloud education and double teacher model
Technology | diverse substrate formats
解决在window中远程连接Linux下的MySQL
百度百科数据爬取及内容分类识别
Solve the problem of remote connection to MySQL under Linux in Windows
Set shell script execution error to exit automatically
text 文本数据增强方法 data argumentation
Super detailed steps to implement Wechat public number H5 Message push
Simple solution to phpjm encryption problem free phpjm decryption tool
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
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
Embedded development is much more difficult than MCU? Talk about SCM and embedded development and design experience