Similar to mnist single-channel dataset how to customize Dataset , each time there is not an error, but when test, the data size is 0, God knows how to get it
def default_loader(path):
return Image.open(path).convert(‘L’)
class myImageFloder(data.Dataset):
def init(self, root, label,train=True,transform=None, target_transform=None, loader=default_loader):
fh = open(label)#实现标签数据的读入
c = 0
imgs = []
class_names = []
for line in fh.readlines():
if c == 0:
class_names = [n.strip() for n in line.rstrip().split(’   ')]#claassname为标签的属性名
else:
cls = line.split()
fn = cls.pop(0)
if os.path.isfile(os.path.join(root, fn)):
imgs.append((fn, tuple([float(v) for v in cls])))
c = c + 1
self.root = root
self.imgs = imgs
self.classes = class_names#计算分类类别
self.transform = transform
self.target_transform = target_transform
self.loader = loader
self.train = train
def getitem(self, index):
    fn, label = self.imgs[index]  #fn代表path   label代表target,这里返回的是图片路径，而需要的是图片格式

    if self.train:
        img, target = self.train_data[index], self.train_labels[index]
    else:
        img, target = self.test_data[index], self.test_labels[index]
    img = Image.fromarray(img.numpy(), mode='L')
    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):
    if self.train:
        return 60000
    else:
        return 10000

def getName(self):
    return self.classes