def demo_image(writer, x, n_iter): """ Show how to add_image() to add image to tensorboard for NNabla. :param writer: nnabla_tensorboard summary writer. :param x: NNabla variable tensor with the format [B, C, H, W]. :param n_iter: global_step for tensorboard. :return: nothing. """ tiled = tile_images(x.d) # return numpy.ndarray writer.add_image('Image', tiled, n_iter, dataformats='HWC')
def get_tiled_image(img, channel_last=False): assert len(img.shape) == 4 assert isinstance(img, np.ndarray) if channel_last: # nnabla.monitor.tile_images requests (B, C, H, W) # (B, H, W, C) -> (B, C, H, W) img = img.transpose(0, 3, 1, 2) B, C, H, W = img.shape # create tiled image. channel last image will be returned. tiled_image = tile_images(img) _, _, Ct = tiled_image.shape assert C == Ct return tiled_image
def add(self, name, var): import nnabla as nn from nnabla.utils.image_utils import imsave if isinstance(var, nn.Variable): data = var.d.copy() elif isinstance(var, nn.NdArray): data = var.data.copy() else: assert isinstance(var, np.ndarray) data = var.copy() assert data.ndim > 2 channels = data.shape[-3] data = data.reshape(-1, *data.shape[-3:]) data = data[:min(data.shape[0], self.num_images)] data = self.normalize_method(data) if channels > 3: data = data[:, :3] elif channels == 2: data = np.concatenate( [data, np.ones((data.shape[0], 1) + data.shape[-2:])], axis=1) tile = tile_images(data) path = os.path.join(self.save_dir, '{}.png'.format(name)) imsave(path, tile)
def plot_stats(digits): print("Num images:", digits.images.shape[0]) print("Image shape:", digits.images.shape[1:]) print("Labels:", digits.target[:10]) plt.imshow(tile_images(digits.images[:64, None]), **imshow_opt)
tiny_digits = import_module("nnabla/tutorial/tiny_digits") np.random.seed(0) imshow_opt = dict(cmap="gray", interpolation="nearest") # ## Logistic Regression # ### Preparing a Toy Dataset digits = tiny_digits.load_digits(n_class=10) tiny_digits.plot_stats(digits) # - data = tiny_digits.data_iterator_tiny_digits(digits, batch_size=64, shuffle=True) # - img, label = data.next() plt.imshow(tile_images(img), **imshow_opt) print("labels:\n", label.reshape(8, 8)) print("Label shape:", label.shape) # ### Preparing the Computation Graph # !Forward pass x = nn.Variable(img.shape) # Define an image variable with nn.parameter_scope("affine1"): y = PF.affine(x, 10) # Output is 10 class # - # !Building a loss graph t = nn.Variable(label.shape) # Define an target variable # !Softmax Xentropy fits multi-class classification problems loss = F.mean(F.softmax_cross_entropy(y, t)) # - print("Printing shapes of variables")