Icon

Image_​Classification_​MNIST_​Solution

Simple CNN for Image Classification

Exercise of the L4-DL Introduction to Deep Learning 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/).

Define the structure of your CNN network using the Keras Layers node. A possible solution can be:- Keras Input Layer node configured with input shape 28, 28, 1- Keras Convolution 2D Layer node: - Filters: 32 - Kernel size: 3,3 - Dilation Rate: 2,2 - Activation Function: ReLu- Keras Max Pooling 2D Layer node: Pool size (2,2) and Stride (2,2), valid Padding- Keras Flatten Layer node- Keras Dense Layer node: 100 units, ReLu activation function- Keras Dense Layer node: 10 units, Softmax activation function TIP: use Name prefix to mark the output layer Train and apply model- Add a Keras Network Learner node that takes in input the preprocessedtran set. Connect the last layer of the network structure previously defined.Open the configuration window. - Set conversion From Image (Auto Mapping) and select the Imagecolumn as input data - Set conversion From Collection of Number (Integer) to One-HotTensor 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 FunctionTIP: during training you can inspect the process: right click on the node andselect View: Learning Monitor- 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 thecorrect conversion is applied Extract prediction and evaluate trained networkThe network produces a 10 elements vector asoutput. In order to calculate one single prediction,you only want to consider the element with highestprobability. Notice that these steps have beenautomatized using a shared component.- Add the Extract prediction component (drag anddrop it form the components folder) to calculatethe element with the highest prediction- Add a scorer node and evaluate the performanceof the model Prepare training dataset- 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 tothese images as well as the path to the parent folder. - 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 correctlyjoin the labels, you have to sort the path in ascending order (../data/train/images/Row1.png , ../data/train/images/Row2.png , and so on)- Use a String Manipulation node to create a new column "Image Number" containing theindex of the image. One solution is given by the following formula regexReplace($Path$, ".*Row(.*)\.png", "$1")- Convert the new column into Number (Integer) format using the String to Number node- Use a Sorter node to sort the rows in ascending order of the Image Number column- 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 Prepare and read test datasetThe Decompress Files node is configured to unzip the test dataset (1500 images) into the data/test folder and output the path to the images aswell as to the parent folder. You have to perform the same steps as the two sections above:- Extract the index of the images- Sort according to the index- Replace the RowIDs- Read test images into KNIME Requirements- KNIME Deep Learning - Keras Integration Please refer to this guide to set up Python and Keras Deep Learning environmenthttps://docs.knime.com/2020-07/deep_learning_installation_guide/index.html#keras-integration- KNIME Expressions- KNIME Image Processing- KNIME Image Processing - Deep Learning Extension Append train data labels- Execute the CSV Reader containing thelabels for the train data. - Use a Column Appender node to match theactual digits to the images TIP: you can visually check in the nodeoutput wether the match is correct Preprocess train dataPrepare the data to be fed into the network- Use the Image Calculator node to normalizeimages.TIP: Result Pixel Type should be FLOATTYPE.Include the following expression $Image$ / 255- Convert the Actual Digit column into acollection. Use the Create Collection Columnnode Read training datasetYou are now ready to load the training imagesinto the workflow.- Use a Path to String node to convert the Pathcolumn into string format- Add an Image Reader (Table) node to load theimages into KNIMETIP: select UNSIGNEDBYTETYPE as Pixel typeYou should now be finally able to see theimages in the node output Append test data labels- Execute the CSV Reader containing thelabels for the test data. - Use a Column Appender node to match theactual digits to the images TIP: you can visually check in the nodeoutput wether the match is correct Preprocess test data- Use the Image Calculator node to normalizeimages.TIP: Result Pixel Type should be FLOATTYPE.Include the following expression $Image$ / 255- Note that there is no need here to convert thetarget column into a collection column Use Case DescriptionThe MNIST dataset (source http://yann.lecun.com/exdb/mnist/) contains images of handwritten digits as well as thecorresponding number. The goal of this exercise is to train a CNN able to recognise the correct number given thepicture of the handwritten digit. Input: 28, 28, 1Filters: 32Kernel: 3, 3Pool size: 2, 2 Units: 100 Activation: ReLUUnits: 10Activation: SoftmaxTrain the modelApply the modelto test dataUnzip test dataUnzip train datalabels test data(= actual digits)image size28x28Appendtarget columnfrom unsignedbyte to floatConvert Pathto String formatlabels train data(= actual digits)Convert targetto collectionAppendtarget columnfrom unsignedbyte to floatExtract img indexand sort rows Extract Prediction Keras Input Layer Keras Convolution2D Layer Keras Max Pooling2D Layer Keras Flatten Layer Keras Dense Layer Keras Dense Layer Keras NetworkLearner Keras NetworkExecutor Scorer Decompress Files Decompress Files CSV Reader Image Reader(Table) Column Appender Image Calculator Path to String CSV Reader Create CollectionColumn Column Appender Image Calculator Prepare files Prepare and readMNIST images Define the structure of your CNN network using the Keras Layers node. A possible solution can be:- Keras Input Layer node configured with input shape 28, 28, 1- Keras Convolution 2D Layer node: - Filters: 32 - Kernel size: 3,3 - Dilation Rate: 2,2 - Activation Function: ReLu- Keras Max Pooling 2D Layer node: Pool size (2,2) and Stride (2,2), valid Padding- Keras Flatten Layer node- Keras Dense Layer node: 100 units, ReLu activation function- Keras Dense Layer node: 10 units, Softmax activation function TIP: use Name prefix to mark the output layer Train and apply model- Add a Keras Network Learner node that takes in input the preprocessedtran set. Connect the last layer of the network structure previously defined.Open the configuration window. - Set conversion From Image (Auto Mapping) and select the Imagecolumn as input data - Set conversion From Collection of Number (Integer) to One-HotTensor 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 FunctionTIP: during training you can inspect the process: right click on the node andselect View: Learning Monitor- 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 thecorrect conversion is applied Extract prediction and evaluate trained networkThe network produces a 10 elements vector asoutput. In order to calculate one single prediction,you only want to consider the element with highestprobability. Notice that these steps have beenautomatized using a shared component.- Add the Extract prediction component (drag anddrop it form the components folder) to calculatethe element with the highest prediction- Add a scorer node and evaluate the performanceof the model Prepare training dataset- 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 tothese images as well as the path to the parent folder. - 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 correctlyjoin the labels, you have to sort the path in ascending order (../data/train/images/Row1.png , ../data/train/images/Row2.png , and so on)- Use a String Manipulation node to create a new column "Image Number" containing theindex of the image. One solution is given by the following formula regexReplace($Path$, ".*Row(.*)\.png", "$1")- Convert the new column into Number (Integer) format using the String to Number node- Use a Sorter node to sort the rows in ascending order of the Image Number column- 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 Prepare and read test datasetThe Decompress Files node is configured to unzip the test dataset (1500 images) into the data/test folder and output the path to the images aswell as to the parent folder. You have to perform the same steps as the two sections above:- Extract the index of the images- Sort according to the index- Replace the RowIDs- Read test images into KNIME Requirements- KNIME Deep Learning - Keras Integration Please refer to this guide to set up Python and Keras Deep Learning environmenthttps://docs.knime.com/2020-07/deep_learning_installation_guide/index.html#keras-integration- KNIME Expressions- KNIME Image Processing- KNIME Image Processing - Deep Learning Extension Append train data labels- Execute the CSV Reader containing thelabels for the train data. - Use a Column Appender node to match theactual digits to the images TIP: you can visually check in the nodeoutput wether the match is correct Preprocess train dataPrepare the data to be fed into the network- Use the Image Calculator node to normalizeimages.TIP: Result Pixel Type should be FLOATTYPE.Include the following expression $Image$ / 255- Convert the Actual Digit column into acollection. Use the Create Collection Columnnode Read training datasetYou are now ready to load the training imagesinto the workflow.- Use a Path to String node to convert the Pathcolumn into string format- Add an Image Reader (Table) node to load theimages into KNIMETIP: select UNSIGNEDBYTETYPE as Pixel typeYou should now be finally able to see theimages in the node output Append test data labels- Execute the CSV Reader containing thelabels for the test data. - Use a Column Appender node to match theactual digits to the images TIP: you can visually check in the nodeoutput wether the match is correct Preprocess test data- Use the Image Calculator node to normalizeimages.TIP: Result Pixel Type should be FLOATTYPE.Include the following expression $Image$ / 255- Note that there is no need here to convert thetarget column into a collection column Use Case DescriptionThe MNIST dataset (source http://yann.lecun.com/exdb/mnist/) contains images of handwritten digits as well as thecorresponding number. The goal of this exercise is to train a CNN able to recognise the correct number given thepicture of the handwritten digit. Input: 28, 28, 1Filters: 32Kernel: 3, 3Pool size: 2, 2 Units: 100 Activation: ReLUUnits: 10Activation: SoftmaxTrain the modelApply the modelto test dataUnzip test dataUnzip train datalabels test data(= actual digits)image size28x28Appendtarget columnfrom unsignedbyte to floatConvert Pathto String formatlabels train data(= actual digits)Convert targetto collectionAppendtarget columnfrom unsignedbyte to floatExtract img indexand sort rows Extract Prediction Keras Input Layer Keras Convolution2D Layer Keras Max Pooling2D Layer Keras Flatten Layer Keras Dense Layer Keras Dense Layer Keras NetworkLearner Keras NetworkExecutor Scorer Decompress Files Decompress Files CSV Reader Image Reader(Table) Column Appender Image Calculator Path to String CSV Reader Create CollectionColumn Column Appender Image Calculator Prepare files Prepare and readMNIST images

Nodes

Extensions

Links