class SegDataset(Dataset):
def init(self, root_dir, train, size):
self.root_dir = root_dir
self.train = train
self.size = size
def __len__(self):
list = os.listdir(self.root_dir + '/ss/')
len_data = len(list)
print(len_data)
return len_data
def __getitem__(self, index):
# conect path
pathImage = glob.glob(self.root_dir + '/ss/*')[index]
pathLabel = glob.glob(self.root_dir + '/mask/*')[index]
# read nii.gz
Imgloader = LoadImage(image_only=True)
Labloader = LoadImage(image_only=True, dtype=np.uint8)
img = Imgloader(pathImage)
lab = Labloader(pathLabel)
Totnsor = ToTensor(device="cuda:0") # numpy TO tensor √
img = Totnsor(img)
lab = Totnsor(lab)
img = img.unsqueeze(dim=0) # add channel √
lab = lab.unsqueeze(dim=0)
# resize √
Resizer = Resize(spatial_size=(self.size, self.size))
img = Resizer(img)
lab = Resizer(lab)
print(pathImage,pathLabel, '//',index,img.shape,lab.shape)
# normalization & rescale
Normaler = NormalizeIntensity(subtrahend=0, divisor=.5, nonzero=True)
img = Normaler(img)
Rescler = ScaleIntensity(maxv=1, minv=0)
img = Rescler(img)
if self.train is True:
# rotate flip
if random.choice([True, False]):
Rflip = Flip(spatial_axis=0)
img = Rflip(img)
lab = Rflip(lab)
if random.choice([True, False]):
RN = np.random.randint(-45, 45, 1).item()/100
Rrotate = Rotate(angle=(0, 0, RN))
img = Rrotate(img)
lab = Rrotate(lab)
return img, lab
ValueError Traceback (most recent call last) f:\jupyterlab\Project pipeline\mulStage.ipynb Cell 5 in <cell line: 2>() 3 loss, step = 0, 0 4 model.train() ----> 6 for img, lab in loader_train: 7 print(img.shape) File f:\Python310\lib\site-packages\torch\utils\data\dataloader.py:530, in _BaseDataLoaderIter.next(self) 528 if self._sampler_iter is None: 529 self._reset() → 530 data = self._next_data() 531 self._num_yielded += 1 532 if self._dataset_kind == _DatasetKind.Iterable and \ 533 self._IterableDataset_len_called is not None and \ 534 self._num_yielded > self._IterableDataset_len_called: File f:\Python310\lib\site-packages\torch\utils\data\dataloader.py:570, in _SingleProcessDataLoaderIter._next_data**(self)** 568 def _next_data(self): 569 index = self._next_index() # may raise StopIteration → 570 data = self._dataset_fetcher.fetch(index) # may raise StopIteration 571 if self._pin_memory: 572 data = _utils.pin_memory.pin_memory(data) File f:\Python310\lib\site-packages\torch\utils\data_utils\fetch.py:49, in _MapDatasetFetcher.fetch**(self, possibly_batched_index)**
…
151 if len(tup) == dim: 152 return tuple(tup) → 154 raise ValueError(f"Sequence must have length {dim}, got {len(tup)}.")
ValueError: Sequence must have length 1, got 3.
How to address this error? thanks