I have written a simple pythorc class to read images and generate Patches from them to obtain my own dataset . I’m using pythorch Dataloader but when I try to iterate trough the dataset it gives me an error:
train()
for i, data in enumerate(train_loader, 0):
return _DataLoaderIter(self)
self._put_indices()
indices = next(self.sample_iter, None)
in __iter__
for idx in self.sampler:
in __iter__
return iter(range(len(self.data_source)))
in __len__
raise NotImplementedError
NotImplementedError
this is my code
def read_image(image_path, **kwargs):
# cv2 requires int images or float between -1 and 1.
image, metadata = read_dcm(image_path, dtype=np.int32, **kwargs)
return image, metadata
def get_ROI(listP, views, patch_size, **kwargs):
num_total = num_pos + num_neg
print('num_pos', num_pos)
print('num_neg', num_neg)
print('num_total', num_total)
rois = roisP + roisN
grts = grtsP + grtsN
"""shuffle your dataset"""
c = list(zip(rois, grts))
random.shuffle(c)
rois, grts = zip(*c)
print('rois shape', np.shape(rois))
print('grts shape', np.shape(grts))
#
# max_w = max(width)
# max_l = max(length)
# print('max_w',max_w)
# print('max_l', max_l)
return rois,grts,num_total,num_pos,num_neg
class UNetMammoMC(Dataset):
"""UNetMammoMC dataset."""
def __init__(self, type_dataset, output_size, transform=None, compute_weights=True, **kwargs):
"""
Parameters
type_dataset: 'train val or test'
output_size: patch size
==========
"""
assert type_dataset in {'train', 'val', 'test'}, 'Choose one of `train`, `val` or `test`.'
self.rois = []
self.grts=[]
self.total=0
self.pos=0
self.neg=0
self.roi_w, self.roi_h=output_size,output_size
self.annotations = cfg.ANNOTATIONS_MC_ROOT
print(self.annotations)
self.lists = cfg.LISTS_MC[type_dataset.upper()]
print(self.lists)
print(cfg.LISTS_MC.ROOT)
self.pos_list = read_list(os.path.join(cfg.LISTS_MC.ROOT, self.lists))
"""Neg list has to be inserted"""
print('self.pos_list', self.pos_list) # image list
self.classes = ['normal', 'has_lesion']
self.idx_to_class = {0: 'normal', 1: 'has_lesion'}
self.n_classes = len(self.classes)
self.view = ['cl', 'cr', 'ml', 'mr']
self.rois,self.grts,self.total, self.pos, self.neg=get_ROI(self.pos_list, self.view, output_size)
print('Init: num total',self.total)
self.transform=transform
def __len__(self):
return self.total
def __getitem__(self, idx):
if idx < self.total:
sample = self.rois[idx]
grt = self.grts[idx]
# sample = torch.from_numpy(sample)
# grt = torch.from_numpy(grt)
return sample,grt
output_shape = 50
def load_dataset(is_train,output_shape):
return UNetMammoMC(
'train', output_shape, num_samples=10 if cfg.UNET.LIMIT_DATASET else False,transform=transforms.ToTensor())
def train(experiment_name='unet', resume=True):
train_dataset=load_dataset(True,output_shape)
width = train_dataset.roi_w
height = train_dataset.roi_h
num_cls = train_dataset.n_classes
train_loader = DataLoader(train_dataset,#batch_size=cfg.UNET.BATCH_SIZE,
batch_size=2,
num_workers=cfg.NUM_WORKERS)
for i, data in enumerate(train_loader, 0):
#inputs, grts = data
print('data')
if __name__ == '__main__':
train()