Issue with stack and object detection dataloader

4158ndfkvBHJ1: <p>Hello,<br>
I am confused by an error I am getting while implementing an object-detection dataloader. This dataloaders returns an image (as a tensor) and a dictionnary, containing a tensor of bounding boxes, and a tensor of labels. I wrote the following code (inspired from <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 1.10.0+cu102 documentation</a>):</p>
<pre><code class="lang-python">class RCNNDataset(Dataset):
        
    def __init__(self, root_dir: str,
                 transforms = Normalize(mean = (0.92,), std = (0.15,)),
                 image_size:tuple=(1008,888)):
                
        self.root_dir = root_dir
        self.label_dir = os.path.join(self.root_dir, "labels")
        self.image_dir = os.path.join(self.root_dir, "images")
        self.images = sorted(os.listdir(self.image_dir))
        self.labels = sorted(os.listdir(self.label_dir))
        self.to_tensor = ToTensor()
        self.transforms = transforms
        self.image_size = image_size

    def __len__(self):
        
        return len(self.label_dir)
    
    def __getitem__(self, idx):

        image = Image.open(os.path.join(self.image_dir, self.images[idx]))
        if image.size != self.image_size:
            image = image.resize(size=self.image_size)

        image = self.to_tensor(image)

        if self.transforms is not None:

            image = self.transforms(image)

        label_array = np.loadtxt(os.path.join(self.label_dir, self.labels[idx]))
        targets = {"labels": torch.from_numpy(label_array[:,-1]),
                   "boxes": torch.from_numpy(label_array[:,0:4])}

        return image, targets
</code></pre>
<p>When I try to iterate through this dataloader (i.e doing <code>images, targets = next(iter(dataloader))</code>, I get the following error:</p>
<pre><code class="lang-auto">RuntimeError: stack expects each tensor to be equal size, but got [47] at entry 0 and [46] at entry 1
</code></pre>
<p>As I understand it, the stack function is expecting my tensors to always have the same shape for every element of the batch (so the same number of objects to detect in an image) ? I am very confused by this.</p>
<p>I would be grateful towards anybody that could help me understand this <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=10" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Thiru: <p>Actually You need not create any tensor for labels and boxes. The collate_fn will create the tensor for those labels and targets. Remove that <code>torch.from_numpy()</code> and try again.</p>
<p>I got the same issue today morning, the problem was I created tensor for labels.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cd1cf34a2c15735352828689c012b7772eafa1b4.png" data-download-href="https://discuss.pytorch.org/uploads/default/cd1cf34a2c15735352828689c012b7772eafa1b4" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd1cf34a2c15735352828689c012b7772eafa1b4_2_690x388.png" alt="image" data-base62-sha1="tgvUfAHHRRa5QL5F258VoiLmfsM" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd1cf34a2c15735352828689c012b7772eafa1b4_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd1cf34a2c15735352828689c012b7772eafa1b4_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cd1cf34a2c15735352828689c012b7772eafa1b4_2_1380x776.png 2x" data-dominant-color="E4E5E5"><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">1920×1080 144 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>

4158ndfkvBHJ1: <p>This still doesn’t work. Even if I don’t pass a dictionnary of tensors, torch tries to use the default collate function (so torch.stack) and fails. I don’t understand because my code is basically exactly the RCNN example, and this example doesn’t implement a custom collate function.</p>
<p>EDIT: the PyTorch example actually does implement a custom collate function that look something like this:</p>
<pre><code class="lang-auto">def collate_fn(batch):
    return tuple(zip(*batch))  
</code></pre>
<p>doing <code>dataloader = DataLoader(dataset, batch_size=bsize, shuffle=True, drop_last=True, collate_fn=collate_fn)</code> then seems to solve my problem.</p>