ptrblck:

Could you post the code for your dataset?


IMG_EXTENSIONS = [ '.jpg', '.JPG', '.jpeg', '.JPEG','.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',]
def is_image_file(filename):
    return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)
def find_classes(dir):
    classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
    classes.sort()
    class_to_idx = {classes[i]: i for i in range(len(classes))}
    return classes, class_to_idx
def make_dataset(dir, class_to_idx):
    images = []
    dir = os.path.expanduser(dir)
    for target in sorted(os.listdir(dir)):
        d = os.path.join(dir, target)
        if not os.path.isdir(d):
            continue
        for root, _, fnames in sorted(os.walk(d)):
            for fname in sorted(fnames):
                if is_image_file(fname):
                    path = os.path.join(root, fname)  # 图片的路径
                    item = (path, class_to_idx[target])  # (图片的路径,图片类别)
                    images.append(item)
    return images
def default_loader(path):
    return Image.open(path).convert('L')
class ImageFolder(data.Dataset):
    def __init__(self, root, train=True,transform=None, target_transform=None,loader=default_loader):
        classes, class_to_idx = find_classes(root)
        imgs = make_dataset(root, class_to_idx)
        if len(imgs) == 0:
            raise (RuntimeError("Found 0 images in subfolders of: " + root + "/n"
                            "Supported image extensions are: " + ",".join(IMG_EXTENSIONS)))
        self.root = root
        self.imgs = imgs
        self.classes = classes
        self.class_to_idx = class_to_idx
        self.transform = transform
        self.target_transform = target_transform
        self.loader = loader
        self.train = train
    def __getitem__(self, index):
        path, target = self.imgs[index]
        img = self.loader(path)
        if self.transform is not None:
            img = self.transform(img)
       if self.target_transform is not None:
            target = self.target_transform(target)

        return img, target

    def __len__(self):
        return len(self.imgs)
mytransform = transforms.Compose([transforms.ToTensor()])
train_data =ImageFolder(root = "/home/zw/ztfd_data/final01/train",train=True,transform = mytransform)
print(len(train_data))
test_data =ImageFolder(root = "/home/zw/ztfd_data/final01/test",train=False,transform = mytransform)
print(len(test_data))
train_loader=torch.utils.data.DataLoader(
     ImageFolder(root = "/home/zw/ztfd_data/final01/train",train=True,transform=mytransform),
           batch_size=64,shuffle=True)
test_loader=torch.utils.data.DataLoader(
     ImageFolder(root = "/home/zw/ztfd_data/final01/test",train=False,transform=mytransform),
           batch_size=64,shuffle=True)
print(len(train_loader))
print(len(test_loader))