Building Convolutional Neural Networks in Python using Keras

Original article can be found here (source): Artificial Intelligence on Medium

Convolutional Neural Network Walkthrough


First, we need data for our deep learning model to learn from. In this example I will be using an open source weather data for classification from Mendeley, I encourage you to follow along by downloading it or using your own data. I will be treating the weather data as a multi-class classification problem with the following labels: cloudy, rain, sunshine, sunrise. To start we will write some code to create a list of image paths which we will use later to read the image and label it.

Let’s breakdown each section of this code one by one…

1.) Get all image path sets

  • This code is responsible for one thing, creating lists of image paths for each respective image class. I put all the images from the weather dataset into the folder weather_data, then by iterating through that directory we append each path to the list if each image pertains to its class by checking if its name is relevant to its class.

2.) Randomly Shuffle Images

  • So that there is no selection bias when creating our training and testing splits we will randomly shuffle these images before aggregating them.

3.) Training and Testing Image Sets

  • Relatively straightforward, we will use the first 150 random images in each list as our training set, and the rest in each list as our testing set.

4.) Garbage Collection

  • Image recognition systems are hardly computationally inexpensive, we can cleanup some memory allocation by deleting the lists we have already reassigned and collecting the garbage.

Image Pre-Processing

Before building the model we need to reformat the images so the model knows what dimensions to expect.

5.) Static Image Variables

  • Here we are declaring nRows and nCols as essentially the pixel height and width we will reformat the image to. Channels refers to color channels, in this case we are using 3 for RGB, but you can use 1 if you prefer grayscale

6.) Training and Testing Splits

  • We created sets earlier that contain the paths to images for our training and testing splits. Now, we are going to use these arrays to contain the read images along with their label.

7/8.) Image Reading and Data Labeling

  • Probably the most important part so let’s break this down. There are two for loops, one for building the training set the other for the testing set. We are iterating through each image path in the sets of paths we built earlier. Using cv2 we read in and resize the image based on our static dimensions from (5). Next we have a series of if statements to determine the class of each read image. Each image’s name is based on its class (e.g. rain1, rain2, rain3,…), so we check to see if cloudy, rain, sunrise, shine is contained within the image path and append a 1, 2, 3, or 4 accordingly. Lastly, I want to acknowledge that some images may fail to be read, so we want to surround the operations within the for loop with a try/except to catch images we fail to read.

9.) Garbage Collection

10.) Convert to NumPy Arrays

  • We want our training and testing sets to be converted into NumPy arrays so we can feed it into our Convolutional Neural Network, and reformat the input and target data using accompanying libraries like Scikit-learn and Keras.

11.) Convert to Categorical

  • So that we can use a softmax activation function X [0, 1] to predict the image class we are going to convert our vector of labels where L {1, 2, 3, 4} to a categorical set L {0, 1}.

Model Development

Lastly, we will build the convolutional neural network using Keras.

12.) Creating the Model

  • Here we create a sequential model so after the Conv2D, MaxPooling2D, Flatten, and Dropout layers do their job we can feed it straight into a fully connected layer for classification.

Conv2D —Extract key features from images

MaxPooling2D — Dimensionality reduction by down-sampling

Flatten — Flattens the input shape e.g. (8, 16) → 128

Dropout — Prevent model overfitting

Dense — Fully connected layer to classify flattened input

13.) Model Summary

  • Here we get an idea of how many parameters are being optimized with each training iteration. Play around with adding and removing MaxPooling2D layers to see how this affects the total number of training parameters. This is obviously correlated with training time.

14.) Compile and Fit the Model

  • At this point we have essentially completed the model. The last thing we need to do is compile and fit our model to the training data. We will use adam as our optimizer and categorical corssentropy and accuracy as our loss and metric respectively.


15/16.) Visualizing Results

  • The last thing we can do to help visualize the results of our model is to create plots for the accuracy and loss over the training life of the model.

After running all the code we developed in this article lets check out the model’s outputs…

After 10 training epochs we ended with training accuracy ~88% and validation accuracy of ~93%, not bad!

There are a variety of ways this model can be improved upon, you can get started by simply adding more hidden layers and more training epochs.

By now you should have a good idea of how to create your own image recognition system! Again if you want to download this project it can be found on my GitHub.