ValueError: Sequence must have length 1, got 3.?

wangtz: <p>class SegDataset(Dataset):<br>
def <strong>init</strong>(self, root_dir, train, size):<br>
self.root_dir = root_dir<br>
self.train = train<br>
self.size = size</p>
<pre><code>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 &amp; 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
</code></pre>
<hr>
<p><strong>ValueError</strong> Traceback (most recent call last) <strong>f:\jupyterlab\Project pipeline\mulStage.ipynb Cell 5</strong> in &lt;cell line: 2&gt;<strong>()</strong> <a>3</a> loss, step = 0, 0 <a>4</a> model.train() <strong>----&gt; <a>6</a></strong> for img, lab in loader_train: <a>7</a> print(img.shape) File <strong>f:\Python310\lib\site-packages\torch\utils\data\dataloader.py:530</strong>, in _BaseDataLoaderIter.<strong>next</strong><strong>(self)</strong> 528 if self._sampler_iter is None: 529 self._reset() <strong> → 530</strong> 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 &gt; self._IterableDataset_len_called: File <strong>f:\Python310\lib\site-packages\torch\utils\data\dataloader.py:570</strong>, in _SingleProcessDataLoaderIter._next_data**(self)** 568 def _next_data(self): 569 index = self._next_index() # may raise StopIteration <strong> → 570</strong> data = self._dataset_fetcher.fetch(index) # may raise StopIteration 571 if self._pin_memory: 572 data = _utils.pin_memory.pin_memory(data) File <strong>f:\Python310\lib\site-packages\torch\utils\data_utils\fetch.py:49</strong>, in _MapDatasetFetcher.fetch**(self, possibly_batched_index)**</p>
<p>…</p>
<p>151 if len(tup) == dim: 152 return tuple(tup) <strong> → 154</strong> raise ValueError(f"Sequence must have length {dim}, got {len(tup)}.")</p>
<p><strong>ValueError</strong>: Sequence must have length 1, got 3.</p>
<p>How to address this error? thanks</p>

nivek: <p>Hi,</p>
<p>Can you post the full traceback in a readable way? It is unclear from which file the exception is raised.</p>