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
Prev Previous commit
Moving the method to detection utils and adding comments.
  • Loading branch information
datumbox committed Nov 3, 2020
commit 07bc7a086dbc89278e9c1c838ed7ef75f32bd712
2 changes: 1 addition & 1 deletion 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.models._utils import overwrite_eps
from torchvision.models.detection._utils import overwrite_eps


def set_rng_seed(seed):
Expand Down
8 changes: 0 additions & 8 deletions torchvision/models/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from torch import nn
from typing import Dict

from ..ops.misc import FrozenBatchNorm2d


class IntermediateLayerGetter(nn.ModuleDict):
"""
Expand Down Expand Up @@ -67,9 +65,3 @@ 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
21 changes: 20 additions & 1 deletion torchvision/models/detection/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import torch
from torch.jit.annotations import List, Tuple
from torch import Tensor
import torchvision

from torchvision.ops.misc import FrozenBatchNorm2d


class BalancedPositiveNegativeSampler(object):
Expand Down Expand Up @@ -349,3 +350,21 @@ def smooth_l1_loss(input, target, beta: float = 1. / 9, size_average: bool = Tru
if size_average:
return loss.mean()
return loss.sum()


def overwrite_eps(model, eps):
"""
This method overwrites the default eps values of all the
FrozenBatchNorm2d layers of the model with the provided value.
This is necessary to address the BC-breaking change introduced
by the bug-fix at pytorch/vision#2933. The overwrite is applied
only when the pretrained weights are loaded to maintain compatibility
with previous versions.

Arguments:
model (nn.Module): The model on which we perform the overwrite.
eps (float): The new value of eps.
"""
for module in model.modules():
if isinstance(module, FrozenBatchNorm2d):
module.eps = eps
2 changes: 1 addition & 1 deletion torchvision/models/detection/faster_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from torchvision.ops import misc as misc_nn_ops
from torchvision.ops import MultiScaleRoIAlign

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

from .anchor_utils import AnchorGenerator
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/keypoint_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from torchvision.ops import MultiScaleRoIAlign

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

from .faster_rcnn import FasterRCNN
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/mask_rcnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from torchvision.ops import misc as misc_nn_ops
from torchvision.ops import MultiScaleRoIAlign

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

from .faster_rcnn import FasterRCNN
Expand Down
2 changes: 1 addition & 1 deletion torchvision/models/detection/retinanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from torch import Tensor
from torch.jit.annotations import Dict, List, Tuple

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

from . import _utils as det_utils
Expand Down