We often use CNN networks to process images in deep learning. Here is an example:
Implement TensorFlow CNN Networks for MNIST Handwritten Digits Classification
However, how to see the effect of images processd by CNN networks? In this tutorial, we will introduce the way to use python matplotlib to display.
How python matplotlib display images
We can use matplotlib.pyplot.imshow() function to display images. Here is the tutorial:
Understand matplotlib.pyplot.imshow(): Display Data as an Image – Matplotlib Tutorial
Then we will introduce the way to use python matplotlib to display images processed by tensorflow cnn networks.
Load MNIST images
We will use an image in MNIST dataset to make an example.
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import os import numpy as np import matplotlib.pyplot as plt mnist = input_data.read_data_sets(os.getcwd() + "/MNIST-data/", one_hot=True) x, y = mnist.train.next_batch(10) print(y[2]) x = x[2]
We will find:
y[2] is: [0. 0. 0. 0. 0. 0. 1. 0. 0. 0.], which means y[2] = 6
x[2] contains the data of image
We will use a cnn network to process x[2] and display the processed image using matplotlib.
Build a tensorflow cnn network to process image
x = tf.convert_to_tensor(x) X_images = tf.reshape(x, [-1, 28, 28, 1]) conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 3], stddev=0.1), name='conv1_Weights') # out_channels = 3 conv1_biases = tf.Variable(tf.constant(0.1, shape=[3]), name='conv1_biases') #[batch, out_height, out_width, out_channels] #out_channels = 3 conv1_conv2d = tf.nn.conv2d(X_images, conv1_Weights, strides=[1, 1, 1, 1], padding='SAME') + conv1_biases conv1_activated = tf.nn.relu(conv1_conv2d) #[batch, out_height, out_width, channels] # channels = 3 conv1_pooled = tf.nn.max_pool(conv1_activated, ksize=[1, 2, 2, 1], strides=[1, 1, 1, 1], padding='SAME')
You should understand how to use tf.nn.conv2d() and tf.nn.max_pool(). Here are tutorials:
Understand tf.nn.conv2d(): Compute a 2-D Convolution in TensorFlow – TensorFlow Tutorial
We must set the out_channels of cnn filter is 1, 3 or 4, because the matplotlib.pyplot.imshow() function only can receive (M,N), (M, N, 3) or (M, N, 4) data.
Then we can display conv1_pooled, which contains the data of image processed by cnn network.
Display Image using matplotlib.pyplot.imshow()
First, we use numpy.squeeze() to remove the axis with length = 1. Here is the tutorial:
Understand numpy.squeeze(): Remove Dimensions of Length 1 – NumPy Tutorial
Then we can display the processed image.
init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) data = sess.run(conv1_pooled) #the shape of data is (1, 14, 14, 32) print(np.shape(data)) data = np.squeeze(data) print(np.shape(data)) plt.imshow(data, cmap='Greys') plt.show()
The image is:
Although we set cmap=’Greys’, we can find the image is colorful. Why?
Because the out_channels of cnn filter is 3, which means the processed image contains red, green and blue.
If we set the out_channels of cnn filter is 1.
conv1_Weights = tf.Variable(tf.truncated_normal([5, 5, 1, 1], stddev=0.1), name='conv1_Weights') # out_channels = 32 conv1_biases = tf.Variable(tf.constant(0.1, shape=[1]), name='conv1_biases')
The processed image will be grey.