I'm using the custom collate_fn function, but there was a problem in fetch step

Jue0: <p>I needed a dataset with channel extended images and bbox labels.<br>
So I set a custom dataset and dataloader that was a inheritance of PyTorch Dataset and DataLoader.<br>
Because the labels of my dataset has variable lengths I created a custom collate_fn function and applied it as ‘self.collate_fn’.<br>
I think it works as I intended.</p>
<p>But when the ‘fetch’ step, an error occurs.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/d/7d3dd4450b8a7cc75544813e23808a79c6b32a32.png" data-download-href="https://discuss.pytorch.org/uploads/default/7d3dd4450b8a7cc75544813e23808a79c6b32a32" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d3dd4450b8a7cc75544813e23808a79c6b32a32_2_690x163.png" alt="image" data-base62-sha1="hRW9r5XqBTx3APcQGnllfaHRHTI" width="690" height="163" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d3dd4450b8a7cc75544813e23808a79c6b32a32_2_690x163.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/d/7d3dd4450b8a7cc75544813e23808a79c6b32a32_2_1035x244.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/d/7d3dd4450b8a7cc75544813e23808a79c6b32a32.png 2x" data-dominant-color="48171A"><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">1146×272 50.8 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>At the line ‘return self.collate_fn(data)’, that ‘self.collate_fn()’ is a list object already.<br>
Why is that? I thought it would be a function object.<br>
I’d like to know what makes that error.</p>
<p>Here’s my custom collate_fn.<br>
No problem with these return values? Thanks for your help.</p>
<pre><code class="lang-auto">def collate_fn_ju0(dataset):

    print('collating..')
    images = [d[0] for d in dataset]
    labels = [d[1] for d in dataset]

    print('converting ndarray to tensor..\n')
    for i in range(dataset.total_num_samples):
        images[i] = torch.from_numpy(images[i])
        labels[i] = torch.from_numpy(labels[i])

    # images_converted = torch.stack(images, 0).permute(0, 3, 1, 2)
    images_converted = torch.stack(images, 0).contiguous().permute(0, 3, 1, 2)

    max_num_labels = max(label.shape[0] for label in labels)
    labels_padded = torch.zeros(len(labels), max_num_labels, 5)
        
    if max_num_labels &gt; 0:

        # label_padded = torch.ones((len(labels), max_num_labels, 5)) * -1
        label_padded = torch.zeros((len(labels), max_num_labels, 5)) * -1

        if max_num_labels &gt; 0:
            for idx, label in enumerate(labels):
                # print(idx, label.shape[0])
                if label.shape[0] &gt; 0:
                    label_padded[idx, :label.shape[0], :] = label
    else:
        # label_padded = torch.ones((len(labels), 1, 5)) * -1
        label_padded = torch.zeros((len(labels), 1, 5)) * -1

    return images_converted, labels_padded
</code></pre>

ptrblck: <p>I don’t fully understand your use case as it seems your custom <code>collate_fn</code> is iterating the entire dataset and “concerts” the data and targets, while its original purpose is to create a batch (or batch-like object) from the already processed items returned by <code>Dataset.__getitem__</code>.<br>
Could you explain your use case a bit more and how you are using this <code>collate_fn</code>?</p>

Jue0: <p>Thanks for replying.<br>
I found out what the problem is.<br>
It should be called collate_fn, but collate_fn(data) was the cause.</p>