PyTorch fasterrcnn-resnet50_fpn detects only one class objects

I posted this because maybe it’ll help to someone. In my case the problem was in annotations data (COCO json).

Description

I want a model for detecting features on custom images. I used officially available code from here (changed and adapted for my purposes but it turned out that changes in code wasn’t a problem).

Issue

Prediction detects only one class of objects. However, dataset images are the same size, have only two colors, presents up to four two well-distinctive shapes (for ex. circle and cross). Distribution of objects is uniformed (nearly 50% of objects of each class). Many different settings of parameters (learning rates, optimizers, schedulers, moments, learning steps, number of batches, number of epochs, weights decays, etc.) didn’t help.

Cause of the problem and solution

In my case I use annotations in COCO format (as JSON file). When “categories” dictionary contained only classes for detection, ex:

"categories": [{"id": 0, "name": "CROSS"}, {"id": 1, "name": "CIRCLE"}]

only circles have been detected on images. When there is an additional class for background, ex:

"categories": [{"id": 0, "name": "BACKGROUND"}, {"id": 1, "name": "CROSS"}, {"id": 2, "name": "CIRCLE"}]

There’s no problem. Category 0 is never used in this file but it seems to exist. Of course, annotations should have proper mapping now to IDs + 1. If this rule is described somewhere, please someone to put the explanation and link here. I found mentioned that the “0” category is needed for background but on different site.