This is an example of using Keras to do some basic image classification, you can find the full code on my github. By now I think almost everyone is probably aware of what Keras is and how easy it can make some tasks like reading in and batching data. To demonstrate this I shared this code at the Indianapolis Machine Learning meetup. It's a simple example where, with really little code, you can build a classifier to identify Simpson characters.
I came across the data on the sub-reddit datasets (great place for anyone looking for unique datasets or for asking questions). John Bencina is the one that put the dataset together and has it as one of his github repos.
Keras makes this example really simple and to the point so let's just dive right in.
Of course the first thing we need to do is import all the packages we'll be using for this demo.
Here we set our parameters. Setting the locations of the images, the batch sizes and epocs, and we're scalling the images to be 80 by 80. In this case we're only making predictions for Homer, Marge, Bart, Lisa. Maggie was left out because there was signifigantly fewer images of her.
This is where Keras starts making life easy. By importing the ImageGenerator from Keras we can quickly create a generator for reading in our data. Not only will this take care of the batching but we can also specify if and how we want the images to be augmented with commands like rescale, sheer, and horizontal_flip.
Same thing but with the validation data.
The first thing I'm doing before the modeling is setting some class weights. This is a good idea because there are a lot more images of Homer than Lisa. Next I'm setting up the model, with just two convolutional layers and two fully connected layer, and then a final layer with the number of nodes equal our number of classes. The last line is using the Keras callback function to log our results so we can view them in TensorBoard.
This is where the model runs. Because I'm only doing 30 epocs this should run in just a couple of minutes with a decient GPU.
Now we have a trained model, it's time to review what we got. With the code below I get a printout of the overall accuracy is. This varries from run to run, but tends to be around 92-95%. Not bad for an extreamly simple and quick model.
A common thing to do with any image classification is to feed the model an image and and see how it predicts. The code below randomly picks an image and displays the prediction. Below is what the output looks like.
Besides using the the code above to view the accuracy we can also use TensorBoard. Because we included the Keras callback all we have to do use run the following command in terminal tensorboar --logdir=path_to_log