Is it a single quad or an eight?

Classifying boat classes via transfer learning.

By Jonathan Harrison 2018-05-20

Here we will try to train a classifier to identify different rowing boat classes. Rowing has several different boat classes based on the number of people in a crew boat and whether each rower has two oars (sculling) or a single oar (sweep rowing). We will use powerful pretrained computer vision models and adapt these for our purposes via Tensorflow.


Collecting a dataset

First lets collect an appropriate dataset of different classes of boat. A Python package (https://github.com/hardikvasa/google-images-download) helps make this simple.

In [1]:
from google_images_download import google_images_download
response = google_images_download.googleimagesdownload()   #class instantiation
In [2]:
search_words = "Rowing Single,Rowing Pair,Rowing Double,Rowing Four,Rowing Quad,Rowing Eight"
arguments = {"keywords":search_words,"limit":5,"print_urls":True}   #creating list of arguments
response.download(arguments)   #passing the arguments to the function
Item no.: 1 --> Item name = Rowing Single
Evaluating...
Starting Download...
Image URL: https://i.ytimg.com/vi/7n7YqlfWNRs/maxresdefault.jpg
Completed Image ====> 1. maxresdefault.jpg
Image URL: https://www.rowperfect.co.uk/wp-content/uploads/2016/01/Rowing-2-1-541x455.png
Completed Image ====> 2. rowing-2-1-541x455.png
Image URL: https://i.ytimg.com/vi/V2AiKs5jpNI/maxresdefault.jpg
Completed Image ====> 3. maxresdefault.jpg
Image URL: https://www.participaction.com/sites/default/files/styles/full_width/public/images/single%20rower%20at%20sunrise.jpg?itok=_5FCOqA-
Completed Image ====> 4. single%20rower%20at%20sunrise.jpg
Image URL: https://resources.stuff.co.nz/content/dam/images/1/h/k/k/5/s/image.related.StuffLandscapeSixteenByNine.620x349.1hkktt.png/1488416507112.jpg
Completed Image ====> 5. 1488416507112.jpg

Errors: 0


Item no.: 2 --> Item name = Rowing Pair
Evaluating...
Starting Download...
Image URL: https://i.ytimg.com/vi/OdxrrSqxbiY/maxresdefault.jpg
Completed Image ====> 1. maxresdefault.jpg
Image URL: https://i.ytimg.com/vi/2OrEa65wEaU/maxresdefault.jpg
Completed Image ====> 2. maxresdefault.jpg
Image URL: https://s1.reutersmedia.net/resources/r/?m=02&d=20120728&t=2&i=635422321&w=780&fh=&fw=&ll=&pl=&sq=&r=CBRE86R10MG00
Completed Image ====> 3. ?m=02&d=20120728&t=2&i=635422321&w=780&fh=&fw=&ll=&pl=&sq=&r=cbre86r10mg00.jpg
Image URL: http://www.abc.net.au/news/image/4161608-3x2-700x467.jpg
Completed Image ====> 4. 4161608-3x2-700x467.jpg
Image URL: https://therowingclub.files.wordpress.com/2013/08/pair01.jpg
Completed Image ====> 5. pair01.jpg

Errors: 0


Item no.: 3 --> Item name = Rowing Double
Evaluating...
Starting Download...
Image URL: https://i.ytimg.com/vi/EmvLcOvKhvg/maxresdefault.jpg
Completed Image ====> 1. maxresdefault.jpg
Image URL: https://i.ytimg.com/vi/2iH2GdKtCDw/maxresdefault.jpg
Completed Image ====> 2. maxresdefault.jpg
Image URL: http://news.images.itv.com/image/file/73082/image_update_2e7958c14b9abe9a_1344077634_9j-4aaqsk.jpeg
Completed Image ====> 3. image_update_2e7958c14b9abe9a_1344077634_9j-4aaqsk.jpeg
Image URL: http://www.worldrowing.com/mm//Photo/Photos/PhotosGeneral/12/75/29/127529_8-MD-SD.jpg
Completed Image ====> 4. 127529_8-md-sd.jpg
Image URL: http://en.people.cn/mediafile/200808/16/P200808161823341422461871.jpg
Completed Image ====> 5. p200808161823341422461871.jpg

Errors: 0


Item no.: 4 --> Item name = Rowing Four
Evaluating...
Starting Download...
Image URL: https://i.ytimg.com/vi/6wtUJAmehFY/maxresdefault.jpg
Completed Image ====> 1. maxresdefault.jpg
Image URL: http://news.images.itv.com/image/file/73071/image_update_489930917bf7f2cb_1344076375_9j-4aaqsk.jpeg
Completed Image ====> 2. image_update_489930917bf7f2cb_1344076375_9j-4aaqsk.jpeg
Image URL: https://therowingclub.files.wordpress.com/2013/08/coxed_four-e1377812714200.jpg
Completed Image ====> 3. coxed_four-e1377812714200.jpg
Image URL: http://img.nauticexpo.com/images_ne/photo-g/22370-10143589.jpg
Completed Image ====> 4. 22370-10143589.jpg
Image URL: https://secure.i.telegraph.co.uk/multimedia/archive/02293/rowing_2293008b.jpg
Completed Image ====> 5. rowing_2293008b.jpg

Errors: 0


Item no.: 5 --> Item name = Rowing Quad
Evaluating...
Starting Download...
Image URL: https://i.ytimg.com/vi/iKg6LAm3ue0/maxresdefault.jpg
Completed Image ====> 1. maxresdefault.jpg
Image URL: http://www.worldrowing.com/uploads/files/63f9bfba6a8f767027054f5822fbc770.jpg
Completed Image ====> 2. 63f9bfba6a8f767027054f5822fbc770.jpg
Image URL: http://2.bp.blogspot.com/-pPbguaTguEA/T6cmuulg3CI/AAAAAAAABhY/eD60WoWQuys/s1600/390650665213.jpg
Completed Image ====> 3. 390650665213.jpg
Image URL: http://www.sport.ox.ac.uk/wp-content/uploads/2014/08/EUSA-OUWLRC-racing-2-1024x363.jpg
Completed Image ====> 4. eusa-ouwlrc-racing-2-1024x363.jpg
Image URL: https://s3-eu-west-1.amazonaws.com/oarsport/production/680adebd2fcf89b37a78ae3ed8d0e434/gbr-mary-crew_large.jpg
Completed Image ====> 5. gbr-mary-crew_large.jpg

Errors: 0


Item no.: 6 --> Item name = Rowing Eight
Evaluating...
Starting Download...
Image URL: http://www.worldrowing.com/mm//Photo/Photos/PhotosGeneral/11/34/65/113465_8-MD-SD.jpg
Completed Image ====> 1. 113465_8-md-sd.jpg
Image URL: https://i.ytimg.com/vi/ZIetslktFQk/maxresdefault.jpg
Completed Image ====> 2. maxresdefault.jpg
Image URL: http://news.bbc.co.uk/sol/shared/bsp/hi/image_maps/05/3/1110284502/img/image.gif
Completed Image ====> 3. image.gif
Image URL: https://stillmed.olympic.org/media/Photos/2016/08/13/13-08-2016-Rowing-Women-Eight-01.jpg
Completed Image ====> 4. 13-08-2016-rowing-women-eight-01.jpg
Image URL: http://www.worldrowing.com/mm//Photo/Photos/PhotosGeneral/12/77/68/127768_8-MD-SD.jpg
Completed Image ====> 5. 127768_8-md-sd.jpg

Errors: 0

We can repeat this similarly with different choices of keywords to expand our dataset. (It seems to be more effective to take top hits for several different key words rather than using many hits for a broad category that gets misinterpreted by the search). Manually checking some of the downloaded images reveals that lots of images are connected with the wrong search term. Search for a double brings up a quad, for example. For retraining the model to be most effective, we want to remove and/or correct the mislabelled data. I do this manually, since there are not so many images, but this does impose a bottleneck on the amount of images. Taking the top results from google searches does also tend to return many of the same images, which also contributes to a restriction on the amount of data.


Do we have enough data?

Let's check how many images we managed to get hold of for each class

In [3]:
import os
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [4]:
boat_classes = [boat_class for boat_class in os.listdir('downloads') if os.path.isdir('downloads/'+boat_class)]
class_frequencies = [len([name for name in os.listdir('downloads/'+boat_class) if name.endswith('.jpg') or name.endswith('.png') ])
 for boat_class in boat_classes]
print('We have a total of {} images'.format(sum(class_frequencies)))
We have a total of 1802 images
In [5]:
plt.bar(boat_classes,class_frequencies)
plt.xticks(boat_classes, boat_classes, rotation='vertical')
plt.ylabel('# of examples')
plt.show()

This should be enough to give it a go at retraining the neural network model.

How does the model work?

To retrain the neural network model, we use the script here: https://raw.githubusercontent.com/tensorflow/hub/r0.1/examples/image_retraining/retrain.py The neural network model we are using is an architecture called Inception V3 trained on a large dataset known as Imagenet. This model consists of many successive layers that capture general features of an image, such as edges, through to fine details and textures at different layers. Nonlinear activations allow the model flexibly to approximate complex representations of data.

Running the script above first passes our images through the network without the final layer and stores the result in a cache. We can then fine tune the weights of the final layer. Storing the results avoids having to repeatedly pass images through the network. Since the Inception V3 model has been trained already on the large dataset of a variety of different objects (Imagenet), the model already has a good representation of objects in images and we can be successful in identifying boat classes simply by slightly altering this model.

In [6]:
os.system('python retrain.py --image_dir downloads/')
Out[6]:
0

Once the images have been converted to the representation given by the penultimate layer of the model (a 2048-dimensional vector), we can train the weights of the final layer via stochastic gradient descent. We define an objective function that tells us about the difference between the model predictions and the known image class labels for our images. In gradient descent, we would evaluate the gradient of the objective function that we want to minimize and move in the direction of $-\nabla f$ to give the direction of greatest decrease of the function. That is update the weights via $$ w^{k+1} = w^k - \alpha\nabla f(w^k) $$ where $w$ are the parameters of the layer we want to learn, $f$ is our objective function and $\alpha$ is the learning rate which describes how large a step we take in parameter space. Under certain conditions on the function $f$ and the learning rate following gradient descent is guaranteed to converge to a local minimum of the function $f$. In practice though, this requires evaluating the gradient on all of the available data at once which is not computationally feasible. Instead we can approximate the gradient by estimating it on a subset of our data (using say 100 images at a time). This introduces some noise via the noisy estimate of the gradient, but this noise has beneficial properties for the performance of stochastic gradient descent. Many more sophisticated variations on this optimization method have been developed using ideas such as momentum to avoid local minima and look for a global optimum. Some of these ideas are explained further here: https://distill.pub/2017/momentum/

To monitor the results of training the model by stochastic gradient descent, we can use an interactive dashboard called tensorboard.

In [7]:
os.system('tensorboard --logdir /tmp/retrain_logs')
Out[7]:
256

Then navigating to localhost:6006 in a browser will allow us to visualise the progress of training the model.

Evaluating the model

The objective function we use here is a cross-entropy loss defined as $H(p,q)=\operatorname {E}_{p}[-\log q]=-\sum _{x}p(x)\,\log q(x).\!$ We can also relate this to the KL divergence via $H(p,q)=H(p)+D_{{{\mathrm {KL}}}}(p\|q),\!$ so that for a fixed distribution, $p$, minimizing the cross-entropy is equivalent to minimizing the KL-divergence between $p$ and $q$. This enforces the distribution of true labels to be similar to the distribution of predicted labels.

We see from the graphs below that the objective decreases noisily, and at the same time the accuracy, the proportion of correctly predicted boat classes for images, increases. Eventually, after 4000 steps of training, the accuracy plateaus at around 65% accuracy on the test set of data. (We separate our data into some data on which to train the model and some test data to check the model is a good model. The blue curve shows metrics evaluated on test data, orange curve is on training data.)

In [8]:
from IPython.display import Image
Image(filename='cross_entropy_curve.png')
Out[8]:
In [9]:
Image(filename='accuracy_curve.png') 
Out[9]:

Example predictions

Great, so we can identify what class a boat is 65% of the time. Probably most humans that have seen a rowing boat before could do a lot better than this, but its a start.

Lets see some examples of images that the model could get right, and some it got wrong. First this image of an oxford college novice eight is incorrectly predicted to be a four. An eight is the second most likely boat class here though, with a four being the next biggest sweep boat after an eight.

In [10]:
Image(filename='test_001.jpg')
Out[10]:
In [11]:
!python label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --input_layer=Placeholder --output_layer=final_result --image=test_001.jpg
2018-05-20 21:55:44.648653: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
rowing eight 0.396509
rowing four 0.336336
rowing pair 0.112891
rowing quad 0.0836542
rowing double 0.0378897

This image of the kiwi pair is correctly labelled as a pair.

In [12]:
Image(filename='test_002.jpg')
Out[12]:
In [13]:
!python label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --input_layer=Placeholder --output_layer=final_result --image=test_002.jpg
2018-05-20 21:55:59.915949: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
rowing pair 0.589658
rowing single 0.233326
rowing four 0.0881896
rowing double 0.0828736
rowing quad 0.00423067

Finally this image of Vicky Thornley is correctly predicted as a single.

In [14]:
Image(filename='test_003.jpg')
Out[14]:
In [15]:
!python label_image.py --graph=/tmp/output_graph.pb --labels=/tmp/output_labels.txt --input_layer=Placeholder --output_layer=final_result --image=test_003.jpg
2018-05-20 21:56:15.235125: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
rowing single 0.573244
rowing pair 0.311283
rowing double 0.102702
rowing four 0.00991238
rowing quad 0.00218129

So, if we did want to improve this model, we could find better ways to gather more labelled data and train the model again. It seems as though the data is the key restriction here. This project was just for a little fun, with bumps racing in the form of summer eights starting this week in Oxford. Since identifying boat classes is a task that humans can already do very effectively and there is little need to automate this task, it's probably not worth the work required to get higher accuracy, but it is cool to see how powerful these models are.

This project has used a technique called transfer learning and you can learn much more here: https://www.tensorflow.org/tutorials/image_retraining