In [2]:
import os
import pickle
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from skimage import io
from sklearn.cross_validation import train_test_split

from keras.models import Sequential, model_from_json
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
from keras.utils import np_utils
Using Theano backend.
In [3]:
%matplotlib inline
In [6]:
PUG_IMG_DIR = "../data/pugs_cropped"
CHI_IMG_DIR = "../data/chihuahuas_cropped"
IMG_ROWS, IMG_COLS = 224, 224
IMG_CHANNELS = 3
In [7]:
n_pug_images = len(os.listdir(PUG_IMG_DIR))
pug_images = np.empty((n_pug_images, IMG_CHANNELS, IMG_ROWS, IMG_COLS), dtype="uint8")

n_chi_images = len(os.listdir(CHI_IMG_DIR))
chi_images = np.empty((n_chi_images, IMG_CHANNELS, IMG_ROWS, IMG_COLS), dtype="uint8")
In [8]:
for n, image in enumerate(os.listdir(PUG_IMG_DIR)):
    pug_images[n] = io.imread(PUG_IMG_DIR+"/"+image).transpose()

for n, image in enumerate(os.listdir(CHI_IMG_DIR)):
    chi_images[n] = io.imread(CHI_IMG_DIR+"/"+image).transpose()
In [9]:
print(pug_images.shape)
print(chi_images.shape)
(903, 3, 224, 224)
(1224, 3, 224, 224)
In [10]:
pug_labels = np.ones(n_pug_images)
chi_labels = np.zeros(n_chi_images)
In [11]:
X = np.concatenate([pug_images, chi_images])
y = np.concatenate([pug_labels, chi_labels])
In [14]:
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.60, stratify=y)
In [15]:
with open("../data/pugs_vs_chihuahuas_data.pkl.gz", "wb") as pickle_file:
    pickle.dump((X_train, X_test, y_train, y_test), pickle_file)
In [ ]:
#Or just open pickle file here
with open("../data/pugs_vs_chihuahuas_data.pkl.gz", "rb") as pickle_file:
    X_train, X_test, y_train, y_test = pickle.load(pickle_file)
In [16]:
X_val, X_test, y_val, y_test = train_test_split(X_train, y_train, test_size=0.50)
In [17]:
print(X_train.shape)
print(X_val.shape)
print(X_test.shape)
(1276, 3, 224, 224)
(638, 3, 224, 224)
(638, 3, 224, 224)
In [18]:
batch_size = 32
n_classes = 2
n_epochs = 10
In [19]:
Y_train = np_utils.to_categorical(y_train, n_classes)
Y_val = np_utils.to_categorical(y_val, n_classes)
Y_test = np_utils.to_categorical(y_test, n_classes)
//anaconda/lib/python2.7/site-packages/keras/utils/np_utils.py:16: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  Y[i, y[i]] = 1.
In [20]:
print(Y_train.shape)
print(Y_val.shape)
print(Y_test.shape)
(1276, 2)
(638, 2)
(638, 2)
In [23]:
# we're going to use a pre-trained deep network and chop off the
# last dense layer; we'll freeze the weights in the early layers
# and then train just the final set of dense weights
# see https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3
def Lee(weights_path=None):
    model = Sequential()
    model.add(ZeroPadding2D((1,1),input_shape=(IMG_CHANNELS, IMG_ROWS, IMG_COLS), trainable=False))
    model.add(Convolution2D(64, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(64, 3, 3, activation='relu', trainable=False))
    model.add(MaxPooling2D((2,2), strides=(2,2), trainable=False))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(128, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(128, 3, 3, activation='relu', trainable=False))
    model.add(MaxPooling2D((2,2), strides=(2,2), trainable=False))

    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(256, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(256, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(256, 3, 3, activation='relu', trainable=False))
    model.add(MaxPooling2D((2,2), strides=(2,2), trainable=False))

    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
    model.add(MaxPooling2D((2,2), strides=(2,2), trainable=False))

    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
    model.add(ZeroPadding2D((1,1), trainable=False))
    model.add(Convolution2D(512, 3, 3, activation='relu', trainable=False))
    model.add(MaxPooling2D((2,2), strides=(2,2), trainable=False))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu', trainable=False))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu', trainable=False))
    model.add(Dropout(0.5))
    model.add(Dense(2, activation='softmax'))

    if weights_path:
        model.load_weights(weights_path)

    return model
In [21]:
sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)
In [24]:
#Load model from saved weights and architecture
model_2 = Lee('chi_model_weights.h5')
model_2.compile(optimizer=sgd, loss='categorical_crossentropy')
In [25]:
y_pred = model_2.predict_classes(X_test)
accuracy = np.sum(y_pred == y_test) / float(len(y_test))
print(accuracy)
638/638 [==============================] - 274s    
0.909090909091
In [37]:
i=3
my_img = X_test[i].transpose()
plt.axis('off')
plt.imshow(my_img)
img = np.array(my_img).transpose()
img = img.reshape((1, 3, 224, 224))
mypred = model_2.predict(img)
print(mypred)
[[  2.48917490e-06   9.99997497e-01]]
In [45]:
YAFFA_IMG_DIR = "../data/yaffa_cropped"
n_yaffa_images = len(os.listdir(YAFFA_IMG_DIR))
yaffa_images = np.empty((n_yaffa_images, IMG_CHANNELS, IMG_ROWS, IMG_COLS), dtype="uint8")

for n, image in enumerate(os.listdir(YAFFA_IMG_DIR)):
    yaffa_images[n] = io.imread(YAFFA_IMG_DIR+"/"+image).transpose()
    
print(yaffa_images.shape)    
(6, 3, 224, 224)
In [56]:
i=4
my_img = yaffa_images[i].transpose()
plt.axis('off')
plt.imshow(my_img)
img = np.array(my_img).transpose()
img = img.reshape((1, 3, 224, 224))
mypred = model_2.predict(img)
print(mypred)
[[ 0.2168123   0.78318769]]
In [57]:
OTHER_IMG_DIR = "../data/other_cropped"
n_other_images = len(os.listdir(OTHER_IMG_DIR))
other_images = np.empty((n_other_images, IMG_CHANNELS, IMG_ROWS, IMG_COLS), dtype="uint8")

for n, image in enumerate(os.listdir(OTHER_IMG_DIR)):
    other_images[n] = io.imread(OTHER_IMG_DIR+"/"+image).transpose()
    
print(other_images.shape) 
(7, 3, 224, 224)
In [66]:
i=0
my_img = other_images[i].transpose()
plt.axis('off')
plt.imshow(my_img)
img = np.array(my_img).transpose()
img = img.reshape((1, 3, 224, 224))
mypred = model_2.predict(img)
print(mypred)
[[  9.99994159e-01   5.82003076e-06]]
In [ ]: