# Faster RCNN yields hundred maches every time

**URL:** <https://discuss.pytorch.org/t/faster-rcnn-yields-hundred-maches-every-time/100279>\
**Category:** vision\
**Created:** [October 22, 2020, 1:49pm UTC](https://discuss.pytorch.org/t/faster-rcnn-yields-hundred-maches-every-time/100279 "2020-10-22T13:49:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vahvero](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@vahvero](https://discuss.pytorch.org/u/vahvero)\
**Post date:** [October 22, 2020, 1:49pm UTC](https://discuss.pytorch.org/t/faster-rcnn-yields-hundred-maches-every-time/100279/1 "2020-10-22T13:49:49Z")

</div>

Hi!

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.

 ![rcnn_trainset](https://discuss.pytorch.org/uploads/default/original/3X/c/c/ccfdbdc23ea6abf4e6a5987602308c610e5efa74.jpeg)

which I train

```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

        <more code>
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()

```

Result from unrelated image from TCGA

 ![rcnn_carbage](https://discuss.pytorch.org/uploads/default/original/3X/f/b/fb9a4bdc69fbc0fcf3cbd52177ab202f721ee264.jpeg)

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?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 23, 2020, 11:03am UTC](https://discuss.pytorch.org/t/faster-rcnn-yields-hundred-maches-every-time/100279/2 "2020-10-23T11:03:44Z")

</div>

You can define the max. number of detections via the `box_detections_per_img` argument.

---

<div class="post-metadata">

**Author:** ![vahvero](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/5e9695/32.png) [@vahvero](https://discuss.pytorch.org/u/vahvero)\
**Post date:** [October 26, 2020, 8:58am UTC](https://discuss.pytorch.org/t/faster-rcnn-yields-hundred-maches-every-time/100279/3 "2020-10-26T08:58:21Z")

</div>

Transforming

```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)

```

to

```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)

```

solved first part of the problem.
