In [ ]:
import cv2
from sklearn.cluster import KMeans
from sklearn.svm import SVC
import numpy as np
from keras.datasets import cifar10
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.utils import np_utils
if __name__=="__main__":
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
sift = cv2.xfeatures2d.SIFT_create() #Uses SIFT to get descriptors
descritores = []
#Extract descritors for all imagens on the dataset
for i in np.arange(x_train.shape[0]):
_,imgDesc = sift.detectAndCompute(x_train[i],None)
if imgDesc is not None:
for j in np.arange(imgDesc.shape[0]):
descritores.append(imgDesc[j])
descritores = np.array(descritores)
print("Calculando centroides a partir dos descritores")
#Apply KMeans to descriptors list to generate 10 centroids corresponding to the 10 visual words
kmeans = KMeans(n_clusters=10).fit(descritores)
treino_hist_X=[]
treino_hist_Y=[]
print("Calculando histogramas para as imagens do treino")
#For each descriptor of an image, find the closest centroid
#This return a vector with the number of each centroid for each descriptor of an image
#Create an histogram out of acho vector for each imagem
#Associate a label for each histogram
for i in np.arange(x_train.shape[0]):
_,imgDesc = sift.detectAndCompute(x_train[i],None)
if imgDesc is not None:
kcent = kmeans.predict(imgDesc) #Calcula centroide pada cada descritor
hist,_ = np.histogram(kcent,bins=[0,1,2,3,4,5,6,7,8,9,10],normed=True)
treino_hist_X.append(hist)
treino_hist_Y.append(y_train[i])
treino_hist_X = np.array(treino_hist_X).reshape((-1,10))
treino_hist_Y = np.array(treino_hist_Y).reshape((-1,))
#Train SVM classifier with histogram and label pairs from the training set
modSVM = SVC()
modSVM.fit(treino_hist_X,treino_hist_Y.reshape((-1,)))
matriz_conf_teste = np.zeros((10,10))
#Aplly SVM on the test set
#Extract descriptor for each image
#Find closest centroid
#Create histogram for each image
#Predict category with the SVM classifiear
#Add error or hit on the confusion matrix
#Lines correspond to true labels and columns correspond to predictions from the SVM classifier
preds=[]
labels=[]
for i in np.arange(x_test.shape[0]):
_,imgDesc = sift.detectAndCompute(x_test[i],None)
if imgDesc is not None:
kcent = kmeans.predict(imgDesc) #Calcula centroide pada cada descritor
hist,_ = np.histogram(kcent,bins=[0,1,2,3,4,5,6,7,8,9,10],normed=True)
ret = modSVM.predict(hist.reshape((1,-1)))[0]
preds.append(ret)
labels.append(y_test[i,0])
real = y_test[i,0]
matriz_conf_teste[real,ret] = matriz_conf_teste[real,ret] + 1
print("accuracy is {0}".format(accuracy_score(labels,preds)))
plt.pcolor(matriz_conf_teste,cmap="jet")
plt.colorbar()
plt.title("Confusion Matrix - Test Set")
In [2]:
(x_train, y_train), (x_test, y_test) = cifar10.load_data() #utliza o dataset cifar10 do keras
nb_classes = 10
nb_epoch=200
batch_size=32
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
in_shape=x_train.shape[1:]
y_train = np_utils.to_categorical(y_train, nb_classes)
model = Sequential()
model.add(Conv2D(12, (5, 5), activation = 'relu', input_shape=in_shape, init='he_normal'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(25, (5, 5), activation = 'relu', init='he_normal'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Flatten the 3D output to 1D tensor for a fully connected layer to accept the input
model.add(Flatten())
model.add(Dense(180, activation = 'relu', init='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(100, activation = 'relu', init='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation = 'softmax', init='he_normal')) #Last layer with one output per class
model.compile(loss='categorical_crossentropy', optimizer='adamax', metrics=["accuracy"])
model.fit(x_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=2)
Out[2]:
In [59]:
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
resp = model.predict_classes(x_test)
respLab = []
for i in range(0,len(y_test)):
respLab.append(y_test[i][0])
mat_conf_teste = np.zeros((10,10))
print(" - Accuracy on test dataset {0}".format(accuracy_score(resp,respLab)))
for i in np.arange(resp.shape[0]):
mat_conf_teste[respLab[i],resp[i]] = mat_conf_teste[respLab[i],resp[i]] + 1
plt.pcolor(mat_conf_teste,cmap="jet")
plt.colorbar()
plt.title("Confusion matrix for the test set")
Out[59]:
In [71]:
resp = model.predict_classes(x_train)
respLab = []
for i in range(0,len(y_train)):
respLab.append(np.where(y_train[i] ==1)[0])
mat_conf_treino = np.zeros((10,10))
print("Accuracy on training dataset {0}".format(accuracy_score(respLab,resp)))
for i in np.arange(resp.shape[0]):
mat_conf_treino[respLab[i],resp[i]] = mat_conf_treino[respLab[i],resp[i]] + 1
plt.pcolor(mat_conf_treino,cmap="jet")
plt.colorbar()
plt.title("Confusion matrix for the training set")
Out[71]:
In [ ]: