In [1]:
import os
import cv2
from time import time
import pandas as pd
import numpy as np
from random import shuffle
from sklearn.utils import shuffle
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.preprocessing import scale
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score,confusion_matrix,silhouette_score, homogeneity_score,adjusted_mutual_info_score,completeness_score,v_measure_score,adjusted_rand_score
from sklearn.model_selection import train_test_split
from matplotlib import pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages as pdf
%matplotlib inline
np.random.seed(123) # for reproducibility
import theano
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras.datasets import mnist
In [2]:
data_dir='/Users/gowthamkommineni/Desktop/ML/project/101_ObjectCategories' #Set Directory
IMG_Size =50
In [3]:
def create_categories():
train=[]
b=[]
i=0
for categ in os.listdir(data_dir):
path = os.path.join(data_dir,categ)
i=i+1
for img in (os.listdir(path)):
path2 = os.path.join(path,img)
if (os.path.exists(path2)):
gray = cv2.imread(path2,cv2.IMREAD_GRAYSCALE)
img = cv2.equalizeHist(gray)
imgb = cv2.GaussianBlur(img,(5,5),0)
imgb=cv2.resize(imgb,(50,50))
train.append(imgb)
b.append(i)
return train,b
In [4]:
a,b=create_categories()
a=np.array(a)
b=np.array(b)
In [5]:
a,b=shuffle(a,b)
a.shape
Out[5]:
In [6]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(a, b, test_size=0.33, random_state=42)
In [7]:
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
In [9]:
max(b)
Out[9]:
In [10]:
X_train.shape[0]
Out[10]:
In [11]:
X_train[0]
plt.imshow(a[0])
Out[11]:
In [12]:
X_train = X_train.reshape(X_train.shape[0], 1, 50, 50)
X_test = X_test.reshape(X_test.shape[0], 1, 50, 50)
print(X_train.shape)
In [13]:
X_train = X_train.reshape(X_train.shape[0], 1, 50, 50)
X_test = X_test.reshape(X_test.shape[0], 1, 50, 50)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
Y_train = np_utils.to_categorical(y_train)
Y_test = np_utils.to_categorical(y_test)
In [14]:
Y_test.shape
Out[14]:
In [15]:
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,50,50),dim_ordering='th'))
print(model.output_shape)
In [16]:
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(103, activation='softmax'))
In [17]:
model = Sequential()
model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1,50,50),dim_ordering='th'))
model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(103, activation='softmax'))
In [18]:
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
In [27]:
model.fit(X_train, Y_train, batch_size=10, nb_epoch=10, verbose=1)
score = model.evaluate(X_test, Y_test, verbose=0)
In [28]:
score
Out[28]:
In [29]:
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))