Skip to content

Commit e2573a7

Browse files
authored
Added __repr__ attribute to GeneralizedRCNNTransform (#1834)
* 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
1 parent 2ca3279 commit e2573a7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

test/test_models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,25 @@ def test_fasterrcnn_switch_devices(self):
248248
self.assertTrue("scores" in out_cpu[0])
249249
self.assertTrue("labels" in out_cpu[0])
250250

251+
def test_generalizedrcnn_transform_repr(self):
252+
253+
min_size, max_size = 224, 299
254+
image_mean = [0.485, 0.456, 0.406]
255+
image_std = [0.229, 0.224, 0.225]
256+
257+
t = models.detection.transform.GeneralizedRCNNTransform(min_size=min_size,
258+
max_size=max_size,
259+
image_mean=image_mean,
260+
image_std=image_std)
261+
262+
# Check integrity of object __repr__ attribute
263+
expected_string = 'GeneralizedRCNNTransform('
264+
_indent = '\n '
265+
expected_string += '{0}Normalize(mean={1}, std={2})'.format(_indent, image_mean, image_std)
266+
expected_string += '{0}Resize(min_size=({1},), max_size={2}, '.format(_indent, min_size, max_size)
267+
expected_string += "mode='bilinear')\n)"
268+
self.assertEqual(t.__repr__(), expected_string)
269+
251270

252271
for model_name in get_available_classification_models():
253272
# for-loop bodies don't define scopes, so we have to save the variables

torchvision/models/detection/transform.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ def postprocess(self, result, image_shapes, original_image_sizes):
181181
result[i]["keypoints"] = keypoints
182182
return result
183183

184+
def __repr__(self):
185+
format_string = self.__class__.__name__ + '('
186+
_indent = '\n '
187+
format_string += "{0}Normalize(mean={1}, std={2})".format(_indent, self.image_mean, self.image_std)
188+
format_string += "{0}Resize(min_size={1}, max_size={2}, mode='bilinear')".format(_indent, self.min_size,
189+
self.max_size)
190+
format_string += '\n)'
191+
return format_string
192+
184193

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

0 commit comments

Comments
 (0)