A problem with torch.stack()

username: John_Wales
cooked: <p>DataLoader produces an error.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/5/551effedb89dcd7bfa1ef3b1b02535e19872b02b.png" data-download-href="https://discuss.pytorch.org/uploads/default/551effedb89dcd7bfa1ef3b1b02535e19872b02b" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/5/551effedb89dcd7bfa1ef3b1b02535e19872b02b.png" alt="image" data-base62-sha1="c91065zYzcKrWFXkcaTts3rEANJ" width="690" height="44" data-dominant-color="383B40"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1325×85 10.9 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
The code for DataLoader is below:</p>
<pre><code class="lang-auto">class PandaDataset(Dataset):
    def __init__(self, path, train, transform):
        self.path = path
        self.names = list(pd.read_csv(train).image_id)
        self.labels = list(pd.read_csv(train).isup_grade)
        self.transform = transform
        
    def __len__(self):
        return len(self.names)

    def __getitem__(self, idx):
        label = self.labels[idx]
        name = self.names[idx]
        img = skimage.io.MultiImage(os.path.join(self.path,name+'.tiff'))[-1]
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        
        list_of_images = []
        tiles = tile(img)
        for img in tiles:

            if x:
                augmented = get_transforms(data='valid')(image=img)
                img = augmented['image']

            list_of_images.append(img)

        new_tiles=torch.stack(list_of_images, axis=0)
    
        
        return new_tiles, torch.Tensor(label)
</code></pre>
<p>when I tried to debug it with code below outside of DataLoader it works fine.</p>
<pre><code class="lang-auto">img = skimage.io.MultiImage(os.path.join(train_images,'0005f7aaab2800f6170c399693a96917'+'.tiff'))[-1]
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

list_of_images = []
tiles = tile(img)
for img in tiles:

    if x:
        augmented = get_transforms(data='train')(image=img)
        img = augmented['image']

    list_of_images.append(img)

new_tiles=torch.stack(list_of_images, axis=0)
</code></pre>
<p><code>def tile</code> simply crops and image to 12 images and append them to numpy array.<br>
Thanks.</p>

username: ptrblck
cooked: <p>Could you print the shapes of all images inside the <code>list_of_images</code> before calling <code>torch.stack</code>?<br>
It seems that some dimensions create a shape mismatch. Are you dealing with differently shapes images?</p>

username: John_Wales
cooked: <p>sure.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/4/0416bbdd714df5f7a58621d41462e84a0e7cd7ba.png" data-download-href="https://discuss.pytorch.org/uploads/default/0416bbdd714df5f7a58621d41462e84a0e7cd7ba" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/4/0416bbdd714df5f7a58621d41462e84a0e7cd7ba_2_690x289.png" alt="image" data-base62-sha1="AaCfsHEcjpNOEo1l4ZP9RTMMoq" width="690" height="289" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/4/0416bbdd714df5f7a58621d41462e84a0e7cd7ba_2_690x289.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/4/0416bbdd714df5f7a58621d41462e84a0e7cd7ba_2_1035x433.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/4/0416bbdd714df5f7a58621d41462e84a0e7cd7ba.png 2x" data-dominant-color="5C5E62"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1145×480 40.4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>All images have the same dimensions, as i said before, when i am trying to debug it outside of DataLoader it works fine.</p>

username: ptrblck
cooked: <p>Since all images have the same shape, the <code>torch.stack</code> call should work:</p>
<pre><code class="lang-python">list_of_images = [torch.randn(3, 128, 128) for _ in range(10)]
x = torch.stack(list_of_images, 0)
print(x.shape)
&gt; torch.Size([10, 3, 128, 128])
</code></pre>
<p>Also, a <code>Dataset</code> with equal shapes works fine:</p>
<pre><code class="lang-python">class PandaDataset(Dataset):
    def __init__(self):
        pass
    
    def __len__(self):
        return 20

    def __getitem__(self, idx):
        list_of_images = []
        tiles = torch.randn(10, 3, 128, 128)
        for img in tiles:
            list_of_images.append(img)
        new_tiles=torch.stack(list_of_images, axis=0)    
        
        return new_tiles, torch.tensor(0)

dataset = PandaDataset()
loader = DataLoader(dataset, batch_size=5)

for data, target in loader:
    print(data.shape)

&gt; torch.Size([5, 10, 3, 128, 128])
torch.Size([5, 10, 3, 128, 128])
torch.Size([5, 10, 3, 128, 128])
torch.Size([5, 10, 3, 128, 128])
</code></pre>

username: John_Wales
cooked: <p>Thanks, somehow I was able to fix it. <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:"></p>