Skip to main content

TensorFlow/Keras Basic Image Classifier (AWS SageMaker)

Hands-On Lab

 

Length

01:00:00

Difficulty

Intermediate

TensorFlow is the biggest name in machine learning and artificial intelligence. In this activity, we will use TensorFlow and Keras to create an artificial neural network that performs a basic image classification task. In this lab, we use our own data set and again get one step closer to the dream of an automated Lego brick sorting machine. The files used in this lab can be found on GitHub.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

TensorFlow/Keras Basic Image Classifier (AWS SageMaker)

Introduction

TensorFlow is the biggest name in machine learning and artificial intelligence. In this activity, we will use TensorFlow and Keras to create an artificial neural network that performs a basic image classification task. In this lab, we use our own data set and again get one step closer to the dream of an automated Lego brick sorting machine. The files used in this lab can be found on GitHub.

Logging In

Log in to AWS with the credentials provided on the hands-on lab page. In the Find Services box, look for SageMaker. Once we're in the SageMaker dashboard, click Notebook instances in the left-hand menu. There should be one sitting there. If there isn't one, double-check to make sure you're in the N. Virginia region (using the dropdown in the upper-right of the dashboard). When it shows up in the list of notebooks, click it and we'll land in the notebook server. To open up the notebook we'll be working with, click Open Jupyter over toward the right.

Navigate to the Jupyter Notebook

Once you're redirected to Jupyter, click to open the listed Tensorflow-keras-basic-classification.ipynb notebook. It may take a few minutes to fully spin up — once it's ready, you should see a Lego image pop up.

Run the Libraries

  1. With The Libraries code block selected, click Run at the top.

Load the Training and Testing Data Sets

  1. Click the first block of code in the Load the Data step, and click Run to load the training and testing data, including the labels, into NumPy arrays:

    train_images = np.load('lego-simple-train-images.npy')
    train_labels = np.load('lego-simple-train-labels.npy')
    test_images = np.load('lego-simple-test-images.npy')
    test_labels = np.load('lego-simple-test-labels.npy')
  2. To show the shape of train_images, click Insert > Insert Cell Below and enter:

    train_images.shape

    Then, click Run. It should return (451, 48, 48).

  3. To see what it looks like inside the data, enter the following and then click Run:

    train_images
  4. To see the labels, enter the following and click Run:

    train_labels
  5. Click the next code block and then click Run to add some human-readable labels for the data:

    class_names = ['2x3 Brick', '2x2 Brick', '1x3 Brick', '2x1 Brick', '1x1 Brick', '2x2 Macaroni', '2x2 Curved End', 'Cog 16 Tooth', '1x2 Handles', '1x2 Grill']
  6. Click the next code block and then click Run to review the data by rendering the first data in the data set:

    plt.figure()
    plt.imshow(train_images[0])
    plt.colorbar()
    plt.xlabel(class_names[train_labels[0]])
    plt.show()

    The first image in our training set should pop up beneath the code once it's run.

    If you'd like to see a different image, change the O in train_images and train_labels to a different number (e.g., 10) to see another image instead.

  7. Optionally, click the next code block and then click Run to plot the first 20 images from the data set:

    plt.figure(figsize=(15,15))
    for i in range(20):
        plt.subplot(5,5,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.imshow(train_images[i], cmap=plt.cm.binary)
        plt.xlabel(class_names[train_labels[i]])
    plt.show()

    The first 20 images should pop up beneath the code.

Train the TensorFlow/Keras Model with the Training Data, and Review

  1. Click the next code block and then click Run to create a TensorFlow model with Keras:

    model = keras.Sequential([
        keras.layers.Flatten(input_shape=(48, 48)),
        keras.layers.Dense(128, activation=tf.nn.relu),
        keras.layers.Dense(10, activation=tf.nn.softmax)
    ])
  2. Click the next code block and then click Run to compile the TensorFlow model:

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
  3. Click the next code block and then click Run to train the model, and store the history so we can review it later:

    history = model.fit(train_images, train_labels, epochs=4)

    The epochs will display once the code executes. Note in the output that with each epoch, the model gets a greater accuracy (acc) score.

  4. Click the next code block and then click Run to review the accuracy and loss of the epochs during training:

    plt.figure(figsize=(15, 4))
    
    plt.subplot(1, 2, 1)
    # Plot training accuracy values
    plt.plot(history.history['acc'])
    plt.title('Model accuracy')
    plt.ylabel('Accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    
    plt.subplot(1, 2, 2)
    # Plot training loss values
    plt.plot(history.history['loss'])
    plt.title('Model loss')
    plt.ylabel('Loss')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Test'], loc='upper left')
    plt.show()

    Model accuracy and Model loss graphs will display once the code is executed.

Test and Analyze the TensorFlow Model

  1. Click the next code block and then click Run to evaluate the model using the testing data and calculate the accuracy:

    test_loss, test_acc = model.evaluate(test_images, test_labels)
    print('Test accuracy:', test_acc)

    We should then see a test accuracy score.

  2. Click the next code block and then click Run to define some functions that help display the output of the training:

    def plot_image(i, predictions_array, true_label, img):
      predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
      plt.xticks([])
      plt.yticks([])
      plt.imshow(img, cmap=plt.cm.binary)
      predicted_label = np.argmax(predictions_array)
      if predicted_label == true_label:
        color = 'green'
      else:
        color = 'red'
      # Print a label with 'predicted class', 'probability %', 'actual class'
      plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                    100*np.max(predictions_array),
                                    class_names[true_label]),
                                    color=color)
    
    def plot_value_array(i, predictions_array, true_label):
      predictions_array, true_label = predictions_array[i], true_label[i]
      plt.xticks(range(10))
      plt.yticks([])
      plot = plt.bar(range(10), predictions_array, color="#777777")
      plt.ylim([0, 1])
      predicted_label = np.argmax(predictions_array)
      plot[predicted_label].set_color('red')
      plot[true_label].set_color('green')

Single Prediction

  1. Next, we're going to make a single prediction from one of the test images. Click the next code block and then click Run to load a single test image into a list:

    test_image_number = 8
    
    img = test_images[test_image_number]
    plt.figure()
    plt.xticks([])
    plt.yticks([])
    plt.imshow(img, cmap=plt.cm.binary)
    plt.show()

    An image should display once the code is executed.

  2. Click the next code block and then click Run to do a quick transformation of the image data, as the predict method expects a list of images:

    img = (np.expand_dims(img,0))
    img.shape

    We should see the output (1, 48, 48).

  3. Now, click the next code block and then click Run to pass our image to the model for prediction:

    predictions_single = model.predict(img)
    predictions_single

    We should see a list of 10 floats in the output.

  4. To find the predicted result, click the next code block and then click Run to use argmax on the output array:

    prediction_result = np.argmax(predictions_single[0])
    class_names[prediction_result]
  5. Click the next code block and then click Run to compare the result with the original label:

    class_names[test_labels[test_image_number]]
  6. To visualize the output, click the next code block and then click Run to use the functions we defined earlier:

    plot_value_array(0, predictions_single, [test_labels[test_image_number]])
    plt.xticks(range(10), class_names, rotation=45)
    plt.show()
  7. Feel free to re-run the single prediction commands again and change out the test_image_number to see different results.

Make a Batch Prediction Using the Testing Data

  1. Click the next code block and then click Run to perform a prediction on all the data in our test set:

    predictions = model.predict(test_images)
  2. Click the next code block and then click Run to summarize the first 16 images in our test data and display the results using the functions defined in the previous task:

    num_rows = 8
    num_cols = 2
    num_images = num_rows*num_cols
    plt.figure(figsize=(15, 16))
    for i in range(num_images):
      plt.subplot(num_rows, 2*num_cols, 2*i+1)
      plot_image(i, predictions, test_labels, test_images)
      plt.subplot(num_rows, 2*num_cols, 2*i+2)
      plot_value_array(i, predictions, test_labels)
    plt.show()

Conclusion

Congratulations on successfully completing this hands-on lab!