报错:
Traceback (most recent call last):
File "train_unsupervised.py", line 107, in <module>
drop_last=False)
File "/home/xinying/self-supervised-relational-reasoning-master/datamanager.py", line 417, in get_train_loader
num_workers=num_workers, pin_memory=True, drop_last=drop_last)
File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 224, in __init__
sampler = RandomSampler(dataset, generator=generator)
File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/sampler.py", line 94, in __init__
if not isinstance(self.num_samples, int) or self.num_samples <= 0:
File "/opt/conda/lib/python3.7/site-packages/torch/utils/data/sampler.py", line 102, in num_samples
return len(self.data_source)
TypeError: object of type 'Living_Body' has no len()
在数据加载中,加入len函数,用于计算总的数据量是多少
class Living_Body(Dataset):
def __init__(self, img_path, repeat_augmentations, **kwds):
super().__init__()
self.repeat_augmentations = repeat_augmentations
self.img_path = img_path
self.imgs = []
self.targets = []
for img in os.listdir(self.img_path):
img_path = os.path.join(self.img_path, img)
self.imgs.append(img_path)
def __len__(self):
return len(self.imgs)
def __getitem__(self, index):
print(self.data[index])
#img, target = self.data[index], self.targets[index]
img = Image.open(self.imgs[index])
pic = Image.fromarray(img)
img_list = list()
if self.transform is not None:
for _ in range(self.repeat_augmentations):
img_transformed = self.transform(pic.copy())
img_list.append(img_transformed)
else:
img_list = None
if self.target_transform is not None:
target = self.target_transform(target)
return img, img_list, img#return img, img_list, target