Skip to content

Commit 9f28cff

Browse files
vfdev-5fmassa
authored andcommitted
Update test_transforms.py (#500)
Improve `test_pad_with_non_constant_padding_modes` to avoid data multiplication in `transforms.ToPILImage()` on float data: ```python img = torch.zeros(3, 27, 27) # Float32 img[:, :, 0] = 1 # we add 1 and not 255 img = transforms.ToPILImage()(img) # This converts 1 to 255 due to [pic = pic.mul(255).byte()](https://github.com/pytorch/vision/blob/master/torchvision/transforms/functional.py#L107) ``` and thus test's correct values are [..., 200, 1, 0]
1 parent 8a4786a commit 9f28cff

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

test/test_transforms.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def test_pad_with_tuple_of_pad_values(self):
245245

246246
def test_pad_with_non_constant_padding_modes(self):
247247
"""Unit tests for edge, reflect, symmetric padding"""
248-
img = torch.zeros(3, 27, 27)
248+
img = torch.zeros(3, 27, 27).byte()
249249
img[:, :, 0] = 1 # Constant value added to leftmost edge
250250
img = transforms.ToPILImage()(img)
251251
img = F.pad(img, 1, (200, 200, 200))
@@ -255,23 +255,23 @@ def test_pad_with_non_constant_padding_modes(self):
255255
# First 6 elements of leftmost edge in the middle of the image, values are in order:
256256
# edge_pad, edge_pad, edge_pad, constant_pad, constant value added to leftmost edge, 0
257257
edge_middle_slice = np.asarray(edge_padded_img).transpose(2, 0, 1)[0][17][:6]
258-
assert np.all(edge_middle_slice == np.asarray([200, 200, 200, 200, 255, 0]))
258+
assert np.all(edge_middle_slice == np.asarray([200, 200, 200, 200, 1, 0]))
259259
assert transforms.ToTensor()(edge_padded_img).size() == (3, 35, 35)
260260

261261
# Pad 3 to left/right, 2 to top/bottom
262262
reflect_padded_img = F.pad(img, (3, 2), padding_mode='reflect')
263263
# First 6 elements of leftmost edge in the middle of the image, values are in order:
264264
# reflect_pad, reflect_pad, reflect_pad, constant_pad, constant value added to leftmost edge, 0
265265
reflect_middle_slice = np.asarray(reflect_padded_img).transpose(2, 0, 1)[0][17][:6]
266-
assert np.all(reflect_middle_slice == np.asarray([0, 0, 255, 200, 255, 0]))
266+
assert np.all(reflect_middle_slice == np.asarray([0, 0, 1, 200, 1, 0]))
267267
assert transforms.ToTensor()(reflect_padded_img).size() == (3, 33, 35)
268268

269269
# Pad 3 to left, 2 to top, 2 to right, 1 to bottom
270270
symmetric_padded_img = F.pad(img, (3, 2, 2, 1), padding_mode='symmetric')
271271
# First 6 elements of leftmost edge in the middle of the image, values are in order:
272272
# sym_pad, sym_pad, sym_pad, constant_pad, constant value added to leftmost edge, 0
273273
symmetric_middle_slice = np.asarray(symmetric_padded_img).transpose(2, 0, 1)[0][17][:6]
274-
assert np.all(symmetric_middle_slice == np.asarray([0, 255, 200, 200, 255, 0]))
274+
assert np.all(symmetric_middle_slice == np.asarray([0, 1, 200, 200, 1, 0]))
275275
assert transforms.ToTensor()(symmetric_padded_img).size() == (3, 32, 34)
276276

277277
def test_pad_raises_with_invalid_pad_sequence_len(self):

0 commit comments

Comments
 (0)