Skip to content

Commit

Permalink
Added number of features in FrozenBatchNorm2d __repr__ (#2168) (#2431)
Browse files Browse the repository at this point in the history
Summary:
* feat: Added number of features in FrozenBatchNorm2d repr

While BatchNorm layers have extensive information in their repr, FrozenBatchNorm2d has one

* refactor: Refactored FrozenBatchNorm2d __repr__

* test: Added unittest for FrozenBatchNorm2d __repr__

* style: Removed blank lines in test_ops

* refactor: Avoids creating an extra attribute for __repr__

* style: Switched __repr__ to f-string

Since support of Python version ealier than 3.6 have been dropped, f-string can be used.

* fix: Fixed typo in __repr__

* style: Switched unittest .format to f-string

Pull Request resolved: #2431

Reviewed By: zhangguanheng66

Differential Revision: D22437793

Pulled By: fmassa

fbshipit-source-id: 76fd6a42f0b6cabe47c96fcb5a343fe580a0005e
  • Loading branch information
fmassa authored and facebook-github-bot committed Jul 9, 2020
1 parent ac7604b commit 3e14f2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,5 +530,15 @@ def script_func(x_, offset_, weight_, bias_, stride_, pad_, dilation_):
(x, offset, weight, bias), nondet_tol=1e-5)


class FrozenBNTester(unittest.TestCase):
def test_frozenbatchnorm2d_repr(self):
num_features = 32
t = ops.misc.FrozenBatchNorm2d(num_features)

# Check integrity of object __repr__ attribute
expected_string = f"FrozenBatchNorm2d({num_features})"
self.assertEqual(t.__repr__(), expected_string)


if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions torchvision/ops/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,6 @@ def forward(self, x):
scale = w * rv.rsqrt()
bias = b - rm * scale
return x * scale + bias

def __repr__(self):
return f"{self.__class__.__name__}({self.weight.shape[0]})"

0 comments on commit 3e14f2a

Please sign in to comment.