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

Overwriting FrozenBN eps=0.0 if pretrained=True for detection models. #2940

Merged
merged 2 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import unittest
import random

from torchvision.ops.misc import FrozenBatchNorm2d
from torchvision.models._utils import overwrite_eps


def set_rng_seed(seed):
Expand Down Expand Up @@ -151,9 +151,7 @@ def _test_detection_model(self, name, dev):
kwargs["score_thresh"] = 0.013
model = models.detection.__dict__[name](num_classes=50, pretrained_backbone=False, **kwargs)
if "keypointrcnn" in name or "retinanet" in name:
for module in model.modules():
if isinstance(module, FrozenBatchNorm2d):
module.eps = 0
overwrite_eps(model, 0.0)
model.eval().to(device=dev)
input_shape = (3, 300, 300)
# RNG always on CPU, to ensure x in cuda tests is bitwise identical to x in cpu tests
Expand Down
8 changes: 8 additions & 0 deletions torchvision/models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from torch import nn
from typing import Dict

from ..ops.misc import FrozenBatchNorm2d


class IntermediateLayerGetter(nn.ModuleDict):
"""
Expand Down Expand Up @@ -65,3 +67,9 @@ def forward(self, x):
out_name = self.return_layers[name]
out[out_name] = x
return out


def overwrite_eps(model, eps):
for module in model.modules():
if isinstance(module, FrozenBatchNorm2d):
module.eps = eps
datumbox marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions torchvision/models/detection/faster_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from torchvision.ops import misc as misc_nn_ops
from torchvision.ops import MultiScaleRoIAlign

from .._utils import overwrite_eps
from ..utils import load_state_dict_from_url

from .anchor_utils import AnchorGenerator
Expand Down Expand Up @@ -361,4 +362,5 @@ def fasterrcnn_resnet50_fpn(pretrained=False, progress=True,
state_dict = load_state_dict_from_url(model_urls['fasterrcnn_resnet50_fpn_coco'],
progress=progress)
model.load_state_dict(state_dict)
overwrite_eps(model, 0.0)
datumbox marked this conversation as resolved.
Show resolved Hide resolved
return model
2 changes: 2 additions & 0 deletions torchvision/models/detection/keypoint_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from torchvision.ops import MultiScaleRoIAlign

from .._utils import overwrite_eps
from ..utils import load_state_dict_from_url

from .faster_rcnn import FasterRCNN
Expand Down Expand Up @@ -332,4 +333,5 @@ def keypointrcnn_resnet50_fpn(pretrained=False, progress=True,
state_dict = load_state_dict_from_url(model_urls[key],
progress=progress)
model.load_state_dict(state_dict)
overwrite_eps(model, 0.0)
return model
2 changes: 2 additions & 0 deletions torchvision/models/detection/mask_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from torchvision.ops import misc as misc_nn_ops
from torchvision.ops import MultiScaleRoIAlign

from .._utils import overwrite_eps
from ..utils import load_state_dict_from_url

from .faster_rcnn import FasterRCNN
Expand Down Expand Up @@ -328,4 +329,5 @@ def maskrcnn_resnet50_fpn(pretrained=False, progress=True,
state_dict = load_state_dict_from_url(model_urls['maskrcnn_resnet50_fpn_coco'],
progress=progress)
model.load_state_dict(state_dict)
overwrite_eps(model, 0.0)
return model
2 changes: 2 additions & 0 deletions torchvision/models/detection/retinanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from torch import Tensor
from torch.jit.annotations import Dict, List, Tuple

from .._utils import overwrite_eps
from ..utils import load_state_dict_from_url

from . import _utils as det_utils
Expand Down Expand Up @@ -628,4 +629,5 @@ def retinanet_resnet50_fpn(pretrained=False, progress=True,
state_dict = load_state_dict_from_url(model_urls['retinanet_resnet50_fpn_coco'],
progress=progress)
model.load_state_dict(state_dict)
overwrite_eps(model, 0.0)
return model