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

Commit

Permalink
change transforms/image to work with yx order
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyu2172 committed Jun 1, 2017
1 parent 5ea1763 commit 6055ed9
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 56 deletions.
12 changes: 6 additions & 6 deletions chainercv/transforms/image/center_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def center_crop(img, size, return_param=False, copy=False):
contents are listed below with key, value-type and the description
of the value.
* **x_slice** (*slice*): A slice used to crop the input image.\
The relation below holds together with :obj:`y_slice`.
* **y_slice** (*slice*): Similar to :obj:`x_slice`.
* **y_slice** (*slice*): A slice used to crop the input image.\
The relation below holds together with :obj:`x_slice`.
* **x_slice** (*slice*): Similar to :obj:`y_slice`.
.. code::
Expand All @@ -40,18 +40,18 @@ def center_crop(img, size, return_param=False, copy=False):
if oW > W or oH > H:
raise ValueError('shape of image needs to be larger than size')

x_offset = int(round((W - oW) / 2.))
y_offset = int(round((H - oH) / 2.))
x_offset = int(round((W - oW) / 2.))

x_slice = slice(x_offset, x_offset + oW)
y_slice = slice(y_offset, y_offset + oH)
x_slice = slice(x_offset, x_offset + oW)

img = img[:, y_slice, x_slice]

if copy:
img = img.copy()

if return_param:
return img, {'x_slice': x_slice, 'y_slice': y_slice}
return img, {'y_slice': y_slice, 'x_slice': x_slice}
else:
return img
8 changes: 4 additions & 4 deletions chainercv/transforms/image/flip.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
def flip(img, x_flip=False, y_flip=False, copy=False):
def flip(img, y_flip=False, x_flip=False, copy=False):
"""Flip an image in vertical or horizontal direction as specified.
Args:
img (~numpy.ndarray): An array that gets flipped. This is in CHW
format.
x_flip (bool): Flip in horizontal direction.
y_flip (bool): Flip in vertical direction.
x_flip (bool): Flip in horizontal direction.
copy (bool): If False, a view of :obj:`img` will be returned.
Returns:
Transformed :obj:`img` in CHW format.
"""
if x_flip:
img = img[:, :, ::-1]
if y_flip:
img = img[:, ::-1, :]
if x_flip:
img = img[:, :, ::-1]

if copy:
img = img.copy()
Expand Down
24 changes: 12 additions & 12 deletions chainercv/transforms/image/random_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def random_crop(img, size, return_param=False, copy=False):
contents are listed below with key, value-type and the description
of the value.
* **x_slice** (*slice*): A slice used to crop the input image.\
The relation below holds together with :obj:`y_slice`.
* **y_slice** (*slice*): Similar to :obj:`x_slice`.
* **y_slice** (*slice*): A slice used to crop the input image.\
The relation below holds together with :obj:`x_slice`.
* **x_slice** (*slice*): Similar to :obj:`x_slice`.
.. code::
Expand All @@ -41,14 +41,6 @@ def random_crop(img, size, return_param=False, copy=False):
"""
H, W = size

if img.shape[2] == W:
x_offset = 0
elif img.shape[2] > W:
x_offset = random.choice(six.moves.range(img.shape[2] - W))
else:
raise ValueError('shape of image needs to be larger than output shape')
x_slice = slice(x_offset, x_offset + W)

if img.shape[1] == H:
y_offset = 0
elif img.shape[1] > H:
Expand All @@ -57,12 +49,20 @@ def random_crop(img, size, return_param=False, copy=False):
raise ValueError('shape of image needs to be larger than output shape')
y_slice = slice(y_offset, y_offset + H)

if img.shape[2] == W:
x_offset = 0
elif img.shape[2] > W:
x_offset = random.choice(six.moves.range(img.shape[2] - W))
else:
raise ValueError('shape of image needs to be larger than output shape')
x_slice = slice(x_offset, x_offset + W)

img = img[:, y_slice, x_slice]

if copy:
img = img.copy()

if return_param:
return img, {'x_slice': x_slice, 'y_slice': y_slice}
return img, {'y_slice': y_slice, 'x_slice': x_slice}
else:
return img
10 changes: 5 additions & 5 deletions chainercv/transforms/image/random_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ def random_expand(img, max_ratio=4, fill=0, return_param=False):
of the value.
* **ratio** (*float*): The sampled value used to make the canvas.
* **x_offset** (*int*): The x coordinate of the top left corner\
of the image after placing on the canvas.
* **y_offset** (*int*): The y coodinate of the top left corner of\
the image after placing on the canvas.
* **x_offset** (*int*): The x coordinate of the top left corner\
of the image after placing on the canvas.
"""

