首页 > 其他分享 >pytorch 的 torchvision.datasets.ImageFolder 来自定义数据集

pytorch 的 torchvision.datasets.ImageFolder 来自定义数据集

时间:2023-06-05 14:11:52浏览次数:31  
标签:... datasets torchvision list self ImageFolder class

import torchvision

class ClassificationDataset(torchvision.datasets.ImageFolder):
	"""
	YOLOv5 Classification Dataset.
	Arguments
		root:  Dataset path
	"""

	def __init__(self, root):
		super().__init__(root=root) # 调用了 父类的 初始化函数,就拥有了以下的 self 属性

		classes = self.classes # list 每个类的文件名
		# ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
		class_to_idx = self.class_to_idx # 字典 每个类的文件名,类别标签(数字)
		# {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
		samples = self.samples # list 图像路径,标签(0,1,2...)
		# [('/data/huyuzhen/proje...in/0/1.png', 0), ('/data/huyuzhen/proje...0/1000.png', 0),...
		targets = self.targets # list 类别标签 数字:0,1,2...
		# [0, 0, 0, 0, 0, 0, 0...


path = '/data/huyuzhen/projects/datasets/mnist/train'
dataset = ClassificationDataset(root=path)

自定义一个图像分类 类,mnist 数据组织为 :

mnist
	├── test
	│   ├── 0
	│   ├── 1
	...
	├── train
	│   ├── 0
	│   ├── 1
	...

ImageFolder是DatasetFolder的子类,有以下属性:

 Attributes:
    classes (list): List of the class names sorted alphabetically.
    class_to_idx (dict): Dict with items (class_name, class_index).
    samples (list): List of (sample path, class_index) tuples
    targets (list): The class_index value for each image in the dataset
"""

使用 torchvision.datasets.ImageFolder 需要把数据集按如上组织。

标签:...,datasets,torchvision,list,self,ImageFolder,class
From: https://www.cnblogs.com/odesey/p/17457530.html

相关文章

  • 利用hf datasets库包构建自己的数据集
    !wgethttps://raw.githubusercontent.com/zhangbo2008/data_ner/main/aomanyupianjian-Oaaaa!pipinstalldatasets#=======加载自己数据集withopen('aaaa')asf:t=f.readlines()save1=[]save2=[]a=''b=''foriint:ifi=......
  • torch+torchvision+cuda10.1
    torch1.7.1#CUDA10.1pipinstalltorch==1.7.1+cu101torchvision==0.8.2+cu101torchaudio==0.7.2-fhttps://download.pytorch.org/whl/torch_stable.html#CPUonlypipinstalltorch==1.7.1+cputorchvision==0.8.2+cputorchaudio==0.7.2-fhttps://download.pyt......
  • SelfDefinedDataset显示没有属性get_datasets
    get_datasets是一个PyTorchLightning框架中的方法,用于返回数据加载器中包含的训练、验证和测试数据集。如果你的自定义数据集类没有该方法,则会出现AttributeError:'YourDataset'objecthasnoattribute'get_datasets'错误。要解决这个问题,你需要在自定义数据集类中实现g......
  • 生信工具推荐之(2) datasets
    datasetsNCBI出品跨平台轻松批量从数据库中下载数据的命令行工具指南:工具处于快速更新迭代阶段,正逐步添加新功能,,参考网址:https://www.ncbi.nlm.nih.gov/datasets/docs/v1/how-tos/安装:curl-odatasets'https://ftp.ncbi.nlm.nih.gov/pub/datasets/command-line/v1/linux-am......
  • tensorflow.keras.datasets 中关于imdb.load_data的使用说明
    python深度学习在加载数据时(num_words=10000)所代表的意义首先写一段深度学习加载数据集的代码:fromkeras.datasetsimportreuters(train_data,train_labels),(test_dat......
  • huggingface datasets数据集本地化
    有时候服务器访问不了外网,可以现在可以访问外网的机器上先把数据集给下好,然后传到对应服务器进行加载。 1.首先下载并存储数据:importdatasetsdataset=datasets.l......
  • Yolov5环境报错解决:No labels found in 与 Could not run 'torchvision::nms' with ar
    问题记录yolov5环境1Nolabelsfoundin(Done)报错内容F:\WorkSpace\GitSpace\yolov5>pythontrain-self.pytrain-self:weights=weights/yolov5s.pt,cfg=models/y......
  • 11、torchvision中的数据集的使用
    transforms要用来处理数据集中的每一张图片,本节主要讲解怎么把数据集和transforms结合在一起;在实际应用中该如何去下载数据集,如何去组织,查看以及使用去pytorch官网,找到to......
  • 基于 Hugging Face Datasets 和 Transformers 的图像相似性搜索
    基于HuggingFaceDatasets和Transformers的图像相似性搜索通过本文,你将学习使用......
  • OutOfMemoryExceptions while remoting very large datasets
    TessFerrandezPrincipalSoftwareEngineeratMicrosoft.Solvingtheproblemsoftheworldwithadebugger,asprinkleofMLandabagofchipsTwitte......