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

Add tile_images #422

Merged
merged 10 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions chainercv/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from chainercv.utils.download import extractall # NOQA
from chainercv.utils.image import read_image # NOQA
from chainercv.utils.image import write_image # NOQA
from chainercv.utils.image.tile_images import tile_images # NOQA
Copy link
Member

Choose a reason for hiding this comment

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

from chainercv.utils.image import tile_images

Copy link
Member

Choose a reason for hiding this comment

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

tile_images comes before write_image in alphabetical order.

from chainercv.utils.iterator import apply_prediction_to_iterator # NOQA
from chainercv.utils.iterator import unzip # NOQA
from chainercv.utils.testing import assert_is_bbox # NOQA
Expand Down
1 change: 1 addition & 0 deletions chainercv/utils/image/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from chainercv.utils.image.read_image import read_image # NOQA
from chainercv.utils.image.write_image import write_image # NOQA
from chainercv.utils.image.tile_images import tile_images # NOQA
46 changes: 46 additions & 0 deletions chainercv/utils/image/tile_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from __future__ import division

import math
import numpy as np


def tile_images(imgs, n_col, pad=2, fill=0):
"""Make a tile of images

Args:
imgs (numpy.ndarray): A batch of images whose shape is BCHW.
n_col (int): Number of columns in a tile.
Copy link
Member

Choose a reason for hiding this comment

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

The number? (I found both Number of and The number of in our code)

pad (int): Amount of pad. Default is 2.
fill (float, tuple or ~numpy.ndarray): The value of padded pixels.
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`.

Returns:
~numpy.ndarray:
An image array in CHW format.
Copy link
Member

Choose a reason for hiding this comment

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

How about adding a note about the output size?
The size of this image is :math:((H + pad) * \lceil B / n_col \rceil, (W + pad) * n_col)


"""
B, C, H, W = imgs.shape
n_col = min(n_col, B)
n_row = int(math.ceil(B / n_col))

shape = (C,
(H + pad) * n_row,
(W + pad) * n_col)
tile = np.empty(shape, dtype=imgs.dtype)
tile[:] = np.array(fill).reshape((-1, 1, 1))

k = 0
for y in range(n_row):
for x in range(n_col):
if k >= B:
break
start_y = y * (H + pad) + pad // 2 + 1
Copy link
Member

Choose a reason for hiding this comment

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

Why do you add 1? In the case that pad == 0, the top-left corner of the first image will be (1, 1) although it should be (0, 0).

start_x = x * (W + pad) + pad // 2 + 1
tile[:,
start_y: start_y + H,
start_x: start_x + W] = imgs[k]
k += 1

return tile
4 changes: 4 additions & 0 deletions docs/source/reference/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ write_image
~~~~~~~~~~~
.. autofunction:: write_image

tile_images
~~~~~~~~~~~
.. autofunction:: tile_images


Iterator Utilities
------------------
Expand Down
37 changes: 37 additions & 0 deletions tests/utils_tests/image_tests/test_tile_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from __future__ import division
import math
import numpy as np
import unittest

from chainer import testing

from chainercv.utils import tile_images


@testing.parameterize(*testing.product({
'fill': [128, (104, 117, 123), np.random.uniform(255, size=(3, 1, 1))],
'pad': [1, 2, 3]
Copy link
Member

Choose a reason for hiding this comment

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

How about testing the case pad = 0? With this test, this bug can be found.

}))
class TestTileImages(unittest.TestCase):

def test_tile_images(self):
B = np.random.randint(10, 20)
n_col = np.random.randint(2, 5)
H = 30
W = 40

imgs = np.random.uniform(255, size=(B, 3, H, W))
tile = tile_images(imgs, n_col, self.pad, fill=self.fill)

n_row = int(math.ceil(B / n_col))
self.assertTrue(n_col >= 1 and n_row >= 1)
start_y_11 = H + self.pad + self.pad // 2 + 1
start_x_11 = W + self.pad + self.pad // 2 + 1
tile_11 = tile[:,
start_y_11:start_y_11 + H,
start_x_11:start_x_11 + W]

np.testing.assert_equal(tile_11, imgs[(n_col - 1) + 2])


testing.run_module(__name__, __file__)