if max_ratio <= 1:
if return_param:
return img, {'ratio': 1, 'x_offset': 0, 'y_offset': 0}
return img, {'ratio': 1, 'y_offset': 0, 'x_offset': 0}
else:
return img

Expand All @@ -57,15 +57,15 @@ def random_expand(img, max_ratio=4, fill=0, return_param=False):
ratio = random.uniform(1, max_ratio)
out_H, out_W = int(H * ratio), int(W * ratio)

x_offset = random.randint(0, out_W - W)
y_offset = random.randint(0, out_H - H)
x_offset = random.randint(0, out_W - W)

out_img = np.empty((C, out_H, out_W), dtype=img.dtype)
out_img[:] = np.array(fill).reshape(-1, 1, 1)
out_img[:, y_offset:y_offset + H, x_offset:x_offset + W] = img

if return_param:
param = {'ratio': ratio, 'x_offset': x_offset, 'y_offset': y_offset}
param = {'ratio': ratio, 'y_offset': y_offset, 'x_offset': x_offset}
return out_img, param
else:
return out_img
18 changes: 9 additions & 9 deletions chainercv/transforms/image/random_flip.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import random


def random_flip(img, x_random=False, y_random=False,
def random_flip(img, y_random=False, x_random=False,
return_param=False, copy=False):
"""Randomly flip an image in vertical or horizontal direction.
Args:
img (~numpy.ndarray): An array that gets flipped. This is in
CHW format.
x_random (bool): Randomly flip in horizontal direction.
y_random (bool): Randomly flip in vertical direction.
x_random (bool): Randomly flip in horizontal direction.
return_param (bool): Returns information of flip.
copy (bool): If False, a view of :obj:`img` will be returned.
Expand All @@ -25,27 +25,27 @@ def random_flip(img, x_random=False, y_random=False,
contents are listed below with key, value-type and the description
of the value.
* **x_flip** (*bool*): Whether the image was flipped in the\
horizontal direction or not.
* **y_flip** (*bool*): Whether the image was flipped in the\
vertical direction or not.
* **x_flip** (*bool*): Whether the image was flipped in the\
horizontal direction or not.
"""
x_flip, y_flip = False, False
if x_random:
x_flip = random.choice([True, False])
if y_random:
y_flip = random.choice([True, False])
if x_random:
x_flip = random.choice([True, False])

if x_flip:
img = img[:, :, ::-1]
if y_flip:
img = img[:, ::-1, :]
if x_flip:
img = img[:, :, ::-1]

if copy:
img = img.copy()

if return_param:
return img, {'x_flip': x_flip, 'y_flip': y_flip}
return img, {'y_flip': y_flip, 'x_flip': x_flip}
else:
return img
10 changes: 5 additions & 5 deletions chainercv/transforms/image/resize_contain.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def resize_contain(img, size, fill=0, return_param=False):
contents are listed below with key, value-type and the description
of the value.
* **x_offset** (*int*): The x coordinate of the top left corner\
of the image after placing on the canvas.
* **y_offset** (*int*): The y coodinate of the top left corner of\
the image after placing on the canvas.
* **x_offset** (*int*): The x coordinate of the top left corner\
of the image after placing on the canvas.
* **scaled_size** (*tuple*): The size to which the image is scaled\
to before placing it on a canvas. This is a tuple of two elements:\
:obj:`height, width`.
Expand All @@ -50,13 +50,13 @@ def resize_contain(img, size, fill=0, return_param=False):
scaled_size = (int(H * scale), int(W * scale))
if scale < 1.:
img = resize(img, scaled_size)
x_slice, y_slice = _get_pad_slice(img, size=size)
y_slice, x_slice = _get_pad_slice(img, size=size)
out_img = np.empty((C, out_H, out_W), dtype=img.dtype)
out_img[:] = np.array(fill).reshape(-1, 1, 1)
out_img[:, y_slice, x_slice] = img

if return_param:
param = {'x_offset': x_slice.start, 'y_offset': y_slice.start,
param = {'y_offset': y_slice.start, 'x_offset': x_slice.start,
'scaled_size': scaled_size}
return out_img, param
else:
Expand Down Expand Up @@ -92,4 +92,4 @@ def _get_pad_slice(img, size):
else:
x_slice = slice(0, int(size[1]))

return x_slice, y_slice
return y_slice, x_slice
2 changes: 1 addition & 1 deletion tests/transforms_tests/image_tests/test_center_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_center_crop(self):
img = np.random.uniform(size=(3, 48, 32))

out, param = center_crop(img, (24, 16), return_param=True)
x_slice = param['x_slice']
y_slice = param['y_slice']
x_slice = param['x_slice']

np.testing.assert_equal(out, img[:, y_slice, x_slice])
self.assertEqual(x_slice, slice(8, 24))
Expand Down
4 changes: 2 additions & 2 deletions tests/transforms_tests/image_tests/test_flip_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestRandomFlip(unittest.TestCase):
def test_random_flip(self):
img = np.random.uniform(size=(3, 24, 24))

out = flip(img, x_flip=True, y_flip=True)
out = flip(img, y_flip=True, x_flip=True)

expected = img
expected = expected[:, :, ::-1]
Expand All @@ -21,7 +21,7 @@ def test_random_flip(self):
def test_random_flip_vertical(self):
img = np.random.uniform(size=(3, 24, 24))

out = flip(img, x_flip=False, y_flip=True)
out = flip(img, y_flip=True, x_flip=False)

expected = img
expected = expected[:, ::-1, :]
Expand Down
4 changes: 2 additions & 2 deletions tests/transforms_tests/image_tests/test_random_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def test_random_crop(self):
img = np.random.uniform(size=(3, 48, 32))

out, param = random_crop(img, (48, 32), return_param=True)
x_slice = param['x_slice']
y_slice = param['y_slice']
x_slice = param['x_slice']
np.testing.assert_equal(out, img)
self.assertEqual(x_slice, slice(0, 32))
self.assertEqual(y_slice, slice(0, 48))
self.assertEqual(x_slice, slice(0, 32))

out = random_crop(img, (24, 12))
self.assertEqual(out.shape[1:], (24, 12))
Expand Down
6 changes: 3 additions & 3 deletions tests/transforms_tests/image_tests/test_random_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def test_random_expand(self):
out, param = random_expand(
img, max_ratio=self.max_ratio, return_param=True)
ratio = param['ratio']
x_offset = param['x_offset']
y_offset = param['y_offset']
x_offset = param['x_offset']
np.testing.assert_equal(
out[:, y_offset:y_offset + 64, x_offset:x_offset + 32], img)
self.assertGreaterEqual(ratio, 1)
Expand All @@ -47,9 +47,9 @@ def test_random_expand_fill(self):

while True:
out, param = random_expand(img, fill=self.fill, return_param=True)
x_offset = param['x_offset']
y_offset = param['y_offset']
if x_offset > 0 or y_offset > 0:
x_offset = param['x_offset']
if y_offset > 0 or x_offset > 0:
break

if isinstance(self.fill, int):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ def test_random_flip(self):
img = np.random.uniform(size=(3, 24, 24))

out, param = random_flip(
img, x_random=True, y_random=True, return_param=True)
x_flip = param['x_flip']
img, y_random=True, x_random=True, return_param=True)
y_flip = param['y_flip']
x_flip = param['x_flip']

expected = img
if x_flip:
expected = expected[:, :, ::-1]
if y_flip:
expected = expected[:, ::-1, :]
if x_flip:
expected = expected[:, :, ::-1]
np.testing.assert_equal(out, expected)


Expand Down
6 changes: 3 additions & 3 deletions tests/transforms_tests/image_tests/test_resize_contain.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ def test_resize_contain(self):
np.testing.assert_array_equal(img, out[:, 8:40, 16:80])
np.testing.assert_array_equal(self.fill, out[:, 0, 0])
self.assertEqual(param['scaled_size'], (32, 64))
self.assertEqual(param['x_offset'], 16)
self.assertEqual(param['y_offset'], 8)
self.assertEqual(param['x_offset'], 16)

def test_resize_contain_canvas_small_x(self):
img = np.random.uniform(size=(3, 32, 64))

out, param = resize_contain(
img, (16, 68), fill=self.fill, return_param=True)
self.assertEqual(param['scaled_size'], (16, 32))
self.assertEqual(param['x_offset'], 18)
self.assertEqual(param['y_offset'], 0)
self.assertEqual(param['x_offset'], 18)

def test_resize_contain_canvas_small_y(self):
img = np.random.uniform(size=(3, 32, 64))

out, param = resize_contain(
img, (24, 16), fill=self.fill, return_param=True)
self.assertEqual(param['scaled_size'], (8, 16))
self.assertEqual(param['x_offset'], 0)
self.assertEqual(param['y_offset'], 8)
self.assertEqual(param['x_offset'], 0)


testing.run_module(__name__, __file__)

0 comments on commit 6055ed9

Please sign in to comment.