当前位置:网站首页>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()
边栏推荐
- Installation de la pagode et déploiement du projet flask
- 软件测试工程师必备之软技能:结构化思维
- Vscode common instructions
- MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
- NLP routes and resources
- oracle sys_ Context() function
- The programming ranking list came out in February. Is the result as you expected?
- 15 医疗挂号系统_【预约挂号】
- MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
- Cmooc Internet + education
猜你喜欢
![[flask] crud addition and query operation of data](/img/fc/7a12ede31befed5f42b32b94ae5ecf.jpg)
[flask] crud addition and query operation of data

In fact, the implementation of current limiting is not complicated

Download and installation of QT Creator

C杂讲 浅拷贝 与 深拷贝

UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd0 in position 0成功解决

Notes of Dr. Carolyn ROS é's social networking speech
![[Julia] exit notes - Serial](/img/d0/87f0d57ff910a666fbb67c0ae8a838.jpg)
[Julia] exit notes - Serial
![15 medical registration system_ [appointment registration]](/img/c1/27c7a5aae82783535e5467583bb176.png)
15 medical registration system_ [appointment registration]

Typescript入门教程(B站黑马程序员)

What is the current situation of the game industry in the Internet world?
随机推荐
C杂讲 浅拷贝 与 深拷贝
Carolyn Rosé博士的社交互通演讲记录
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
MySQL learning diary (II)
Routes and resources of AI
Constants and pointers
好博客好资料记录链接
MySQL的存储引擎
MySQL real battle optimization expert 08 production experience: how to observe the machine performance 360 degrees without dead angle in the process of database pressure test?
14 medical registration system_ [Alibaba cloud OSS, user authentication and patient]
MySQL combat optimization expert 12 what does the memory data structure buffer pool look like?
Competition vscode Configuration Guide
Download address of canoe, download and activation of can demo 16, and appendix of all canoe software versions
[NLP] bert4vec: a sentence vector generation tool based on pre training
UEditor国际化配置,支持中英文切换
MySQL ERROR 1040: Too many connections
Automation sequences of canoe simulation functions
Several silly built-in functions about relative path / absolute path operation in CAPL script
Write your own CPU Chapter 10 - learning notes