Faster RCNN yields hundred maches every time

username: vahvero
cooked: <p>Hi!</p>
<p>I have trained a model on MonuSeg-dataset (20k unique cells and from 30 unique images) which I transformed with min/maxing to bounding boxes.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/c/ccfdbdc23ea6abf4e6a5987602308c610e5efa74.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/ccfdbdc23ea6abf4e6a5987602308c610e5efa74" title="rcnn_trainset"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/ccfdbdc23ea6abf4e6a5987602308c610e5efa74_2_417x500.jpeg" alt="rcnn_trainset" data-base62-sha1="tfr2BLmCBIUQ9btFa4nacuxyuvW" width="417" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/ccfdbdc23ea6abf4e6a5987602308c610e5efa74_2_417x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/ccfdbdc23ea6abf4e6a5987602308c610e5efa74_2_625x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/c/ccfdbdc23ea6abf4e6a5987602308c610e5efa74_2_834x1000.jpeg 2x" data-dominant-color="CFA4BA"><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">rcnn_trainset</span><span class="informations">927×1111 444 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>which I train</p>
<pre><code class="lang-auto">def create_model(pretrained=False, num_classes=2):
        """Creates faster rcnn 50 model

        Args:
            pretrained (bool): To use Coco pretrained
            num_classes (int): Number of output classes
        """
        return tv.models.detection.fasterrcnn_resnet50_fpn(
            pretrained=pretrained,
            num_classes=num_classes,
            pretrained_backbone=True

        &lt;more code&gt;
def train():
        # Load data
        dataset = CellDataset()
        dataloader = DataLoader(
            dataset=dataset,
            batch_size=self.BATCH_SIZE,
            shuffle=True,
            pin_memory=True,
            num_workers=self.NUM_WORKERS,
            collate_fn=collate
        )

        # Create model
        model = self.create_model(
            pretrained=pretrained
        )
        model = model.to(self.device)
        model.train()

        # Pass parametres which need optimizing to the optimizer
        params = [p for p in model.parameters() if p.requires_grad]
        optimizer = torch.optim.SGD(
            params, lr=learning_rate, momentum=.9, weight_decay=5e-4)

        start = time.time()
        # Main train loop
        for epoch in range(self.epochs):
            # Reset loss
            epoch_loss = .0
            # Iterate over the dataloader
            for img, targets in dataloader:
                img = [item.to(self.device) for item in img]
                for target in targets:
                    for key in target:
                        target[key] = target[key].to(self.device)
                # Evaluate
                output = model(img, list(targets))
                # Sum losses of the output
                # Contains 4 types of loss
                # loss_classifier
                # loss_box_reg
                # loss_objectiveness
                # loss_rpn_box_reg
                losses = sum(output.values())

                # Backpropagate
                optimizer.zero_grad()
                losses.backward()
                optimizer.step()

                epoch_loss += losses.item()
</code></pre>
<p>Result from unrelated image from TCGA</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/b/fb9a4bdc69fbc0fcf3cbd52177ab202f721ee264.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/fb9a4bdc69fbc0fcf3cbd52177ab202f721ee264" title="rcnn_carbage"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb9a4bdc69fbc0fcf3cbd52177ab202f721ee264_2_690x413.jpeg" alt="rcnn_carbage" data-base62-sha1="zTMqfJQNF55UF52g4o41grM9OnO" width="690" height="413" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb9a4bdc69fbc0fcf3cbd52177ab202f721ee264_2_690x413.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb9a4bdc69fbc0fcf3cbd52177ab202f721ee264_2_1035x619.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/b/fb9a4bdc69fbc0fcf3cbd52177ab202f721ee264_2_1380x826.jpeg 2x" data-dominant-color="E8D6E1"><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">rcnn_carbage</span><span class="informations">1853×1111 700 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>But all the results seem complete garbage and the prediction is always 100 boxes (this is odd) and score nearly identical for all boxes. I would have assumed at least some human readable results with varying amounts detected with unique scores. How to increase the maximum amount of matches detected?</p>

username: ptrblck
cooked: <p>You can define the max. number of detections via the <code>box_detections_per_img</code> argument.</p>

username: vahvero
cooked: <p>Transforming</p>
<pre><code class="lang-auto">def create_model(pretrained=False, num_classes=2):
        """Creates faster rcnn 50 model

        Args:
            pretrained (bool): To use Coco pretrained
            num_classes (int): Number of output classes
        """
        return tv.models.detection.fasterrcnn_resnet50_fpn(
            pretrained=pretrained,
            num_classes=num_classes,
            pretrained_backbone=True)
</code></pre>
<p>to</p>
<pre><code class="lang-auto">def create_model(pretrained=False, num_classes=2):
        """Creates faster rcnn 50 model

        Args:
            pretrained (bool): To use Coco pretrained
            num_classes (int): Number of output classes
        """
        return tv.models.detection.fasterrcnn_resnet50_fpn(
            pretrained=pretrained,
            num_classes=num_classes,
            pretrained_backbone=True,
            box_detections_per_img=1000)
</code></pre>
<p>solved first part of the problem.</p>