In [620]:
import glob
import numpy as np
import PIL
from PIL import Image
from collections import Counter
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
from keras.utils import np_utils
from sklearn.model_selection import StratifiedShuffleSplit, train_test_split
from sklearn.metrics import accuracy_score
In [2]:
cat_im = Image.open("data/train/cat.0.jpg")
dog_im = Image.open("data/train/dog.0.jpg")
In [3]:
cat_im
Out[3]:
In [4]:
dog_im
Out[4]:
In [10]:
cat_im.size
Out[10]:
In [8]:
cat_array = np.array(cat_im.getdata())
In [9]:
cat_array.shape
Out[9]:
In [361]:
from keras.datasets import cifar10
In [362]:
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
In [363]:
# 3 RBG, 32*32 Image
X_train.shape
Out[363]:
In [365]:
# need to resize image so that numpy concat works
resized_cat = cat_im.resize((100, 100))
resized_cat
Out[365]:
In [367]:
np.array(resized_cat).shape
Out[367]:
In [369]:
PIL.Image.fromarray(np.array(resized_cat).T[1])
Out[369]:
In [440]:
def convert_image_to_data(image):
image_resized = Image.open(image).resize((100, 100))
cat_array = np.array(image_resized).T
return cat_array
In [441]:
cat_files = glob.glob("data/train/cat*")
dog_files = glob.glob("data/train/dog*")
In [442]:
cat_list = []
for i in cat_files[:10]:
cat_list.append(convert_image_to_data(i))
In [443]:
dog_list = []
for i in dog_files[:10]:
dog_list.append(convert_image_to_data(i))
In [488]:
y_cat = np.zeros(len(cat_list))
In [489]:
y_dog = np.ones(len(dog_list))
In [478]:
X = np.concatenate([cat_list, dog_list])
In [479]:
# Cast as float32 type
X = X.astype("float32")
In [480]:
X[:2]
Out[480]:
In [481]:
X.shape
Out[481]:
In [494]:
y = np.concatenate([y_cat, y_dog])
In [496]:
# cast as integer
y = y.astype("int")
In [497]:
y
Out[497]:
In [ ]:
In [499]:
X.shape
Out[499]:
In [500]:
y.shape
Out[500]:
In [581]:
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=1)
In [582]:
for train_index, test_index in sss.split(X, y):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
In [583]:
y_train
Out[583]:
In [584]:
y_test
Out[584]:
In [585]:
Counter(y_train), Counter(y_test)
Out[585]:
In [612]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1, stratify=y)
In [613]:
y_train, y_test
Out[613]:
In [614]:
Counter(y_train), Counter(y_test)
Out[614]:
In [622]:
model = Sequential()
# input: 100x100 images with 3 channels -> (3, 100, 100) tensors.
# this applies 32 convolution filters of size 3x3 each.
model.add(Convolution2D(32, 3, 3, border_mode='valid', input_shape=(3, 100, 100)))
model.add(Activation('relu'))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(64, 3, 3, border_mode='valid'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
# Note: Keras does automatic shape inference.
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy'])
In [623]:
model.fit(X_train, y_train, batch_size=32, nb_epoch=20)
Out[623]:
In [618]:
y_test_predict = model.predict(X_test)
In [621]:
accuracy_score(y_test, y_test_predict)
Out[621]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: