Skip to content

assert error len(grid_sizes) == len(strides) == len(cell_anchors) #3246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Jan 13, 2021 · 3 comments
Closed

assert error len(grid_sizes) == len(strides) == len(cell_anchors) #3246

ghost opened this issue Jan 13, 2021 · 3 comments
Labels

Comments

@ghost
Copy link

ghost commented Jan 13, 2021

It looks like a bug. When I do not set the AnchorGenerator() in FasterRCNN, the default anchor_sizes in ### detection/faster_rcnn.py line182 shows that 'anchor_sizes = ((32,), (64,), (128,), (512,))' which cause len(cell_anchors) == 5. And I found that in the detection/faster_rcnn.py line120 the anchor_size set '((32, 64, 128, 256, 512), )' and len(cell_anchors) == 1

@oke-aditya
Copy link
Contributor

oke-aditya commented Jan 13, 2021

Hi !

I think this error is fixed on master. #2971 #2960 #2983 #2947.

This would be error message on master / next release

if not (len(grid_sizes) == len(strides) == len(cell_anchors)):
    raise ValueError("Achors should be Tuple[Tuple[int]] because each feature "
    "map could potentially have different sizes and aspect ratios. "
    "There needs to be a match between the number of "
    "feature maps passed and the number of sizes / aspect ratios specified.")

In short, you need to pass a Tuple[Typle[int]] instead of a Tuple[int] to Anchor Generator.
This was done to avoid potentially bad results.

Also, I think we should change Line 121 from FRCNN to Tuple[Tuple[]] ?

It think that above line is causing confusion

@datumbox
Copy link
Contributor

@alpha-gradient As @oke-aditya mentioned, the error message has been updated to make the situation less confusing.

Here is a simplified version of the code that you are quoting:

backbone = torchvision.models.mobilenet_v2(pretrained=True).features
backbone.out_channels = 1280

anchor_generator = AnchorGenerator(sizes=((32, 64, 128, 256, 512),),
                                   aspect_ratios=((0.5, 1.0, 2.0),))

model = FasterRCNN(backbone, num_classes=2, rpn_anchor_generator=anchor_generator)

The above snippet uses sizes=((32, 64, 128, 256, 512),), or in other words defines 1 level/group of 5 anchor-sizes. Why 1 level/group? Because the backbone provides only 1 ouput.

On the other hand the default anchors used in faster-rcnn is ((32,), (64,), (128,), (256,), (512,)) which means we have 5 levels/groups with 1 anchor size:

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

Why is that? This is because by default it uses a Feature Pyramid as a backbone which returns 5 outputs (intermediate layers of the original backbone).

The error message that you got basically indicates that the number of outputs on the backbone should match the number of levels of anchor sizes.

@fmassa
Copy link
Member

fmassa commented Jan 20, 2021

Closing following @oke-aditya and @datumbox great answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants