Creating a TensorFlow Image Classifier in AWS SageMaker

1 hour
  • 5 Learning Objectives

About this Hands-on Lab

TensorFlow is the biggest name in machine learning frameworks. In this lab, you will use TensorFlow to create a neural network that performs a basic image classification task: deciding which LEGO brick is in an image to help you sort your giant pile of blocks.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Navigate to the Jupyter Notebook

Log in to the AWS console and navigate to the AWS SageMaker page. From there, load the Jupyter Notebook that has been provided with this hands-on lab.

Load and Prepare the Data
  1. Load the training images and labels into numpy arrays. The images and labels are provided in the files lego-simple-train-images.npy and lego-simple-train-labels.npy, respectively.
  2. Load the testing images and labels into numpy arrays. The images and labels are provided in the files lego-simple-test-images.npy and lego-simple-test-labels.npy, respectively.
  3. Add in the human-readable class names for the labels.
  4. Visualize the first few images from the training data set to better understand the data.
Train the TensorFlow Model
  1. Create a neural network model using Keras.
    • Remember to check your input shape and adjust if necessary.
    • You can get decent performance from a single hidden layer, but feel free to experiment with different model architectures.
    • You should have as many output nodes as labels you are trying to predict. Remember to pick an activation function that will output categorical probabilites.
  2. Compile the model, including accuracy as a metric. Your loss function should be one appropriate for classification tasks.
  3. Train the model using the training data and training labels. You won’t need to train for many epochs. Save the history of the training process.
Evaluate the Model
  1. Calculate the loss and accuracy of the model on the testing data.
  2. View the raw output of a model prediction for an image in the test set.
  3. Determine the label that the model predicted for the image and compare that to the actual label.
Make a Batch Prediction
  1. Predict the labels for all of the testing data.
  2. Compare the predictions against the actual labels.

Additional Resources


You have a lot of LEGO bricks to sort. Doing it by hand will take forever and will cause you so much eye strain from looking at the tiny pieces. Instead, you should train a machine to recognize the bricks and tell you (or ideally, a robot) which bin the piece should be placed in. You already have a lot of pictures of LEGO bricks, which is what you'll use to train the model.

The files used in this lab can be found on GitHub.

Lab Goals

  1. Navigate to the Jupyter Notebook
  2. Load and Prepare the Data
  3. Train the TensorFlow Model
  4. Test and Analyze the Model
  5. Make a Batch Prediction Using the Testing Data

Logging in to the Lab Environment

To avoid issues with the lab, use a new Incognito or Private browser window to log in to the lab. This ensures that your personal account credentials, which may be active in your main window, are not used for the lab.

Please make sure you are in the us-east-1 (N. Virginia) region when in the AWS console.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?