Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Support (C, 1, 1) for fill argument #306

Merged
merged 4 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions chainercv/transforms/image/random_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ def random_expand(img, max_ratio=4, fill=0, return_param=False):
paper, this value is 4.
fill (float, tuple or ~numpy.ndarray): The value of padded pixels.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float or ~numpy.ndarray

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think supporting tuple is convenient. Why do you want to drop it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me summarize.

fill can be one of float, tuple or (C, 1, 1).
mean is (C, 1, 1).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill can be one of float, tuple or (C, 1, 1).
mean is (C, 1, 1).

Yes. And I want to pass a tuple value that is never broadcasted to an image.

In the original paper, this value is the mean of ImageNet.
If it is :class:`numpy.ndarray`,
its shape should be :math:`(C, 1, 1)`,
where :math:`C` is the number of channels of :obj:`img`.
return_param (bool): Returns random parameters.

Returns:
Expand Down
3 changes: 3 additions & 0 deletions chainercv/transforms/image/resize_contain.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def resize_contain(img, size, fill=0, return_param=False):
size (tuple of two ints): A tuple of two elements:
:obj:`height, width`. The size of the image after resizing.
fill (float, tuple or ~numpy.ndarray): The value of padded pixels.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

If it is :class:`numpy.ndarray`,
its shape should be :math:`(C, 1, 1)`,
where :math:`C` is the number of channels of :obj:`img`.
return_param (bool): Returns information of resizing and offsetting.

Returns:
Expand Down
10 changes: 5 additions & 5 deletions tests/transforms_tests/image_tests/test_random_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_random_expand(self):
@testing.parameterize(
{'fill': 128},
{'fill': (104, 117, 123)},
{'fill': np.random.uniform(255, size=3)},
{'fill': np.random.uniform(255, size=(3, 1, 1))},
)
class TestRandomExpandFill(unittest.TestCase):

Expand All @@ -53,11 +53,11 @@ def test_random_expand_fill(self):
break

if isinstance(self.fill, int):
np.testing.assert_equal(
out[:, 0, 0], (self.fill,) * 3)
fill = (self.fill,) * 3
else:
np.testing.assert_equal(
out[:, 0, 0], self.fill)
fill = self.fill
np.testing.assert_equal(
out[:, 0, 0], np.array(fill).flatten())


testing.run_module(__name__, __file__)
11 changes: 9 additions & 2 deletions tests/transforms_tests/image_tests/test_resize_contain.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@testing.parameterize(
{'fill': 128},
{'fill': (104, 117, 123)},
{'fill': np.random.uniform(255, size=3)},
{'fill': np.random.uniform(255, size=(3, 1, 1))},
)
class TestResizeContain(unittest.TestCase):

Expand All @@ -20,7 +20,14 @@ def test_resize_contain(self):
img, (48, 96), fill=self.fill, return_param=True)

np.testing.assert_array_equal(img, out[:, 8:40, 16:80])
np.testing.assert_array_equal(self.fill, out[:, 0, 0])

if isinstance(self.fill, int):
fill = (self.fill,) * 3
else:
fill = self.fill
np.testing.assert_array_equal(
out[:, 0, 0], np.array(fill).flatten())

self.assertEqual(param['scaled_size'], (32, 64))
self.assertEqual(param['y_offset'], 8)
self.assertEqual(param['x_offset'], 16)
Expand Down