This repository has been archived by the owner on Jul 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 303
Add tile_images #422
Merged
Merged
Add tile_images #422
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aa7706d
add make_grid
yuyu2172 b4267c7
fix pad value
yuyu2172 5e50d7c
Merge remote-tracking branch 'origin/master' into make_grid
yuyu2172 3a29026
add run_module to test
yuyu2172 f42f371
make_grid -> tile_images
yuyu2172 111bdd0
fix alphabetical order
yuyu2172 81543b6
fix doc
yuyu2172 cc11241
fix start
yuyu2172 27515ee
test when pad=0
yuyu2172 2a8df6a
doc
yuyu2172 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.tile_images import tile_images # NOQA | ||
from chainercv.utils.image.write_image import write_image # NOQA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
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): The number of columns in a tile. | ||
pad (int): Amount of pad. The default value 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. | ||
The size of this image is | ||
:math:`((H + pad) \\times \\lceil B / n_{n_{col}} \\rceil, | ||
(W + pad) \\times 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 | ||
start_x = x * (W + pad) + pad // 2 | ||
tile[:, | ||
start_y: start_y + H, | ||
start_x: start_x + W] = imgs[k] | ||
k += 1 | ||
|
||
return tile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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': [0, 1, 2, 3] | ||
})) | ||
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 | ||
start_x_11 = W + self.pad + self.pad // 2 | ||
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__) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)