A simple deep learning example on how to start classifying images with your own data. This notebook is expected to be executed after 17flowers_data.ipynb (for data creation).
After preparing necessary environment, please make sure to have these files where you execute this notebook. https://github.com/roebius/deeplearning1_keras2/tree/master/nbs
In [1]:
from __future__ import division, print_function
%matplotlib inline
In [2]:
path = "data/17flowers/"
In [3]:
import os, json
from glob import glob
import numpy as np
np.set_printoptions(precision=4, linewidth=100)
from matplotlib import pyplot as plt
In [4]:
# check that ~/.keras/keras.json is set for Theano and includes "image_data_format": "channels_first"
from importlib import reload # Python 3
import utils; reload(utils)
from utils import plots
In [5]:
# As large as you can, but no larger than 64 is recommended.
batch_size = 8
#batch_size = 64
In [6]:
# Import our class, and instantiate
import vgg16; reload(vgg16)
from vgg16 import Vgg16
The original pre-trained Vgg16 class classifies images into one of the 1000 categories. This number of categories depends on the dataset which Vgg16 was trained with. (http://image-net.org/challenges/LSVRC/2014/browse-synsets)
In order to classify images into the categories which we prepare (17 categories of flowers, in this notebook), fine-tuning technology is useful. It:
In [7]:
vgg = Vgg16()
# Grab a few images at a time for training and validation.
# NB: They must be in subdirectories named based on their category
batches = vgg.get_batches(path+'train', batch_size=batch_size)
val_batches = vgg.get_batches(path+'valid', batch_size=batch_size*2)
# please note that the vgg model is compiled inside the finetune method.
vgg.finetune(batches)
vgg.fit(batches, val_batches, batch_size, nb_epoch=1)
Let's grab batches of data from our test folder:
In [8]:
batches = vgg.get_batches(path+'test', batch_size=5)
In [9]:
imgs,labels = next(batches)
print(labels)
In [10]:
#plots(imgs, titles=labels)
plots(imgs)
With finetuning, the model vgg outputs the probability of 17 classes. As you can see below, they are generally relevant to the input images.
Note that these images are not used during model training.
In [11]:
vgg.predict(imgs, True)
Out[11]:
In [12]:
vgg.classes[:20]
Out[12]:
In [13]:
# make new Vgg16 instance
vgg2 = Vgg16()
# need to compile the model before using
vgg2.compile()
Again get some images for prediction
In [14]:
batches = vgg2.get_batches(path+'test', batch_size=5)
In [15]:
imgs,labels = next(batches)
print(labels)
In [16]:
plots(imgs)
Without finetuning, the model vgg2 outputs the probability of 1000 classes. As you can see below, they are sometimes totally irrelevant.
In [17]:
vgg2.predict(imgs, True)
Out[17]:
In [18]:
vgg2.classes[:20]
Out[18]: