ImageFolder
是一个通用的数据加载器,它要求我们以下面这种格式来组织数据集的训练、验证或者测试图片。
root/dog/xxx.png
root/dog/xxy.png
root/dog/xxz.png
root/cat/123.png
root/cat/nsdf3.png
root/cat/asd932_.png
对于上面的root,假设data文件夹在.py文件的同级目录中,那么root一般都是如下这种形式:./data/train
和./data/valid
dataset=torchvision.datasets.ImageFolder(
root, transform=None,
target_transform=None,
loader=<function default_loader>,
is_valid_file=None)
参数详解:
返回的dataset都有以下三种属性:
self.classes
:用一个 list 保存类别名称self.class_to_idx
:类别对应的索引,与不做任何转换返回的 target 对应self.imgs
:保存(img-path, class) tuple的 listfrom torchvision.datasets import ImageFolder
from torchvision import transforms
#加上transforms
normalize=transforms.Normalize(mean=[.5,.5,.5],std=[.5,.5,.5])
transform=transforms.Compose([
transforms.RandomCrop(180),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(), #将图片转换为Tensor,归一化至[0,1]
normalize
])
dataset=ImageFolder('./data/train',transform=transform)
我们得到的dataset
,它的结构就是[(img_data,class_id),(img_data,class_id),…],下面我们打印第一个元素:
print(dataset[0])
'''
输出:
(tensor([[[-0.5137, -0.4667, -0.4902, ..., -0.0980, -0.0980, -0.0902],
[-0.5922, -0.5529, -0.5059, ..., -0.0902, -0.0980, -0.0667],
[-0.5373, -0.5294, -0.4824, ..., -0.0588, -0.0824, -0.0196],
...,
[-0.3098, -0.3882, -0.3725, ..., -0.4353, -0.4510, -0.4196],
[-0.2863, -0.3647, -0.3725, ..., -0.4431, -0.4118, -0.4196],
[-0.3412, -0.3569, -0.3882, ..., -0.4667, -0.4588, -0.4196]],
[[-0.6157, -0.5686, -0.5922, ..., -0.2863, -0.2784, -0.2706],
[-0.6941, -0.6549, -0.6078, ..., -0.2784, -0.2784, -0.2471],
[-0.6392, -0.6314, -0.5843, ..., -0.2471, -0.2706, -0.2078],
...,
[-0.4431, -0.5059, -0.5059, ..., -0.5608, -0.5765, -0.5451],
[-0.4196, -0.4824, -0.5059, ..., -0.5686, -0.5373, -0.5451],
[-0.4745, -0.4902, -0.5294, ..., -0.5922, -0.5843, -0.5451]],
[[-0.6627, -0.6157, -0.6549, ..., -0.5059, -0.5216, -0.5137],
[-0.7412, -0.7020, -0.6706, ..., -0.4980, -0.5216, -0.4902],
[-0.6863, -0.6784, -0.6471, ..., -0.4667, -0.4902, -0.4275],
...,
[-0.6000, -0.6549, -0.6627, ..., -0.6784, -0.6941, -0.6627],
[-0.5765, -0.6314, -0.6471, ..., -0.6863, -0.6549, -0.6627],
[-0.6314, -0.6314, -0.6392, ..., -0.7098, -0.7020, -0.6627]]]), 0)
'''
下面我们再看一下dataset
的三个属性:
print(dataset.classes) #根据分的文件夹的名字来确定的类别
print(dataset.class_to_idx) #按顺序为这些类别定义索引为0,1...
print(dataset.imgs) #返回从所有文件夹中得到的图片的路径以及其类别
'''
输出:
['cat', 'dog']
{'cat': 0, 'dog': 1}
[('./data/train\\cat\\1.jpg', 0),
('./data/train\\cat\\2.jpg', 0),
('./data/train\\dog\\1.jpg', 1),
('./data/train\\dog\\2.jpg', 1)]
'''