当前位置:网站首页>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()
边栏推荐
- AI的路线和资源
- Implement context manager through with
- [Julia] exit notes - Serial
- Download and installation of QT Creator
- Automation sequences of canoe simulation functions
- MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
- Installation of pagoda and deployment of flask project
- C杂讲 文件 初讲
- Windchill配置远程Oracle数据库连接
- Security design verification of API interface: ticket, signature, timestamp
猜你喜欢
Implement context manager through with
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd0 in position 0成功解决
宝塔的安装和flask项目部署
Several silly built-in functions about relative path / absolute path operation in CAPL script
寶塔的安裝和flask項目部署
Write your own CPU Chapter 10 - learning notes
Installation de la pagode et déploiement du projet flask
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
Carolyn Rosé博士的社交互通演讲记录
随机推荐
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
UEditor国际化配置,支持中英文切换
MySQL实战优化高手12 Buffer Pool这个内存数据结构到底长个什么样子?
[NLP] bert4vec: a sentence vector generation tool based on pre training
NLP路线和资源
Configure system environment variables through bat script
如何让shell脚本变成可执行文件
Random notes
C杂讲 双向循环链表
Set shell script execution error to exit automatically
Implement context manager through with
Simple solution to phpjm encryption problem free phpjm decryption tool
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
安装OpenCV时遇到的几种错误
Solve the problem of remote connection to MySQL under Linux in Windows
MySQL storage engine
Docker MySQL solves time zone problems
实现以form-data参数发送post请求
C杂讲 浅拷贝 与 深拷贝
Delayed note learning