MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / stack_patches

Function stack_patches

tensorpack/utils/viz.py:157–203  ·  view source on GitHub ↗

Stacked patches into grid, to produce visualizations like the following: .. image:: https://github.com/tensorpack/tensorpack/raw/master/examples/GAN/demo/BEGAN-CelebA-samples.jpg Args: patch_list(list[ndarray] or ndarray): NHW or NHWC images in [0,255]. nr_row(int), nr

(
        patch_list, nr_row, nr_col, border=None,
        pad=False, bgcolor=255, viz=False, lclick_cb=None)

Source from the content-addressed store, hash-verified

155
156
157def stack_patches(
158 patch_list, nr_row, nr_col, border=None,
159 pad=False, bgcolor=255, viz=False, lclick_cb=None):
160 """
161 Stacked patches into grid, to produce visualizations like the following:
162
163 .. image:: https://github.com/tensorpack/tensorpack/raw/master/examples/GAN/demo/BEGAN-CelebA-samples.jpg
164
165 Args:
166 patch_list(list[ndarray] or ndarray): NHW or NHWC images in [0,255].
167 nr_row(int), nr_col(int): rows and cols of the grid.
168 ``nr_col * nr_row`` must be no less than ``len(patch_list)``.
169 border(int): border length between images.
170 Defaults to ``0.05 * min(patch_width, patch_height)``.
171 pad (boolean): when `patch_list` is a list, pad all patches to the maximum height and width.
172 This option allows stacking patches of different shapes together.
173 bgcolor(int or 3-tuple): background color in [0, 255]. Either an int
174 or a BGR tuple.
175 viz(bool): whether to use :func:`interactive_imshow` to visualize the results.
176 lclick_cb: A callback function ``f(patch, patch index in patch_list)``
177 to get called when a patch get clicked in imshow.
178
179 Returns:
180 np.ndarray: the stacked image.
181 """
182 if pad:
183 patch_list = _pad_patch_list(patch_list, bgcolor)
184 patch_list = _preprocess_patch_list(patch_list)
185
186 if lclick_cb is not None:
187 viz = True
188 ph, pw = patch_list.shape[1:3]
189
190 canvas = Canvas(ph, pw, nr_row, nr_col,
191 patch_list.shape[-1], border, bgcolor)
192
193 if lclick_cb is not None:
194 def lclick_callback(img, x, y):
195 idx = canvas.get_patchid_from_coord(x, y)
196 lclick_cb(patch_list[idx], idx)
197 else:
198 lclick_callback = None
199
200 canvas.draw_patches(patch_list)
201 if viz:
202 interactive_imshow(canvas.canvas, lclick_cb=lclick_callback)
203 return canvas.canvas
204
205
206def gen_stack_patches(patch_list,

Callers 5

sampleFunction · 0.90
sampleFunction · 0.90
sampleFunction · 0.90
dump_dataflow_imagesFunction · 0.85
viz.pyFile · 0.85

Calls 5

draw_patchesMethod · 0.95
_pad_patch_listFunction · 0.85
_preprocess_patch_listFunction · 0.85
CanvasClass · 0.85
interactive_imshowFunction · 0.85

Tested by

no test coverage detected