Can't change the Anchors in Faster RCNN
I'm a newbie in pytorch and I was trying to put some custom anchors on my Faster RCNN network in pytorch. Basically, I'm using a resnet50 backbone and when I try to put the anchors, I got a mismatch error.

This is the code that I have:

backbone = torchvision.models.detection.backbone_utils.resnet_fpn_backbone('resnet50', True)
backbone.out_channels = 256

anchor_generator = AnchorGenerator(sizes=((4, 8, 16, 32, 64, 128),),
                                    aspect_ratios=((0.5, 1.0, 2.0),))

roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=[0],
                                                    output_size=7,
                                                    sampling_ratio=2)

model = FasterRCNN(backbone, 
                   num_classes=10,
                   rpn_anchor_generator=anchor_generator,
                   box_roi_pool=roi_pooler)



The error that I got is the following: shape '[1440000, -1]' is invalid for input of size 7674336.

Alright, after some digging into the source code of PyTorch Faster RCNN, I found how they initialize the anchors: 

anchor_sizes = ((32,), (64,), (128,), (256,), (512,))
            aspect_ratios = ((0.5, 1.0, 2.0),) * len(anchor_sizes)

rpn_anchor_generator = AnchorGenerator(
                anchor_sizes, aspect_ratios
            )


Following the same pattern for my custom anchors, the code will be:

anchor_sizes = ((4,), (8,), (16,), (32,), (64,), (128,))
            aspect_ratios = ((0.5, 1.0, 2.0),) * len(anchor_sizes)

rpn_anchor_generator = AnchorGenerator(
                anchor_sizes, aspect_ratios
            )



It will work!
