Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Car detector by pre-trained ResNet50
# 1. Import library
import os
import h5py
import numpy as np
import tensorflow as tf
import time
import glob
import json
import csv
from PIL import Image
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
import keras
from keras import applications
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D,Cropping2D
from keras.layers import Activation, Dropout, Flatten, Dense, Lambda, GlobalAveragePooling2D
from keras.utils.np_utils import to_categorical
from keras.models import Model
#number of class
n=4
# 2.Prepare dataset
# load training data of A
images = glob.glob('data/train/LEX/*')
trainA=[]
for image in images:
trainA.append(image)
# Read A Images
trainAim = []
from keras.preprocessing import image
for images in trainA:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
trainAim.append(x)
print(np.shape(trainAim))
# load training data of B
images = glob.glob('data/train/GTR/*')
trainB=[]
for image in images:
trainB.append(image)
# Read B Images
trainBim = []
from keras.preprocessing import image
for images in trainB:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
trainBim.append(x)
print(np.shape(trainBim))
# load training data of C
images = glob.glob('data/train/POR/*')
trainC=[]
for image in images:
trainC.append(image)
# Read C Images
trainCim = []
from keras.preprocessing import image
for images in trainC:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
trainCim.append(x)
print(np.shape(trainCim))
# load training data of D
images = glob.glob('data/train/LAM/*')
trainD=[]
for image in images:
trainD.append(image)
# Read D Images
trainDim = []
from keras.preprocessing import image
for images in trainD:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
trainDim.append(x)
print(np.shape(trainDim))
# load validation data of A
images = glob.glob('data/validation/LEX/*')
validationA=[]
for image in images:
validationA.append(image)
# Read A Images
validationAim = []
from keras.preprocessing import image
for images in validationA:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
validationAim.append(x)
print(np.shape(validationAim))
# load validation data of B
images = glob.glob('data/validation/GTR/*')
validationB=[]
for image in images:
validationB.append(image)
# Read B Images
validationBim = []
from keras.preprocessing import image
for images in validationB:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
validationBim.append(x)
print(np.shape(validationBim))
# load validation data of C
images = glob.glob('data/validation/POR/*')
validationC=[]
for image in images:
validationC.append(image)
# Read C Images
validationCim = []
from keras.preprocessing import image
for images in validationC:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
validationCim.append(x)
print(np.shape(validationCim))
# load validation data of D
images = glob.glob('data/validation/LAM/*')
validationD=[]
for image in images:
validationD.append(image)
# Read D Images
validationDim = []
from keras.preprocessing import image
for images in validationD:
img = image.load_img(images, target_size=(224, 224))
x = image.img_to_array(img)
validationDim.append(x)
print(np.shape(validationDim))
#Create train data
X_train = np.vstack((trainAim,trainBim,trainCim,trainDim))
X_test= np.vstack((validationAim,validationBim,validationCim,validationDim))
#Create label data
y_train = np.hstack( (np.zeros(len(X_train)/n), np.ones(len(X_train)/n), np.ones(len(X_train)/n)*2, np.ones(len(X_train)/n)*3) )
y_test = np.hstack( (np.zeros(len(X_test)/n), np.ones(len(X_test)/n), np.ones(len(X_test)/n)*2, np.ones(len(X_test)/n)*3) )
# Make categorical
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# 3.Extract features by ResNet50 and train FC model
# create the base pre-trained model
base_model = ResNet50(weights='imagenet', include_top=False)
epoch=20
t=time.time()
# add a global spatial average pooling layer
x = base_model.output #4d
print(np.shape(x))
x = GlobalAveragePooling2D()(x) #2d
print(np.shape(x))
# add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# we have n classes
predictions = Dense(n, activation='softmax')(x)
# this is the model we will train
model = Model(input=base_model.input, output=predictions)
# first: train only the top layers (which were randomly initialized, i.e. freeze all convolutional ResNet50 layers
for layer in base_model.layers:
layer.trainable = False
# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
# train the model on the new data
history=model.fit(X_train, y_train, validation_data=[X_test, y_test] , epochs=epoch, batch_size=32, shuffle=True)
t2 = time.time()
print(round(t2-t, 5), 'Seconds to predict')
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend(['loss', 'val_loss'], loc='upper right')
plt.show()
# The idea of these code is inspired by this awesome work
# "Usage examples for image classification models" (by Mr.François Chollet)
# https://keras.io/applications/#resnet50
# TOSHI STATS SDN. BHD. and I do not accept any responsibility or liability for loss or damage occasioned to any person or property through using materials, instructions, methods, algorithm or ideas contained herein, or acting or refraining from acting as a result of such use. TOSHI STATS SDN. BHD. and I expressly disclaim all implied warranties, including merchantability or fitness for any particular purpose. There will be no duty on TOSHI STATS SDN. BHD. and me to correct any errors or defects in the codes and the software.
Owner

TOSHISTATS commented May 24, 2017 edited

Object of this test

There are 80 images like images above. There are four classes in it. 1.Lexus, 2.GTR, 3.Porsche, 4. Lamborghini. Each class has 20 images. So let us classify them into one of the classes automatically by deep learning. So It is a classification problem "one out of four"

Solution

Based on pre-trained ResNet50, I create a car detector model with small data set. There is four kind of classes in training data. Each class has 100 images for training and 20 images for validation. I achieve over 90% accuracy rate for validation data. It takes around 120 minutes for training by MAC Air 13.

res 0 91

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment