Non-Binary Image Classifying CNN Tutorial (5 categories) with Keras

Colleen Dunlap
Analytics Vidhya
Published in
4 min readNov 16, 2019

There are a ton of datasets and tutorials out there for building binary (2 category) CNN image classifiers. However, I don’t see nearly as many resources out there for building Image Classifying CNNs with 3 or more categories. And there are some important differences when designing a non-binary Image Classification CNN, as opposed to a binary CNN. This tutorial will walk you through how to build a CNN for a dataset with 3 or more categories.

First thing’s first — let’s get our dataset. It was surprisingly difficult to find a raw image dataset (of substantial sample size) with more than 2 categories. However, I did end up finding a solid Flower Recognition dataset by Alexander Mamaev on Kaggle. This dataset has images of flowers belonging to 5 different categories — Dandelion, Daisy, Tulip, Rose, and Sunflower. Each category has between 700 and 1100 images.

While the images of this dataset were already split into folders based on category, they weren’t split into testing/training sets. I used a quick Python script to do so. You can view that script at here.

Now to develop our CNN. The outline of my model looks like this

1. Input Convolution Layer, take in 32x32 size images, generate 32 3x3 feature maps
2. Dropout Layer (to prevent Overfitting)
3. Max Pooling Layer (Picks strongest features in feature map)
4. Full Connection/ANN Layers (one with 1,024 units, another with 512 units, both with relu activation function)
5. Output Layer (softmax function)

Here’s a Visual Representation of that. We get our image data matrix, generate multiple feature maps, and pool from those feature maps. We then “flatten” that data into a vector to feed into our ANN/Fully Connected Layers. We then have an output layer that contains the probability that the image belongs to each of our 5 categories.

Image Source Here

So now onto the code. First, let’s import our dependencies and set up our Convolution Layer.

Now for our Dropout Layer. I set this at 10% to prevent over-fitting our model.

And finally, we pool using Max Pooling and flatten to prepare our data to be passed to the ANN Layers

Now we create our “Full Connection.” I used two layers for our ANN — one takes in 1024 units, the second takes in 512 units. Then, comes our output layer, a “softmax” function where the number of nodes is equivalent to the number of categories (in our case, 5).

Next, we set up the Image Data Generators. This flow from the directories our testing and training data is in and creates image data for us to use as inputs to our model. In the original dataset, images are about 200x200 pixels. We will reduce this for speed and the prevention of over-fitting.

And now we train our model. I settled on 10 epochs, with 50 steps per epoch. You can play around with these numbers and see how this affects your accuracy.

From here, we can validate our model’s accuracy, and plot the model’s accuracy and loss over training time.

And then we can run the model on a single image and see the result!

It worked! result[0][0] is the probability of the image being a daisy (index is 0 because daisy is our first category listed)

And there you have it! Wasn’t so bad was it? Now you can expand this method to have as many categories as you want! And if you have enough data quality and quantity, it’ll even be accurate :D

Any questions? Comments? Words of Admiration? Let me know in the comments below ❤

--

--

Colleen Dunlap
Analytics Vidhya

I’m a free-spirited techie who loves Social Impact Projects, Hackathons, and the Circus. You can follow me on Twitter @ColleenDHere