当使用torchvision.datasets.ImageFolder读取猫狗数据集时,dataset中存的图片是 '猫狗猫狗猫狗猫狗' 还是 '猫猫猫猫狗狗狗狗' 呢?
数据集文件的存放路径如下图
测试代码如下
import torch
import torchvisiontransform = torchvision.transforms.Compose([torchvision.transforms.Resize((512,512)), # 调整图像大小为 224x224torchvision.transforms.ToTensor(), # 转换为张量torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # 归一化
])
dataset = torchvision.datasets.ImageFolder('C:\\Users\\ASUS\\PycharmProjects\\pythonProject1\\cats_and_dogs_train',transform=transform)val_ratio = 0.2
val_size = int(len(dataset) * val_ratio)
train_size = len(dataset) - val_size
train_dataset = torch.utils.data.Subset(dataset, range(train_size))
val_dataset = torch.utils.data.Subset(dataset, range(train_size, len(dataset)))cats_num = 0
dogs_num = 0
for x,y in train_dataset:if y == 0:cats_num += 1else:dogs_num += 1print("cats_num: ",cats_num)
print("dogs_num: ",dogs_num)cats_num2 = 0
dogs_num2 = 0
for x,y in val_dataset:if y == 0:cats_num2 += 1else:dogs_num2 += 1print("cats_num2: ",cats_num2)
print("dogs_num2: ",dogs_num2)
输出结果如下
可以得知,是 '猫猫猫猫狗狗狗狗'