Icon

Image_​Classification_​MNIST

Simple CNN for Image Classification
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 preprocessed tran set. Connect the lastlayer 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 theAggregatedValues 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 and select View: LearningMonitor- 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 isapplied Extract prediction and evaluate trained networkThe network produces a 10 elements vector as output. In order tocalculate one single prediction, you only want to consider theelement with highest probability. Notice that these steps have beenautomatized using a shared component.- Add the Extract prediction component (drag and drop it form thecomponents folder) to calculate the element with the highestprediction- Add a scorer node and evaluate the performance of 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 imagesfrom the MNIST dataset. The node outputs the path to these 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 correctly join the labels, you have to sort thepath 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 the index of the image. One solution isgiven 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 as well as to the parent folder. You have to perform thesame steps as the two sections above:- Extract the index of the images- Sort according to the index- Replace the RowIDs- Read test images into KNIMEOPTIONAL: Instead of copy-paste single nodes, create a component out of the nodes of the above sections to automatize the process: - Select the node of Prepare training dataset and Read training dataset sections - Right click and select Create Component... - Give it a meaningful name (e.g. Prepare and read MNIST images) - Create a copy of the component and append it the Decompress Files node to load test data 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 the labels for the traindata. - Use a Column Appender node to match the actual digits to theimages TIP: you can visually check in the node output wetherthe match is correct Preprocess train dataPrepare the data to be fed into the network- Use the Image Calculator node to normalize images.TIP: Result Pixel Type should be FLOATTYPE. Include thefollowing expression $Image$ / 255- Convert the Actual Digit column into a collection. Use theCreate Collection Column node Read training datasetYou are now ready to load the training images into the workflow.- Use a Path to String node to convert the Path column intostring format- Add an Image Reader (Table) node to load the images intoKNIMETIP: select UNSIGNEDBYTETYPE as Pixel typeYou should now be finally able to see the images in the nodeoutput Append test data labels- Execute the CSV Reader containing the labels for the testdata. - Use a Column Appender node to match the actual digits to theimages TIP: you can visually check in the node output wetherthe match is correct Preprocess test data- Use the Image Calculator node to normalize images.TIP: Result Pixel Type should be FLOATTYPE. Include thefollowing expression $Image$ / 255- Note that there is no need here to convert the target columninto a collection column Use Case DescriptionThe MNIST dataset (source http://yann.lecun.com/exdb/mnist/) contains images of handwritten digits as well as the corresponding number. The goal of thisexercise is to train a CNN able to recognise the correct number given the picture of the handwritten digit. Unzip test dataUnzip train datalabels test data(= actual digits)labels train data(= actual digits)Decompress Files Decompress Files CSV Reader CSV Reader 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 preprocessed tran set. Connect the lastlayer 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 theAggregatedValues 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 and select View: LearningMonitor- 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 isapplied Extract prediction and evaluate trained networkThe network produces a 10 elements vector as output. In order tocalculate one single prediction, you only want to consider theelement with highest probability. Notice that these steps have beenautomatized using a shared component.- Add the Extract prediction component (drag and drop it form thecomponents folder) to calculate the element with the highestprediction- Add a scorer node and evaluate the performance of 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 imagesfrom the MNIST dataset. The node outputs the path to these 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 correctly join the labels, you have to sort thepath 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 the index of the image. One solution isgiven 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 as well as to the parent folder. You have to perform thesame steps as the two sections above:- Extract the index of the images- Sort according to the index- Replace the RowIDs- Read test images into KNIMEOPTIONAL: Instead of copy-paste single nodes, create a component out of the nodes of the above sections to automatize the process: - Select the node of Prepare training dataset and Read training dataset sections - Right click and select Create Component... - Give it a meaningful name (e.g. Prepare and read MNIST images) - Create a copy of the component and append it the Decompress Files node to load test data 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 the labels for the traindata. - Use a Column Appender node to match the actual digits to theimages TIP: you can visually check in the node output wetherthe match is correct Preprocess train dataPrepare the data to be fed into the network- Use the Image Calculator node to normalize images.TIP: Result Pixel Type should be FLOATTYPE. Include thefollowing expression $Image$ / 255- Convert the Actual Digit column into a collection. Use theCreate Collection Column node Read training datasetYou are now ready to load the training images into the workflow.- Use a Path to String node to convert the Path column intostring format- Add an Image Reader (Table) node to load the images intoKNIMETIP: select UNSIGNEDBYTETYPE as Pixel typeYou should now be finally able to see the images in the nodeoutput Append test data labels- Execute the CSV Reader containing the labels for the testdata. - Use a Column Appender node to match the actual digits to theimages TIP: you can visually check in the node output wetherthe match is correct Preprocess test data- Use the Image Calculator node to normalize images.TIP: Result Pixel Type should be FLOATTYPE. Include thefollowing expression $Image$ / 255- Note that there is no need here to convert the target columninto a collection column Use Case DescriptionThe MNIST dataset (source http://yann.lecun.com/exdb/mnist/) contains images of handwritten digits as well as the corresponding number. The goal of thisexercise is to train a CNN able to recognise the correct number given the picture of the handwritten digit. Unzip test dataUnzip train datalabels test data(= actual digits)labels train data(= actual digits)Decompress Files Decompress Files CSV Reader CSV Reader

Nodes

Extensions

Links