Created
April 11, 2019 17:16
-
-
Save gaurav-gogia/d7336ce4c910a35461ef600ec2d384d1 to your computer and use it in GitHub Desktop.
vggface attempt at face recognition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import keras | |
import numpy as np | |
from PIL import Image | |
import matplotlib.pyplot as plt | |
from os.path import isfile, join | |
from sklearn.decomposition import PCA | |
from keras.optimizers import Adam, SGD | |
from keras.layers import ZeroPadding2D | |
from keras.layers import Conv2D, MaxPool2D | |
from keras.activations import relu, softmax, sigmoid | |
from keras.models import Sequential, Model, model_from_json | |
from keras.layers import Flatten, Dense, Dropout, Activation | |
from keras.preprocessing.image import load_img, img_to_array | |
from keras.losses import categorical_crossentropy, binary_crossentropy | |
epsilon = 0.25 | |
imgsize = 350 | |
trainpath = "../ssd/gaurav_train/gaurav/" | |
testpath = "../ssd/gaurav_test/gaurav/" | |
weights_path = "../data/vgg_face_weights.h5" | |
def prepocess_image(path): | |
img = load_img(path, target_size=(imgsize, imgsize)) | |
img = img_to_array(img) | |
img = np.expand_dims(img, axis=0) | |
return img | |
def cosine_similarity(src_rep, dst_rep): | |
a = np.matmul(np.transpose(src_rep), dst_rep) | |
b = np.sum(np.multiply(src_rep, src_rep)) | |
c = np.sum(np.multiply(dst_rep, dst_rep)) | |
return 1 - (a / (np.sqrt(b) * np.sqrt(c))) | |
model = Sequential() | |
model.add(ZeroPadding2D((1, 1), input_shape=(imgsize, imgsize, 3))) | |
model.add(Conv2D(64, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(64, (3, 3), activation=relu)) | |
model.add(MaxPool2D((2, 2), strides=(2, 2))) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(128, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(128, (3, 3), activation=relu)) | |
model.add(MaxPool2D((2, 2), strides=(2, 2))) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(256, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(256, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(256, (3, 3), activation=relu)) | |
model.add(MaxPool2D((2, 2), strides=(2, 2))) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(512, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(512, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(512, (3, 3), activation=relu)) | |
model.add(MaxPool2D((2, 2), strides=(2, 2))) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(512, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(512, (3, 3), activation=relu)) | |
model.add(ZeroPadding2D((1, 1))) | |
model.add(Conv2D(512, (3, 3), activation=relu)) | |
model.add(MaxPool2D((2, 2), strides=(2, 2))) | |
model.add(Conv2D(4096, (7, 7), activation=relu)) | |
model.add(Dropout(0.5)) | |
model.add(Conv2D(4096, (1, 1), activation=relu)) | |
model.add(Dropout(0.5)) | |
model.add(Conv2D(2622, (1, 1), activation=relu)) | |
model.add(Flatten()) | |
model.add(Activation(softmax)) | |
model.load_weights(weights_path) | |
vgg_face_descriptor = Model( | |
inputs=model.layers[0].input, outputs=model.layers[-2].output) | |
def verify(src, dst): | |
src_rep = vgg_face_descriptor.predict(prepocess_image(src))[0, :] | |
dst_rep = vgg_face_descriptor.predict(prepocess_image(dst))[0, :] | |
similar = cosine_similarity(src_rep, dst_rep) | |
""" | |
f = plt.figure() | |
f.add_subplot(1, 2, 1) | |
plt.imshow(load_img(src)) | |
plt.xticks([]) | |
plt.yticks([]) | |
f.add_subplot(1, 2, 2) | |
plt.imshow(load_img(dst)) | |
plt.xticks([]) | |
plt.yticks([]) | |
""" | |
print('Source: ', src) | |
print('Testing: ', dst) | |
print('Cosine Similarity: ', similar) | |
if similar < epsilon: | |
text = 'Gaurav FOUND!' | |
plt.title(text) | |
print(text) | |
else: | |
text = 'Where is Waldo? :/' | |
plt.title(text) | |
print('Where is Waldo? :/') | |
# plt.show(block=True) | |
print('----------------------------------------------------------------------------------------') | |
print() | |
print() | |
print() | |
print() | |
verify(trainpath+'gogo1.jpg', testpath+'gaurav.jpg') | |
verify(trainpath+'gogo1.jpg', testpath+'gav3.jpg') | |
verify(trainpath+'gogo1.jpg', testpath+'gav4.jpg') | |
verify(trainpath+'gogo1.jpg', testpath+'roma.jpg') | |
#verify(trainpath+'gogo1.jpg', testpath+'tar2.jpeg') | |
verify(trainpath+'gogo1.jpg', testpath+'tarang.jpg') | |
""" | |
i = 1 | |
while i <= 6: | |
print('PASS: ', str(i)) | |
path = trainpath+'gogo'+str(i)+'.jpg' | |
verify(path, testpath+'gaurav.jpg') | |
verify(path, testpath+'gav3.jpg') | |
verify(path, testpath+'gav4.jpg') | |
verify(path, testpath+'roma.jpg') | |
verify(path, testpath+'tar2.jpeg') | |
verify(path, testpath+'tarang.jpg') | |
i += 1 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment