Skip to content
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

Remove _has_warned attribute from detection models #3752

Closed
3 tasks
datumbox opened this issue Apr 29, 2021 · 2 comments
Closed
3 tasks

Remove _has_warned attribute from detection models #3752

datumbox opened this issue Apr 29, 2021 · 2 comments

Comments

@datumbox
Copy link
Contributor

🚀 Feature

Several detection models make use of an attribute _has_warned which was originally used by JIT to avoid throwing the same warning multiple times:

# used only on torchscript mode
self._has_warned = False

if not self._has_warned:
warnings.warn("RetinaNet always returns a (Losses, Detections) tuple in scripting")
self._has_warned = True

JIT should now be able to handle this automatically without the need of this variable. Thus we should remove the specific variable and confirm that calling forward multiple times on a JIT-scripted model produces a single warning. If that's indeed the case, we can remove it from all places:

@prabhat00155
Copy link
Contributor

If I remove the if check from line 554, I see the warning appear every time forward() is invoked. Here is what I did:

model = retinanet_resnet50_fpn(pretrained=True)
model.eval()
img = torch.rand([1, 3, 512, 512])
model(img)
for p in model.parameters():
    p.requires_grad_(False)
scripted_model = torch.jit.script(model, img)
for _ in range(10):
    img_test = torch.rand([3, 512, 512])
    scripted_model([img_test])

This prints the warning 10 times

@datumbox
Copy link
Contributor Author

datumbox commented May 1, 2021

Thanks for the investigation @prabhat00155!

Sounds like it's not fixed yet on the TorchScript side and can't remove it. I'll be closing the issue and we can revisit on the future once it's patched.

@datumbox datumbox closed this as completed May 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants