API - 可视化

TensorFlow 提供了可视化模型和激活输出等的工具 TensorBoard。 在这里,我们进一步提供一些可视化模型参数和数据的函数。

read_image(image[, path]) Read one image.
read_images(img_list[, path, n_threads, ...]) Returns all images in list by given path and name of each image file.
save_image(image[, image_path]) Save a image.
save_images(images, size[, image_path]) Save multiple images into one single image.
draw_boxes_and_labels_to_image(image, ...[, ...]) Draw bboxes and class labels on image.
draw_mpii_pose_to_image(image, poses[, ...]) Draw people(s) into image using MPII dataset format as input, return or save the result image.
W([W, second, saveable, shape, name, fig_idx]) Visualize every columns of the weight matrix to a group of Greyscale img.
CNN2d([CNN, second, saveable, name, fig_idx]) Display a group of RGB or Greyscale CNN masks.
frame([I, second, saveable, name, cmap, fig_idx]) Display a frame.
images2d([images, second, saveable, name, ...]) Display a group of RGB or Greyscale images.
tsne_embedding(embeddings, reverse_dictionary) Visualize the embeddings by using t-SNE.

读取与保存图片

读取单个图片

tensorlayer.visualize.read_image(image, path='')[源代码]

Read one image.

参数:
  • image (str) -- The image file name.
  • path (str) -- The image folder path.
返回:

The image.

返回类型:

numpy.array

读取多个图片

tensorlayer.visualize.read_images(img_list, path='', n_threads=10, printable=True)[源代码]

Returns all images in list by given path and name of each image file.

参数:
  • img_list (list of str) -- The image file names.
  • path (str) -- The image folder path.
  • n_threads (int) -- The number of threads to read image.
  • printable (boolean) -- Whether to print information when reading images.
返回:

The images.

返回类型:

list of numpy.array

保存单个图片

tensorlayer.visualize.save_image(image, image_path='_temp.png')[源代码]

Save a image.

参数:
  • image (numpy array) -- [w, h, c]
  • image_path (str) -- path

保存多个图片

tensorlayer.visualize.save_images(images, size, image_path='_temp.png')[源代码]

Save multiple images into one single image.

参数:
  • images (numpy array) -- (batch, w, h, c)
  • size (list of 2 ints) -- row and column number. number of images should be equal or less than size[0] * size[1]
  • image_path (str) -- save path

Examples

>>> import numpy as np
>>> import tensorlayer as tl
>>> images = np.random.rand(64, 100, 100, 3)
>>> tl.visualize.save_images(images, [8, 8], 'temp.png')

保存目标检测图片

tensorlayer.visualize.draw_boxes_and_labels_to_image(image, classes, coords, scores, classes_list, is_center=True, is_rescale=True, save_name=None)[源代码]

Draw bboxes and class labels on image. Return or save the image with bboxes, example in the docs of tl.prepro.

参数:
  • image (numpy.array) -- The RGB image [height, width, channel].
  • classes (list of int) -- A list of class ID (int).
  • coords (list of int) --
    A list of list for coordinates.
    • Should be [x, y, x2, y2] (up-left and botton-right format)
    • If [x_center, y_center, w, h] (set is_center to True).
  • scores (list of float) -- A list of score (float). (Optional)
  • classes_list (list of str) -- for converting ID to string on image.
  • is_center (boolean) --
    Whether the coordinates is [x_center, y_center, w, h]
    • If coordinates are [x_center, y_center, w, h], set it to True for converting it to [x, y, x2, y2] (up-left and botton-right) internally.
    • If coordinates are [x1, x2, y1, y2], set it to False.
  • is_rescale (boolean) --
    Whether to rescale the coordinates from pixel-unit format to ratio format.
    • If True, the input coordinates are the portion of width and high, this API will scale the coordinates to pixel unit internally.
    • If False, feed the coordinates with pixel unit format.
  • save_name (None or str) -- The name of image file (i.e. image.png), if None, not to save image.
返回:

The saved image.

返回类型:

numpy.array

References

保存姿态估计图片(MPII)

tensorlayer.visualize.draw_mpii_pose_to_image(image, poses, save_name='image.png')[源代码]

Draw people(s) into image using MPII dataset format as input, return or save the result image.

This is an experimental API, can be changed in the future.

参数:
  • image (numpy.array) -- The RGB image [height, width, channel].
  • poses (list of dict) -- The people(s) annotation in MPII format, see tl.files.load_mpii_pose_dataset.
  • save_name (None or str) -- The name of image file (i.e. image.png), if None, not to save image.
返回:

The saved image.

返回类型:

numpy.array

Examples

>>> import pprint
>>> import tensorlayer as tl
>>> img_train_list, ann_train_list, img_test_list, ann_test_list = tl.files.load_mpii_pose_dataset()
>>> image = tl.vis.read_image(img_train_list[0])
>>> tl.vis.draw_mpii_pose_to_image(image, ann_train_list[0], 'image.png')
>>> pprint.pprint(ann_train_list[0])

References

可视化模型参数

可视化Weight Matrix

tensorlayer.visualize.W(W=None, second=10, saveable=True, shape=None, name='mnist', fig_idx=2396512)

Visualize every columns of the weight matrix to a group of Greyscale img.

参数:
  • W (numpy.array) -- The weight matrix
  • second (int) -- The display second(s) for the image(s), if saveable is False.
  • saveable (boolean) -- Save or plot the figure.
  • shape (a list with 2 int or None) -- The shape of feature image, MNIST is [28, 80].
  • name (a string) -- A name to save the image, if saveable is True.
  • fig_idx (int) -- matplotlib figure index.

Examples

>>> tl.visualize.draw_weights(network.all_params[0].eval(), second=10, saveable=True, name='weight_of_1st_layer', fig_idx=2012)

可视化CNN 2d filter

tensorlayer.visualize.CNN2d(CNN=None, second=10, saveable=True, name='cnn', fig_idx=3119362)[源代码]

Display a group of RGB or Greyscale CNN masks.

参数:
  • CNN (numpy.array) -- The image. e.g: 64 5x5 RGB images can be (5, 5, 3, 64).
  • second (int) -- The display second(s) for the image(s), if saveable is False.
  • saveable (boolean) -- Save or plot the figure.
  • name (str) -- A name to save the image, if saveable is True.
  • fig_idx (int) -- The matplotlib figure index.

Examples

>>> tl.visualize.CNN2d(network.all_params[0].eval(), second=10, saveable=True, name='cnn1_mnist', fig_idx=2012)

可视化图像

matplotlib显示单图

tensorlayer.visualize.frame(I=None, second=5, saveable=True, name='frame', cmap=None, fig_idx=12836)[源代码]

Display a frame. Make sure OpenAI Gym render() is disable before using it.

参数:
  • I (numpy.array) -- The image.
  • second (int) -- The display second(s) for the image(s), if saveable is False.
  • saveable (boolean) -- Save or plot the figure.
  • name (str) -- A name to save the image, if saveable is True.
  • cmap (None or str) -- 'gray' for greyscale, None for default, etc.
  • fig_idx (int) -- matplotlib figure index.

Examples

>>> env = gym.make("Pong-v0")
>>> observation = env.reset()
>>> tl.visualize.frame(observation)

matplotlib显示多图

tensorlayer.visualize.images2d(images=None, second=10, saveable=True, name='images', dtype=None, fig_idx=3119362)[源代码]

Display a group of RGB or Greyscale images.

参数:
  • images (numpy.array) -- The images.
  • second (int) -- The display second(s) for the image(s), if saveable is False.
  • saveable (boolean) -- Save or plot the figure.
  • name (str) -- A name to save the image, if saveable is True.
  • dtype (None or numpy data type) -- The data type for displaying the images.
  • fig_idx (int) -- matplotlib figure index.

Examples

>>> X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(shape=(-1, 32, 32, 3), plotable=False)
>>> tl.visualize.images2d(X_train[0:100,:,:,:], second=10, saveable=False, name='cifar10', dtype=np.uint8, fig_idx=20212)

可视化词嵌入矩阵

tensorlayer.visualize.tsne_embedding(embeddings, reverse_dictionary, plot_only=500, second=5, saveable=False, name='tsne', fig_idx=9862)[源代码]

Visualize the embeddings by using t-SNE.

参数:
  • embeddings (numpy.array) -- The embedding matrix.
  • reverse_dictionary (dictionary) -- id_to_word, mapping id to unique word.
  • plot_only (int) -- The number of examples to plot, choice the most common words.
  • second (int) -- The display second(s) for the image(s), if saveable is False.
  • saveable (boolean) -- Save or plot the figure.
  • name (str) -- A name to save the image, if saveable is True.
  • fig_idx (int) -- matplotlib figure index.

Examples

>>> see 'tutorial_word2vec_basic.py'
>>> final_embeddings = normalized_embeddings.eval()
>>> tl.visualize.tsne_embedding(final_embeddings, labels, reverse_dictionary,
...                   plot_only=500, second=5, saveable=False, name='tsne')