Project - How to Build a Neural Network for Image Classification with TensorFlow

5 / 8

Visualizing the Data

With reference to the class labels and the class names as given in the official documantation of Keras, let us store all them in a list class_names as follows.

Note:

  • plt.imshow(X, cmap="binary") displays X data as an image. cmap is used to map scalar data to colors. cmap=binary maps the image color to black and white format while displaying the image.

  • plt.axis('off') is used to turn off the axes for subplots( you could remove this line and observe the difference for yourself).

  • plt.subplot(a,b) is used to create a figure and a set of subplots with a rows and b columns.

  • plt.subplots_adjust() is used to tune the subplot layout.

INSTRUCTIONS
  • Write the following code to store the class names of the data set.

    class_names = ["T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
           "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
    
  • Plot an image using Matplotlib's plt.imshow() function, with a 'binary' color map:

    print("Class label is:", y_train[0])
    print("Class name is:", class_names[y_train[0]])
    plt.imshow(X_train[0], cmap="binary")
    plt.axis('off')
    plt.show()
    
  • Let's take a look at a sample of the images in the dataset.

    n_rows = 4
    n_cols = 10
    plt.figure(figsize=(15, 6))
    for row in range(n_rows):
        for col in range(n_cols):
            index = n_cols * row + col
            plt.subplot(n_rows, n_cols, index + 1)
            plt.imshow(X_train[index],cmap="binary")
            plt.axis('off')
            plt.title(class_names[y_train[index]], fontsize=12)
    plt.subplots_adjust(wspace=0.2, hspace=0.5)
    plt.show()
    
Get Hint See Answer


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...