Saturday, March 18, 2017

Classifying Dogs vs Cats on a Regular Laptop with 2GB GPU and 90% Accuracy


Machine learning ecosystem has evolved a lot during recent years.
I am amazed that I could run a very sophisticated experiment of classifying dogs vs cats with 90% accuracy on my regular laptop laptop.
It has 2GB NVidia GPU card and 8GB RAM.
Just in 2012 the state of art result of the dogs vs cats classification was 80%.

I ran it based on an excellent course provided by fast.ai (http://course.fast.ai/).
The competition is organized by Kaggle:
https://www.kaggle.com/c/dogs-vs-cats-redux-kernels-edition

Here's an overview of the approach taken to achieve 90% accuracy.
First, retrieve a publicly available model VGG16, which was prepared by scientists for image recognition competition (for ImageNet). Then remove last layer out of it and replace with Yes / No layer for recognizing cats vs dogs. The remaining layers were set as non trainable. Then run learning process for such model.

The main libraries used here are Keras with Tensorflow backend.

Full code is available on fast.ai website. Here in an overview of the most important parts.
Training code:

import tensorflow as tf
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.allow_soft_placement=True
config.log_device_placement=True
set_session(tf.Session(config=config))

# Import our class, and instantiate
import vgg16; reload(vgg16)
from vgg16 import Vgg16
vgg = Vgg16()

batch_size=16
path = "data/dogscats/"
#path = "data/dogscats/sample/"
batches = vgg.get_batches(path+'train', batch_size=batch_size)
val_batches = vgg.get_batches(path+'valid', batch_size=batch_size)
vgg.finetune(batches)
vgg.fit(batches, val_batches, nb_epoch=1)
vgg.model.save('vgg2.h5')

The code uses vgg.finetune call to update the last layer of the model. Here's how it looks like:

model = self.model
        model.pop()
        for layer in model.layers: layer.trainable=False
        model.add(Dense(num, activation='softmax'))

Next, it trains model using vgg.fit call and saves result to vgg2.h5 file. 

I had to put a few tweaks to the model related to device placement for Tensorflow so it could fit in GPU memory. The last few layers were placed on CPU. Here's the code:

      model = self.model = Sequential()
        model.add(Lambda(vgg_preprocess, input_shape=(3,224,224), output_shape=(3,224,224)))

        with tf.device('/gpu:0'):
            self.ConvBlock(2, 64)
            self.ConvBlock(2, 128)
            self.ConvBlock(3, 256)
            self.ConvBlock(3, 512)
            self.ConvBlock(3, 512)

        with tf.device('/cpu:0'):
            model.add(Flatten())
            self.FCBlock()
            self.FCBlock()
            model.add(Dense(1000, activation='softmax'))

        fname = 'vgg16.h5'
        model.load_weights(get_file(fname, self.FILE_PATH+fname, cache_subdir='models'))


Here's the result of a learning process:

23000/23000 [==============================] - 2103s - loss: 0.5482 - acc: 0.8676 - val_loss: 0.4194 - val_acc: 0.9060

The training process completed in 35 minutes with 90% accuracy on validation set. 

I'm very positively surprised that such powerful machine learning tools are available these days and are runnable on regular computers. Moreover the approach presented by fast.ai is very interesting and resembles natural evolution of intelligence by adding new layers. 

14 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Get the best web design for your offline business and take your business online. Connect with the best Website designing company in Ghaziabad and increase your online business.

    ReplyDelete
  3. Goal tide is the best IAS coaching center, we have trained, qualified, and experienced teacher who passed toughest exam of UPSC. Join with us and come true your dreams.
    Daily online Quiz for UPSC prelims Free

    ReplyDelete
  4. Do you want Work permit in malta? If yes then connect with Steadfast services that is located in United Arab Emirates.

    ReplyDelete
  5. Its Pleasure to read your blog.The above articles is very impressive, and I really enjoyed reading your blog and points that you expressed. I love to come back on a regular basis,post more on the subject.
    ibm full form in india |
    ssb ka full form |
    what is the full form of dp |
    full form of brics |
    gnm nursing full form |
    full form of bce |
    full form of php |
    bhim full form |
    nota full form in india |
    apec full form

    ReplyDelete
  6. Every company also has its own unique website development process but there is a certain process, we have elaborated it to develop and rank your website. Contact for more information
    Web design company Edmonton

    ReplyDelete
  7. Are you looking for a iphone insurance If yes then we have a best option for you connect with us, we have a great deal for you
    Iphone insurance plan

    ReplyDelete
  8. If you want to grow your business through a website, then CSS Founder would be a good choice for you? contact us to connect with CSS Founder. CSS Founder is the best website design company
    Website disign company in singapore

    ReplyDelete
  9. We have 75% return customers Just because of getting satisfactory results from Promotedial. Don't waste your time and spread your business worldwide. Google ads Services in Italy

    ReplyDelete
  10. Very nice blog and information you have shared. Connect with Al mumtazt tents if you are looking to making your party more impressive. We are the best provider of tents in Dubai. Car Parking Shades Supplier

    ReplyDelete
  11. Earth is the only known planet in this universe that has water and life. uv ultraviolet 12 24 36 gpm vertical type

    ReplyDelete