From 6146639ea3ba7db483ca1c7d8ac135e336d6a5c8 Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Tue, 28 Aug 2018 19:39:59 +0900 Subject: [PATCH 1/8] accept color=None when no checking channels --- chainercv/utils/testing/assertions/assert_is_image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chainercv/utils/testing/assertions/assert_is_image.py b/chainercv/utils/testing/assertions/assert_is_image.py index 9bdc2f348b..4d1de764d1 100644 --- a/chainercv/utils/testing/assertions/assert_is_image.py +++ b/chainercv/utils/testing/assertions/assert_is_image.py @@ -28,7 +28,7 @@ def assert_is_image(img, color=True, check_range=True): if color: assert C == 3, 'The number of channels must be 3.' - else: + elif color is False: assert C == 1, 'The number of channels must be 1.' if check_range: From 5cc111759f3423c4fef0429b6091e0234a5b84dc Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Tue, 28 Aug 2018 19:49:21 +0900 Subject: [PATCH 2/8] assert_is_image in transforms/image functions --- chainercv/transforms/image/center_crop.py | 4 ++++ chainercv/transforms/image/flip.py | 5 +++++ chainercv/transforms/image/pca_lighting.py | 3 +++ chainercv/transforms/image/resize.py | 3 +++ chainercv/transforms/image/resize_contain.py | 2 ++ chainercv/transforms/image/scale.py | 2 ++ chainercv/transforms/image/ten_crop.py | 3 +++ 7 files changed, 22 insertions(+) diff --git a/chainercv/transforms/image/center_crop.py b/chainercv/transforms/image/center_crop.py index 2301993ef8..45856b7e16 100644 --- a/chainercv/transforms/image/center_crop.py +++ b/chainercv/transforms/image/center_crop.py @@ -1,3 +1,6 @@ +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image + + def center_crop(img, size, return_param=False, copy=False): """Center crop an image by `size`. @@ -35,6 +38,7 @@ def center_crop(img, size, return_param=False, copy=False): out_img = img[:, y_slice, x_slice] """ + assert_is_image(img, color=None) _, H, W = img.shape oH, oW = size if oH > H or oW > W: diff --git a/chainercv/transforms/image/flip.py b/chainercv/transforms/image/flip.py index a377999964..0b7b0db56a 100644 --- a/chainercv/transforms/image/flip.py +++ b/chainercv/transforms/image/flip.py @@ -1,3 +1,6 @@ +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image + + def flip(img, y_flip=False, x_flip=False, copy=False): """Flip an image in vertical or horizontal direction as specified. @@ -11,6 +14,8 @@ def flip(img, y_flip=False, x_flip=False, copy=False): Returns: Transformed :obj:`img` in CHW format. """ + + assert_is_image(img, color=None) if y_flip: img = img[:, ::-1, :] if x_flip: diff --git a/chainercv/transforms/image/pca_lighting.py b/chainercv/transforms/image/pca_lighting.py index 5e4d56837a..70f922bcda 100644 --- a/chainercv/transforms/image/pca_lighting.py +++ b/chainercv/transforms/image/pca_lighting.py @@ -1,5 +1,7 @@ import numpy as np +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image + def pca_lighting(img, sigma, eigen_value=None, eigen_vector=None): """AlexNet style color augmentation @@ -30,6 +32,7 @@ def pca_lighting(img, sigma, eigen_value=None, eigen_vector=None): An image in CHW format. """ + assert_is_image(img, color=True) if sigma <= 0: return img diff --git a/chainercv/transforms/image/resize.py b/chainercv/transforms/image/resize.py index c6e695fe53..6e26f4f3e1 100644 --- a/chainercv/transforms/image/resize.py +++ b/chainercv/transforms/image/resize.py @@ -2,6 +2,8 @@ import PIL import warnings +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image + try: import cv2 @@ -68,5 +70,6 @@ def resize(img, size, interpolation=PIL.Image.BILINEAR): ~numpy.ndarray: A resize array in CHW format. """ + assert_is_image(img, color=None) img = _resize(img, size, interpolation) return img diff --git a/chainercv/transforms/image/resize_contain.py b/chainercv/transforms/image/resize_contain.py index 9a5bac9dfe..dd37931550 100644 --- a/chainercv/transforms/image/resize_contain.py +++ b/chainercv/transforms/image/resize_contain.py @@ -3,6 +3,7 @@ import PIL from chainercv.transforms import resize +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image def resize_contain(img, size, fill=0, interpolation=PIL.Image.BILINEAR, @@ -52,6 +53,7 @@ def resize_contain(img, size, fill=0, interpolation=PIL.Image.BILINEAR, :obj:`height, width`. """ + assert_is_image(img, color=None) C, H, W = img.shape out_H, out_W = size scale_h = out_H / H diff --git a/chainercv/transforms/image/scale.py b/chainercv/transforms/image/scale.py index 48e54b09bd..abea333a3d 100644 --- a/chainercv/transforms/image/scale.py +++ b/chainercv/transforms/image/scale.py @@ -1,6 +1,7 @@ import PIL from chainercv.transforms import resize +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image def scale(img, size, fit_short=True, interpolation=PIL.Image.BILINEAR): @@ -30,6 +31,7 @@ def scale(img, size, fit_short=True, interpolation=PIL.Image.BILINEAR): ~numpy.ndarray: A scaled image in CHW format. """ + assert_is_image(img, color=None) _, H, W = img.shape # If resizing is not necessary, return the input as is. diff --git a/chainercv/transforms/image/ten_crop.py b/chainercv/transforms/image/ten_crop.py index eda75bef04..4b7d99b94c 100644 --- a/chainercv/transforms/image/ten_crop.py +++ b/chainercv/transforms/image/ten_crop.py @@ -1,5 +1,7 @@ import numpy as np +from chainercv.utils.testing.assertions.assert_is_image import assert_is_image + def ten_crop(img, size): """Crop 10 regions from an array. @@ -31,6 +33,7 @@ def ten_crop(img, size): The cropped arrays. The shape of tensor is :math:`(10, C, H, W)`. """ + assert_is_image(img, color=None) H, W = size iH, iW = img.shape[1:3] From 835a90b233132c4bb6185e2f2cc93d860d208c54 Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Tue, 28 Aug 2018 19:54:16 +0900 Subject: [PATCH 3/8] assert_is_bbox in transforms/bbox functions --- chainercv/transforms/bbox/crop_bbox.py | 4 +++- chainercv/transforms/bbox/flip_bbox.py | 4 ++++ chainercv/transforms/bbox/resize_bbox.py | 4 ++++ chainercv/transforms/bbox/translate_bbox.py | 5 ++++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/chainercv/transforms/bbox/crop_bbox.py b/chainercv/transforms/bbox/crop_bbox.py index cb17b12fd4..5018b3d449 100644 --- a/chainercv/transforms/bbox/crop_bbox.py +++ b/chainercv/transforms/bbox/crop_bbox.py @@ -1,5 +1,7 @@ import numpy as np +from chainercv.utils.testing.assertions.assert_is_bbox import assert_is_bbox + def crop_bbox( bbox, y_slice=None, x_slice=None, @@ -46,7 +48,7 @@ def crop_bbox( bounding boxes. """ - + assert_is_bbox(bbox) t, b = _slice_to_bounds(y_slice) l, r = _slice_to_bounds(x_slice) crop_bb = np.array((t, l, b, r)) diff --git a/chainercv/transforms/bbox/flip_bbox.py b/chainercv/transforms/bbox/flip_bbox.py index 78a98e6b2b..0f81160b8e 100644 --- a/chainercv/transforms/bbox/flip_bbox.py +++ b/chainercv/transforms/bbox/flip_bbox.py @@ -1,3 +1,6 @@ +from chainercv.utils.testing.assertions.assert_is_bbox import assert_is_bbox + + def flip_bbox(bbox, size, y_flip=False, x_flip=False): """Flip bounding boxes accordingly. @@ -23,6 +26,7 @@ def flip_bbox(bbox, size, y_flip=False, x_flip=False): Bounding boxes flipped according to the given flips. """ + assert_is_bbox(bbox, size) H, W = size bbox = bbox.copy() if y_flip: diff --git a/chainercv/transforms/bbox/resize_bbox.py b/chainercv/transforms/bbox/resize_bbox.py index dd694fa544..c95855bc30 100644 --- a/chainercv/transforms/bbox/resize_bbox.py +++ b/chainercv/transforms/bbox/resize_bbox.py @@ -1,3 +1,6 @@ +from chainercv.utils.testing.assertions.assert_is_bbox import assert_is_bbox + + def resize_bbox(bbox, in_size, out_size): """Resize bounding boxes according to image resize. @@ -21,6 +24,7 @@ def resize_bbox(bbox, in_size, out_size): Bounding boxes rescaled according to the given image shapes. """ + assert_is_bbox(bbox, in_size) bbox = bbox.copy() y_scale = float(out_size[0]) / in_size[0] x_scale = float(out_size[1]) / in_size[1] diff --git a/chainercv/transforms/bbox/translate_bbox.py b/chainercv/transforms/bbox/translate_bbox.py index 6ad5dcf7e9..75431e73d1 100644 --- a/chainercv/transforms/bbox/translate_bbox.py +++ b/chainercv/transforms/bbox/translate_bbox.py @@ -1,3 +1,6 @@ +from chainercv.utils.testing.assertions.assert_is_bbox import assert_is_bbox + + def translate_bbox(bbox, y_offset=0, x_offset=0): """Translate bounding boxes. @@ -24,7 +27,7 @@ def translate_bbox(bbox, y_offset=0, x_offset=0): Bounding boxes translated according to the given offsets. """ - + assert_is_bbox(bbox) out_bbox = bbox.copy() out_bbox[:, :2] += (y_offset, x_offset) out_bbox[:, 2:] += (y_offset, x_offset) From 87983a009d35fdd876e9bd4aefd27a4480da483f Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Tue, 28 Aug 2018 20:03:47 +0900 Subject: [PATCH 4/8] refine transforms_tests/bbox_tests --- tests/transforms_tests/bbox_tests/test_crop_bbox.py | 6 +++--- tests/transforms_tests/bbox_tests/test_flip_bbox.py | 4 ++-- tests/transforms_tests/bbox_tests/test_resize_bbox.py | 4 ++-- tests/transforms_tests/bbox_tests/test_translate_bbox.py | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tests/transforms_tests/bbox_tests/test_crop_bbox.py b/tests/transforms_tests/bbox_tests/test_crop_bbox.py index 0f6230e1c4..8d0cdeed29 100644 --- a/tests/transforms_tests/bbox_tests/test_crop_bbox.py +++ b/tests/transforms_tests/bbox_tests/test_crop_bbox.py @@ -15,7 +15,7 @@ def setUp(self): (0, 5, 3, 6), (1, 2, 3, 4), (3, 3, 4, 6), - )) + ), dtype=np.float32) self.y_slice = slice(1, 5) self.x_slice = slice(0, 4) @@ -25,7 +25,7 @@ def test_crop_bbox(self): (0, 0, 4, 4), (0, 2, 2, 4), (2, 3, 3, 4), - )) + ), dtype=np.float32) out, param = crop_bbox( self.bbox, y_slice=self.y_slice, x_slice=self.x_slice, @@ -38,7 +38,7 @@ def test_crop_bbox_disallow_outside_center(self): (0, 0, 2, 4), (0, 0, 4, 4), (0, 2, 2, 4), - )) + ), dtype=np.float32) out, param = crop_bbox( self.bbox, y_slice=self.y_slice, x_slice=self.x_slice, diff --git a/tests/transforms_tests/bbox_tests/test_flip_bbox.py b/tests/transforms_tests/bbox_tests/test_flip_bbox.py index f0ba60546f..c245eb5c1b 100644 --- a/tests/transforms_tests/bbox_tests/test_flip_bbox.py +++ b/tests/transforms_tests/bbox_tests/test_flip_bbox.py @@ -4,13 +4,13 @@ from chainer import testing from chainercv.transforms import flip_bbox +from chainercv.utils.testing.generate_random_bbox import generate_random_bbox class TestFlipBbox(unittest.TestCase): def test_flip_bbox(self): - bbox = np.random.uniform( - low=0., high=32., size=(10, 4)) + bbox = generate_random_bbox(10, (32, 32), 0, 32) out = flip_bbox(bbox, size=(34, 32), y_flip=True) bbox_expected = bbox.copy() diff --git a/tests/transforms_tests/bbox_tests/test_resize_bbox.py b/tests/transforms_tests/bbox_tests/test_resize_bbox.py index effd847adc..9835068978 100644 --- a/tests/transforms_tests/bbox_tests/test_resize_bbox.py +++ b/tests/transforms_tests/bbox_tests/test_resize_bbox.py @@ -4,13 +4,13 @@ from chainer import testing from chainercv.transforms import resize_bbox +from chainercv.utils.testing.generate_random_bbox import generate_random_bbox class TestResizeBbox(unittest.TestCase): def test_resize_bbox(self): - bbox = np.random.uniform( - low=0., high=32., size=(10, 4)) + bbox = generate_random_bbox(10, (32, 32), 0, 32) out = resize_bbox(bbox, in_size=(32, 32), out_size=(64, 128)) bbox_expected = bbox.copy() diff --git a/tests/transforms_tests/bbox_tests/test_translate_bbox.py b/tests/transforms_tests/bbox_tests/test_translate_bbox.py index ffca0982a8..6ae2d6f168 100644 --- a/tests/transforms_tests/bbox_tests/test_translate_bbox.py +++ b/tests/transforms_tests/bbox_tests/test_translate_bbox.py @@ -4,13 +4,13 @@ from chainer import testing from chainercv.transforms import translate_bbox +from chainercv.utils.testing.generate_random_bbox import generate_random_bbox class TestTranslateBbox(unittest.TestCase): def test_translate_bbox(self): - bbox = np.random.uniform( - low=0., high=32., size=(10, 4)) + bbox = generate_random_bbox(10, (32, 32), 0, 32) out = translate_bbox(bbox, y_offset=5, x_offset=3) bbox_expected = np.empty_like(bbox) From 7d3edf0ea26f42af8357bb61cff33ed3deec9552 Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Sat, 8 Sep 2018 22:32:34 +0900 Subject: [PATCH 5/8] use kwargs for ResBlock in pspnet --- chainercv/experimental/links/model/pspnet/pspnet.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chainercv/experimental/links/model/pspnet/pspnet.py b/chainercv/experimental/links/model/pspnet/pspnet.py index 1b2126b142..7d039b2463 100644 --- a/chainercv/experimental/links/model/pspnet/pspnet.py +++ b/chainercv/experimental/links/model/pspnet/pspnet.py @@ -72,16 +72,16 @@ def __init__(self, n_layer, initialW, bn_kwargs=None): x, ksize=3, stride=2, pad=1) self.res2 = ResBlock( n_block[0], 128, 64, 256, 1, 1, - initialW, bn_kwargs, stride_first=False) + initialW=initialW, bn_kwargs=bn_kwargs, stride_first=False) self.res3 = ResBlock( n_block[1], 256, 128, 512, 2, 1, - initialW, bn_kwargs, stride_first=False) + initialW=initialW, bn_kwargs=bn_kwargs, stride_first=False) self.res4 = ResBlock( n_block[2], 512, 256, 1024, 1, 2, - initialW, bn_kwargs, stride_first=False) + initialW=initialW, bn_kwargs=bn_kwargs, stride_first=False) self.res5 = ResBlock( n_block[3], 1024, 512, 2048, 1, 4, - initialW, bn_kwargs, stride_first=False) + initialW=initialW, bn_kwargs=bn_kwargs, stride_first=False) class PSPNet(chainer.Chain): From 3d842a1bf9e421085a042c4f6768e6da582169ec Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Sat, 8 Sep 2018 22:41:31 +0900 Subject: [PATCH 6/8] fix check_prepare with img in [0, 255] --- .../links_tests/model_tests/fcis_tests/test_fcis.py | 3 ++- .../model_tests/faster_rcnn_tests/test_faster_rcnn.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/experimental_tests/links_tests/model_tests/fcis_tests/test_fcis.py b/tests/experimental_tests/links_tests/model_tests/fcis_tests/test_fcis.py index 3ffbfeb0ab..aaac88b0f3 100644 --- a/tests/experimental_tests/links_tests/model_tests/fcis_tests/test_fcis.py +++ b/tests/experimental_tests/links_tests/model_tests/fcis_tests/test_fcis.py @@ -183,7 +183,8 @@ def setUp(self): ) def check_prepare(self): - x = _random_array(np, self.in_shape) + x = np.random.randint( + 0, 256, size=self.in_shape).astype(np.float32) out = self.link.prepare(x) self.assertIsInstance(out, np.ndarray) self.assertEqual(out.shape, self.expected_shape) diff --git a/tests/links_tests/model_tests/faster_rcnn_tests/test_faster_rcnn.py b/tests/links_tests/model_tests/faster_rcnn_tests/test_faster_rcnn.py index 3e4e103459..f7996b6ba6 100644 --- a/tests/links_tests/model_tests/faster_rcnn_tests/test_faster_rcnn.py +++ b/tests/links_tests/model_tests/faster_rcnn_tests/test_faster_rcnn.py @@ -92,7 +92,8 @@ def setUp(self): ) def check_prepare(self): - x = _random_array(np, self.in_shape) + x = np.random.randint( + 0, 256, size=self.in_shape).astype(np.float32) out = self.link.prepare(x) self.assertIsInstance(out, np.ndarray) self.assertEqual(out.shape, self.expected_shape) From 5a2243e5353b466597fc8d5e4ba165b4ba18dbef Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Sat, 8 Sep 2018 22:56:49 +0900 Subject: [PATCH 7/8] do not assert_is_bbox with in_size in resize_bbox --- chainercv/transforms/bbox/resize_bbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chainercv/transforms/bbox/resize_bbox.py b/chainercv/transforms/bbox/resize_bbox.py index c95855bc30..14162b7ef0 100644 --- a/chainercv/transforms/bbox/resize_bbox.py +++ b/chainercv/transforms/bbox/resize_bbox.py @@ -24,7 +24,7 @@ def resize_bbox(bbox, in_size, out_size): Bounding boxes rescaled according to the given image shapes. """ - assert_is_bbox(bbox, in_size) + assert_is_bbox(bbox) bbox = bbox.copy() y_scale = float(out_size[0]) / in_size[0] x_scale = float(out_size[1]) / in_size[1] From 55fefb049673ee210e061da6910506cb7c669635 Mon Sep 17 00:00:00 2001 From: Shingo Kitagawa Date: Sat, 8 Sep 2018 23:49:22 +0900 Subject: [PATCH 8/8] support cupy array for assert_is_image and assert_is_bbox --- chainercv/utils/testing/assertions/assert_is_bbox.py | 9 +++++---- chainercv/utils/testing/assertions/assert_is_image.py | 6 ++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/chainercv/utils/testing/assertions/assert_is_bbox.py b/chainercv/utils/testing/assertions/assert_is_bbox.py index 165fcc239b..6e00ecdae4 100644 --- a/chainercv/utils/testing/assertions/assert_is_bbox.py +++ b/chainercv/utils/testing/assertions/assert_is_bbox.py @@ -1,4 +1,4 @@ -import numpy as np +from chainer.backends import cuda def assert_is_bbox(bbox, size=None): @@ -16,9 +16,10 @@ def assert_is_bbox(bbox, size=None): Each bounding box should be within the image. """ - assert isinstance(bbox, np.ndarray), \ - 'bbox must be a numpy.ndarray.' - assert bbox.dtype == np.float32, \ + xp = cuda.get_array_module(bbox) + assert isinstance(bbox, xp.ndarray), \ + 'bbox must be a numpy.ndarray or cupy.ndarray.' + assert bbox.dtype == xp.float32, \ 'The type of bbox must be numpy.float32,' assert bbox.shape[1:] == (4,), \ 'The shape of bbox must be (*, 4).' diff --git a/chainercv/utils/testing/assertions/assert_is_image.py b/chainercv/utils/testing/assertions/assert_is_image.py index 4d1de764d1..47818f8fa4 100644 --- a/chainercv/utils/testing/assertions/assert_is_image.py +++ b/chainercv/utils/testing/assertions/assert_is_image.py @@ -1,4 +1,4 @@ -import numpy as np +from chainer.backends import cuda def assert_is_image(img, color=True, check_range=True): @@ -22,7 +22,9 @@ def assert_is_image(img, color=True, check_range=True): """ - assert isinstance(img, np.ndarray), 'img must be a numpy.ndarray.' + xp = cuda.get_array_module(img) + assert isinstance(img, xp.ndarray), \ + 'img must be a numpy.ndarray or cupy.ndarray.' assert len(img.shape) == 3, 'img must be a 3-dimensional array.' C, H, W = img.shape