Gregor Koehler / Oct 06 2017

Bird or No Bird in Keras

curl -o data.zip https://s3-us-west-2.amazonaws.com/ml-is-fun/data.zip
unzip data.zip
cp full_dataset.pkl /results
Done
fetch data
Bash



full_dataset.pkl
Open in new window
conda install -qy -c anaconda tensorflow-gpu h5py
pip install keras
pip install Pillow
Done
install keras
Bash































































# imports for array-handling and plotting
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

# let's keep our keras backend tensorflow quiet
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
# for testing on CPU
#os.environ['CUDA_VISIBLE_DEVICES'] = ''

# keras imports for the dataset and building our neural network
from keras.datasets import mnist
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Dropout, Flatten#, Activation
from keras.layers.convolutional import Conv2D
from keras.layers.pooling import MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import np_utils

# pickle to load the dataset object
import pickle

# scipy to load images
import scipy
Done
imports
Python


# image preprocessing and real-time augmentation during training
datagen = ImageDataGenerator(
    featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=25,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True)
Done
preprocessing / image augmentation
Python
# load the dataset
X_train, Y_train, X_test, Y_test = pickle.load(open(fetch data.full_dataset.pkl, "rb"))
Done
load data
Python
datagen.fit(X_train)
Done
compute preprocessing quantities
Python
# define neural network as linear stack of layers
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', 
                 input_shape=(32, 32, 3)))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(Conv2D(64, kernel_size=(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())

model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(2, activation='softmax'))

model.summary()

# compile model
model.compile(loss='categorical_crossentropy',
             metrics=['accuracy'],
             optimizer='adam')
Done
define model
Python


























# training settings
n_epochs = 10
batch_size = 128

# training the model
history = model.fit_generator(
  datagen.flow(X_train, Y_train, batch_size=batch_size), 
  steps_per_epoch=len(X_train) / batch_size, 
  epochs = n_epochs, verbose=2, 
  validation_data=(X_test, Y_test))

# saving the model
save_dir = "/results/"
model_name = 'keras_birds.h5'
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)

# plotting the metrics
fig = plt.figure()
plt.subplot(2,1,1)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='lower right')

plt.subplot(2,1,2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper right')

plt.tight_layout()

fig
Done
train model and plot learning curve
Python






















keras_birds.h5
Open in new window
# loading the image file
img = scipy.ndimage.imread(bird_bullocks_oriole.jpg, mode="RGB")

# rescaling the image
img = scipy.misc.imresize(img, (32, 32),
                          interp="bicubic").astype(np.float32,
                                                   casting='unsafe')

# reshaping the image to fit network input requirements
img.reshape(1,32,32,3).shape

# loading the model 
birds_model = load_model(train model and plot learning curve.keras_birds.h5)

# classifying the image
prediction = birds_model.predict(img.reshape(1,32,32,3))

# checking the result
is_bird = np.argmax(prediction[0]) == 1

if is_bird:
    print("That's a bird!")
else:
    print("That's not a bird!")
Done
test uploaded image
Python