Skip to content

Commit

Permalink
Added __repr__ attribute to GeneralizedRCNNTransform (#1834)
Browse files Browse the repository at this point in the history
* feat: Added __repr__ attribute to GeneralizedRCNNTransform

Added more details to default __repr__ attribute for printing.

* fix: Put back relative imports

* style: Fixed pep8 compliance

Switched strings with  syntax to f-strings.

* test: Added test for GeneralizedRCNNTransform __repr__

Checked integrity of __repr__ attribute

* test: Fixed unittest for __repr__

Fixed the formatted strings in the __repr__ integrity check for GeneralizedRCNNTransform

* fix: Fixed f-strings for earlier python versions

Switched back f-strings to .format syntax for Python3.5 compatibility.

* fix: Fixed multi-line string

Fixed multiple-line string syntax for compatibility

* fix: Fixed GeneralizedRCNNTransform unittest

Fixed formatting of min_size argument of the resizing part
  • Loading branch information
frgfm authored Feb 4, 2020
1 parent 2ca3279 commit e2573a7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,25 @@ def test_fasterrcnn_switch_devices(self):
self.assertTrue("scores" in out_cpu[0])
self.assertTrue("labels" in out_cpu[0])

def test_generalizedrcnn_transform_repr(self):

min_size, max_size = 224, 299
image_mean = [0.485, 0.456, 0.406]
image_std = [0.229, 0.224, 0.225]

t = models.detection.transform.GeneralizedRCNNTransform(min_size=min_size,
max_size=max_size,
image_mean=image_mean,
image_std=image_std)

# Check integrity of object __repr__ attribute
expected_string = 'GeneralizedRCNNTransform('
_indent = '\n '
expected_string += '{0}Normalize(mean={1}, std={2})'.format(_indent, image_mean, image_std)
expected_string += '{0}Resize(min_size=({1},), max_size={2}, '.format(_indent, min_size, max_size)
expected_string += "mode='bilinear')\n)"
self.assertEqual(t.__repr__(), expected_string)


for model_name in get_available_classification_models():
# for-loop bodies don't define scopes, so we have to save the variables
Expand Down
9 changes: 9 additions & 0 deletions torchvision/models/detection/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ def postprocess(self, result, image_shapes, original_image_sizes):
result[i]["keypoints"] = keypoints
return result

def __repr__(self):
format_string = self.__class__.__name__ + '('
_indent = '\n '
format_string += "{0}Normalize(mean={1}, std={2})".format(_indent, self.image_mean, self.image_std)
format_string += "{0}Resize(min_size={1}, max_size={2}, mode='bilinear')".format(_indent, self.min_size,
self.max_size)
format_string += '\n)'
return format_string


def resize_keypoints(keypoints, original_size, new_size):
# type: (Tensor, List[int], List[int])
Expand Down

0 comments on commit e2573a7

Please sign in to comment.