In [ ]:
import os
import ast
import glob
from scipy.misc import imread, imresize
import numpy as np
from sklearn.utils import shuffle
data_dir = '../data/dog_vs_cat'
def data_load(data_dir_path, img_height, img_width):
"""
:param data_dir_path: data home dir
:return: x, y x is image content, y is category name
"""
data_dir = data_dir_path
subdirs = [os.path.join(data_dir, x) for x in os.listdir(data_dir)]
x, y = [], []
for cls in subdirs:
imgs = glob.glob(os.path.join(cls, '*'))
tmp_y = os.path.split(cls)[-1]
for img_path in imgs:
x.append(imresize(imread(img_path), (img_height, img_width)))
y.append(tmp_y)
return shuffle(np.array(x), y)
org_x,org_y = data_load(data_dir,224,224)
print(org_x.shape,org_y[0])
from keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
encoder = LabelEncoder()
cate_y = to_categorical(encoder.fit_transform(org_y))
In [ ]:
from keras.applications import vgg16, vgg19
model = vgg16.VGG16(input_shape=(224,224,3),weights=None,classes=2)
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(org_x,cate_y,validation_split=0.2,verbose=1,epochs=10)
In [ ]:
model = vgg19.VGG19(input_shape=(224,224,3),weights=None,classes=2)
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(org_x,cate_y,validation_split=0.2,verbose=1,epochs=10)