torchvision.dataset.ImageFolder报错FileNotFoundError: Couldn‘t find any class folder

     代码来自博客的transforms.normalize如何对特定数据集设定标准化参数,但报错了,代码段和报错如下:   

import torch
from torchvision.datasets import ImageFolder
from torchvision import transforms


def getStat(train_data):
    '''
    Compute mean and variance for training data
    :param train_data: 自定义类Dataset(或ImageFolder即可)
    :return: (mean, std)
    '''
    print('Compute mean and variance for training data.')
    print(len(train_data))
    train_loader = torch.utils.data.DataLoader(
        train_data, batch_size=1, shuffle=False, num_workers=0,
        pin_memory=True)
    mean = torch.zeros(3)
    std = torch.zeros(3)
    for X, _ in train_loader:
        for d in range(3):
            mean[d] += X[:, d, :, :].mean()
            std[d] += X[:, d, :, :].std()
    mean.div_(len(train_data))
    std.div_(len(train_data))
    return list(mean.numpy()), list(std.numpy())


if __name__ == '__main__':
    train_dataset = ImageFolder(root=r"/CapsNet-master/data/Add_gender", transform=transforms.ToTensor())
    print(getStat(train_dataset))

torchvision.dataset.ImageFolder报错FileNotFoundError: Couldn‘t find any class folder_第1张图片

解决方法:因为我的图片数据都在Add_gender目录下,但ImageFolder调用的只能是一个数据包,这里可以理解成Add_gender文件夹就是那个数据包,所以只需要删除Add_gender就可以把图片全部加载出来

 

你可能感兴趣的:(python,深度学习,机器学习)