Icon

Image_​Classification_​MNIST_​Solution

Simple CNN for Image Classification

Exercise of the L4-DL Deep Learning Specialization course.

Train a CNN model to classify handwritten digit images with the correct number.
- Define network structure
- Prepare and read the dataset
- Preprocess images
- Train the model and evaluate performance

The enclosed pictures are from the MNIST dataset (http://yann.lecun.com/exdb/mnist/).

URL: L4-DL course slides https://www.knime.com/form/material-download-registration

Step 5. Prepare and read test dataset

The Decompress Files node is configured to unzip the test dataset (1500 images) into the data/test folder and output the path to the images as well as to the parent folder. You have to perform the same steps as the two sections above:

  1. Extract the index of the images

  2. Sort according to the index

  3. Replace the RowIDs

  4. Read test images into KNIME



Step 1. Prepare training dataset

  1. Execute the Decompress Files node. It will unzip the train dataset and create a new folder (data/train) containing 10000 images from the MNIST dataset. The node outputs the path to these images as well as the path to the parent folder.

  2. Use a Row filter node to exclude the row containing the path to the parent folder (i.e. Directory = true)

Notice from the Path attribute that the images are read in random order. In order to correctly join the labels, you have to sort the path in ascending order (../data/train/images/Row1.png , ../data/train/images/Row2.png , and so on)

  1. Use a Path to String node to convert the Path column into string format.

  2. Use an Expression node to create a new column "Image Number" containing the index of the image.
    Hint: Ask K-AI to Extract the number at the end of the path and provide an example path.

  3. Convert the new column into Number (Integer) format using the String to Number node

  4. Use a Sorter node to sort the rows in ascending order of the Image Number column

  5. Add a RowID node to replace the RowIDs with ordered ones (Row0, Row1...)


* OPTIONAL: wrap up this process' nodes into a metadone to tidy up your workflow



Session 3 - Convolutional Neural Networks

Exercise 01 Simple CNN for Image Classification

Learning objective: In this exercise you'll learn how to train a CNN able to recognise the correct number given the picture of the handwritten digit.


Workflow description:

Train a CNN model to classify handwritten digit images with the correct number.

  • Define network structure

  • Prepare and read the dataset

  • Preprocess images

  • Train the model and evaluate performance

The enclosed pictures are from the MNIST dataset (http://yann.lecun.com/exdb/mnist/).


Requirements:

You'll find the instructions to the exercises in the yellow annotations.

Step 2. Read training dataset

You are now ready to load the training images into the workflow.

  1. Add an Image Reader (Table) node to load the images into KNIME (TIP: select UNSIGNEDBYTETYPE as Pixel type)

You should now be finally able to see the images in the node output



Step 3. Append train data labels

  1. Execute the CSV Reader containing the labels for the train data

  2. Use a Column Appender node to match the actual digits to the images (TIP: you can visually check in the node output wether the match is correct)



Step 4. Preprocess train data

Prepare the data to be fed into the network

  1. Use the Image Calculator node to normalize images. (TIP: Result Pixel Type should be FLOATTYPE)

    Include the following expression: $Image$ / 255

  2. Convert the Actual Digit column into a collection. Use the Create Collection Column node



Step 6. Append test data labels

  1. Execute the CSV Reader containing the labels for the test data.

  2. Use a Column Appender node to match the actual digits to the images (TIP: you can visually check in the node output wether the match is correct)



Step 7. Preprocess test data

  1. Use the Image Calculator node to normalize images. (TIP: Result Pixel Type should be FLOATTYPE)

    Include the following expression: $Image$ / 255

  2. Note that there is no need here to convert the target column into a collection column



Step 8. Define the structure of your CNN network using the Keras Layers node

A possible solution can be:

  1. Keras Input Layer node configured with input shape 28, 28, 1

  2. Keras Convolution 2D Layer node:

    • Filters: 32

    • Kernel size: 3,3

    • Dilation Rate: 2,2

    • Activation Function: ReLu

  3. Keras Max Pooling 2D Layer node: Pool size (2,2) and Stride (2,2), valid Padding

  4. Keras Flatten Layer node

  5. Keras Dense Layer node: 100 units, ReLu activation function

  6. Keras Dense Layer node: 10 units, Softmax activation function (TIP: use Name prefix to mark the output layer)



Step 9. Train and apply model

  1. Add a Keras Network Learner node that takes in input the preprocessed tran set. Connect the last layer of the network structure previously defined. Open the configuration window

    • Set conversion From Image (Auto Mapping) and select the Image column as input data

    • Set conversion From Collection of Number (Integer) to One-Hot Tensor and select the AggregatedValues column as target data

    • In the Option tab specify the number of Epochs (e.g. 10)

    • Select Categorical cross entropy as Loss Function
      (TIP: during training you can inspect the process: right click on the node and select View: Learning Monitor)

  2. Apply the model to the test set using the Keras Network Executor node

    • Check the box to keep input columns in the output table

    • Click on "add output" and select the last layer of your model
      (TIP: select the correct input column. If you cannot see it, check if the correct conversion is applied)



Step 10. Extract prediction and evaluate trained network

The network produces a 10 elements vector as output. In order to calculate one single prediction, you only want to consider the element with highest probability. Notice that these steps have been automatized using a shared component.

  1. Add the Extract prediction component (drag and drop it form the components folder) to calculate the element with the highest prediction

  2. Add a scorer node and evaluate the performance of the model



Extract Prediction
Append target column
Column Appender
Input: 28, 28, 1
Keras Input Layer
Keras Flatten Layer
Units: 100 Activation: ReLU
Keras Dense Layer
Filters: 32Kernel: 3, 3
Keras Convolution 2D Layer
Pool size: 2, 2
Keras Max Pooling 2D Layer
Apply the model to test data
Keras Network Executor
Units: 10Activation: Softmax
Keras Dense Layer
from unsignedbyte to float
Image Calculator
Train the model
Keras Network Learner
Unzip test data
Decompress Files
Append target column
Column Appender
Scorer
Unzip train data
Decompress Files
labels train data (= actual digits)
CSV Reader
Extract img index and sort rows
Prepare files
Convert target to collection
Create Collection Column
from unsignedbyte to float
Image Calculator
image size28x28
Image Reader (Table)
labels test data (= actual digits)
CSV Reader
Prepare and read MNIST images

Nodes

Extensions

Links