Icon

Image_​Classification_​MNIST_​Solution

<p>Simple CNN for Image Classification<br><br>Train a CNN model to classify MNIST images.<br>- Define network structure<br>- Prepare and read the dataset<br>- Preprocess images<br>- Train the model and evaluate performance<br><br>The enclosed pictures are from the MNIST dataset originally published by Yann LeCun (http://yann.lecun.com/).</p>
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, Sigmoid activation function Tip: Use the Name prefix to mark the output layer. Train and apply model Add the Keras Network Learner node that takes in input the preprocessed train 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. 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. 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. Add the Extract Prediction component (drag and drop it form the components folder) to calculate the element with the highest prediction Add the 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 images from the MNIST dataset. The node outputs the path to these images as well as the path to the parent folder. Use the Path to String node to convert the Path column into string format. 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) Use the Expression node to create a new column "Image Number" containing the index of the image and convert it into an integer. One solution is given by the following formula parse_int(regex_replace($["Location"], ".*Row(.*).png", "$1")) 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: Extract the index of the images Sort according to the index Replace the RowIDs Read test images into KNIME Optional: Instead of copy-pasting 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 Append train data labels Execute the CSV Reader containing the labels for the train data. Use the Column Appender node to match the actual digits to the images Tip: You can visually check in the node output whether the match is correct. Preprocess train data Prepare the data to be fed into the network. Use the Image Calculator node to normalize images. Tip: Result Pixel Type should be FLOATTYPE. Include the following expression: $Image$ / 255. Convert the Actual Digit column into a collection. Use the Create Collection Column node Read training dataset You are now ready to load the training images into the workflow. Add the 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. Append test data labels Execute the CSV Reader containing the labels for the test data Use the Column Appender node to match the actual digits to the images Tip: You can visually check in the node output whether the match is correct. Preprocess test data Use the Image Calculator node to normalize images. Tip: Result Pixel Type should be FLOATTYPE. Include the following expression $Image$ / 255. Note that there is no need here to convert the target column into a collection column. Chapter 9: MNIST Classification Solution: Image Classification with a Convolutional Neural Network (CNN) This workflow defines, trains and applies a simple Convolutional Neural Network (CNN) to classify images from the MNIST dataset. Requirements: KNIME Deep Learning - Keras Integration. Please refer to the KNIME docs to set up Python and Keras Deep Learning environment. KNIME Expressions KNIME Image Processing KNIME Image Processing - Deep Learning Extension Input: 28, 28, 1 Filters: 32 Kernel: 3, 3 Pool size: 2, 2 Units: 100 Activation: ReLU Units: 10 Activation: Softmax Train the model Apply the model to test data Unzip test data Unzip train data labels test data (= actual digits) image size 28x28 Append target column from unsigned byte to float labels train data (= actual digits) Convert target to collection Append target column from unsigned byte to float Extract img index and sort rows Keras Input Layer Keras Convolution 2D Layer Keras Max Pooling 2D Layer Keras Flatten Layer Keras Dense Layer Keras Dense Layer Keras Network Learner Keras Network Executor Scorer Decompress Files Decompress Files CSV Reader Image Reader (Table) Column Appender Image Calculator CSV Reader Create Collection Column Extract Prediction Column Appender Image Calculator Prepare and read MNIST images Prepare files 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, Sigmoid activation function Tip: Use the Name prefix to mark the output layer. Train and apply model Add the Keras Network Learner node that takes in input the preprocessed train 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. 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. 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. Add the Extract Prediction component (drag and drop it form the components folder) to calculate the element with the highest prediction Add the 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 images from the MNIST dataset. The node outputs the path to these images as well as the path to the parent folder. Use the Path to String node to convert the Path column into string format. 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) Use the Expression node to create a new column "Image Number" containing the index of the image and convert it into an integer. One solution is given by the following formula parse_int(regex_replace($["Location"], ".*Row(.*).png", "$1")) 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: Extract the index of the images Sort according to the index Replace the RowIDs Read test images into KNIME Optional: Instead of copy-pasting 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 Append train data labels Execute the CSV Reader containing the labels for the train data. Use the Column Appender node to match the actual digits to the images Tip: You can visually check in the node output whether the match is correct. Preprocess train data Prepare the data to be fed into the network. Use the Image Calculator node to normalize images. Tip: Result Pixel Type should be FLOATTYPE. Include the following expression: $Image$ / 255. Convert the Actual Digit column into a collection. Use the Create Collection Column node Read training dataset You are now ready to load the training images into the workflow. Add the 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. Append test data labels Execute the CSV Reader containing the labels for the test data Use the Column Appender node to match the actual digits to the images Tip: You can visually check in the node output whether the match is correct. Preprocess test data Use the Image Calculator node to normalize images. Tip: Result Pixel Type should be FLOATTYPE. Include the following expression $Image$ / 255. Note that there is no need here to convert the target column into a collection column. Chapter 9: MNIST Classification Solution: Image Classification with a Convolutional Neural Network (CNN) This workflow defines, trains and applies a simple Convolutional Neural Network (CNN) to classify images from the MNIST dataset. Requirements: KNIME Deep Learning - Keras Integration. Please refer to the KNIME docs to set up Python and Keras Deep Learning environment. KNIME Expressions KNIME Image Processing KNIME Image Processing - Deep Learning Extension Input: 28, 28, 1 Filters: 32 Kernel: 3, 3 Pool size: 2, 2 Units: 100 Activation: ReLU Units: 10 Activation: Softmax Train the model Apply the model to test data Unzip test data Unzip train data labels test data (= actual digits) image size 28x28 Append target column from unsigned byte to float labels train data (= actual digits) Convert target to collection Append target column from unsigned byte to float Extract img index and sort rows Keras Input Layer Keras Convolution 2D Layer Keras Max Pooling 2D Layer Keras Flatten Layer Keras Dense Layer Keras Dense Layer Keras Network Learner Keras Network Executor Scorer Decompress Files Decompress Files CSV Reader Image Reader (Table) Column Appender Image Calculator CSV Reader Create Collection Column Extract Prediction Column Appender Image Calculator Prepare and read MNIST images Prepare files

Nodes

Extensions

Links