(
        tensor: megengine.Tensor,  # [N,C,H,W]
        nrow: int = 8,
        padding: int = 2,
        background: float = 0,
        normalize: bool = False,
)
return grid
align [N, C, H, W] image tensor to [H, W, 3] image grids, for visualization
align [N, C, H, W] image tensor to [H, W, 3] image grids, for visualization
align [ N C H W ] image tensor to [ H W 3 ] image grids for visualization
def make_grid(
        tensor: megengine.Tensor,  # [N,C,H,W]
        nrow: int = 8,
        padding: int = 2,
        background: float = 0,
        normalize: bool = False,
) -> megengine.Tensor:
    """align [N, C, H, W] image tensor to [H, W, 3] image grids, for visualization"""
    if normalize:
        tensor = normalize_image(tensor, scale=255)  # normalize to 0-255 scale

    c = tensor.shape[1]
    assert c in (1, 3), "only support color/grayscale images, got channel = {}".format(c)
    nmaps = tensor.shape[0]
    xmaps = min(nrow, nmaps)
    ymaps = int(math.ceil(float(nmaps) / xmaps))
    height, width = int(tensor.shape[2] + padding), int(tensor.shape[3] + padding)
    num_channels = tensor.shape[1]
    grid = megengine.ones((num_channels, height * ymaps + padding, width * xmaps + padding), "float32") * background
    k = 0
    for y in range(ymaps):
        for x in range(xmaps):
            if k >= nmaps:
                break
            grid = grid.set_subtensor(tensor[k])[:,
                                                 y * height + padding: (y + 1) * height,
                                                 x * width + padding: (x + 1) * width]
            k = k + 1
    c, h, w = grid.shape
    grid = grid.dimshuffle(1, 2, 0)  # [C,H,W] -> [H,W,C]
    grid = grid.broadcast(h, w, 3)   # [H,W,C] -> [H,W,3]
    return grid
def make_grid ( tensor : megengine . Tensor , # [N,C,H,W] nrow : int = 8 , padding : int = 2 , background : float = 0 , normalize : bool = False , ) -> megengine . Tensor : if normalize : tensor = normalize_image ( tensor , scale = 255 ) # normalize to 0-255 scale c = tensor . shape [ 1 ] assert c in ( 1 , 3 ) , "only support color/grayscale images, got channel = {}" . format ( c ) nmaps = tensor . shape [ 0 ] xmaps = min ( nrow , nmaps ) ymaps = int ( math . ceil ( float ( nmaps ) / xmaps ) ) height , width = int ( tensor . shape [ 2 ] + padding ) , int ( tensor . shape [ 3 ] + padding ) num_channels = tensor . shape [ 1 ] grid = megengine . ones ( ( num_channels , height * ymaps + padding , width * xmaps + padding ) , "float32" ) * background k = 0 for y in range ( ymaps ) : for x in range ( xmaps ) : if k >= nmaps : break grid = grid . set_subtensor ( tensor [ k ] ) [ : , y * height + padding : ( y + 1 ) * height , x * width + padding : ( x + 1 ) * width ] k = k + 1 c , h , w = grid . shape grid = grid . dimshuffle ( 1 , 2 , 0 ) # [C,H,W] -> [H,W,C] grid = grid . broadcast ( h , w , 3 ) # [H,W,C] -> [H,W,3] return grid