Pytorch dataloader: data shape issue with custom COCO dataset for model finetuning

username: alelom
cooked: <p>I have an object detection task for which I prepared images and annotations*. The objective to is fine-tune an existing model with Pytorch. Images (PNGs) are stored in the same folder where the COCO json annotations are stored. The json annotations use the <a href="https://cocodataset.org/#format-data" rel="noopener nofollow ugc">Object Detection</a> COCO format:</p>
<details>
<pre data-code-wrap="json"><code class="lang-json">
    "info": {...},
     "images": [
            {
                "id": 1,
                "width": 4961,
                "height": 3508,
                "file_name": "somefile.png",
                "license": 1,
                "coco_url": null,
                "date_captured": null
            },
            ...
     ],
     "annotations": [
            {
                "id": 1,
                "image_id": 1,
                "category_id": 3,
                "segmentation": [[4372,2830], [4803,2830],[4803,3131],[4372,3131]],
                "area": 129731,
                "bbox": [4587.5,2980.5,431,301],
                "iscrowd": 0
            }, ...
    ],
    "categories": [
            {
                "id": 1,
                "name": "someCategoryName",
                "supercategory": "someSuperCategoryName"
            },
            ...
    ],
    "licenses": [...]
</code></pre>
</details>
<p>I need to read this dataset to fine-tune an existing model. This is how I’m opening it:</p>
<pre data-code-wrap="Python"><code class="lang-Python"># By default, define a transform to convert PIL image to a Torch tensor
transform = transforms.Compose([transforms.PILToTensor()])
train_dataset = dset.CocoDetection(root=dataset_path, annFile=annotationFilePath, transform=transform)
</code></pre>
<p>I’m then defining a Data Loader as follows. The dataloader <code>next()</code> function works fine:</p>
<pre data-code-wrap="Python"><code class="lang-Python">train_loader = DataLoader(train_dataset, batch_size=4, shuffle=True, num_workers=4, collate_fn=lambda batch: tuple(zip(*batch)))
images, targets = next(iter(train_loader)) # check that `next()` works
</code></pre>
<p>The content of <code>images</code> and <code>targets</code> objects is like this:</p>
<pre data-code-wrap="json"><code class="lang-json">images: [tensor([[[255, 255, ...rch.uint8), tensor([[[255, 255, ...rch.uint8)]
targets: [{'id': 265, 'image_id': 265, 'category_id': 2, 'segmentation': [...], 'area': 1726288, 'bbox': [...], 'iscrowd': 0}]
</code></pre>
<p>In particular, note that <strong><code>targets</code></strong> is a list of dictionaries, where <em>each dictionary retains the shape of the <code>annotations</code> entry</em> in the json (see the code snippet).</p>
<p>Finally, I test if the <code>model</code> function can actually run, with:</p>
<pre data-code-wrap="Python"><code class="lang-Python">model = models.detection.fasterrcnn_mobilenet_v3_large_fpn(weights=torchvision.models.detection.FasterRCNN_MobileNet_V3_Large_FPN_Weights.COCO_V1)
output = model(images, targets)  # check if `model()` works 
</code></pre>
<p>However, this always raises:</p>
<blockquote>
<p>TypeError: list indices must be integers or slices, not str</p>
</blockquote>
<p>This error comes from the <code>forward()</code> method of <code>generalized_rcnn.py</code>, <a href="https://github.com/pytorch/vision/blob/d80d5bd21c6ee677af50d39689ba7005645b7ef4/torchvision/models/detection/generalized_rcnn.py#L64C20-L65" rel="noopener nofollow ugc">because it expects a  “boxes” key entry when iterating the <code>targets</code></a>.</p>
<p>Now, I would have expected the <code>DataLoader</code> to appropriately shape the <code>targets</code> for them to work with the <code>forward()</code> method, given that I’m using a COCO dataset. Why isn’t that so? What should I do to make it work? I cannot seem to find anything in the documentation.</p>
<p>*<sub> For completeness: to label, my company needs to use a specific PDF annotator. Therefore, I wrote my own converter from that output to get a COCO-shaped dataset. There may be something incorrect in the COCO json annotations I obtained, but I personally do not think this error is related to that.<br>
</sub></p>