Last active
April 8, 2019 08:11
-
-
Save daniellerch/f61f1de3b0b26430c9b1bf2e087fa43e to your computer and use it in GitHub Desktop.
Keras/VGGFace
This file contains hidden or 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 sys | |
import numpy as np | |
from PIL import Image | |
from scipy import misc, ndimage | |
from keras import Model | |
from keras import Sequential | |
from keras.layers import Activation | |
from keras.layers import Dropout | |
from keras.layers import Flatten | |
from keras.layers import ZeroPadding2D | |
from keras.layers import Convolution2D | |
from keras.layers import MaxPooling2D | |
from keras.models import model_from_json | |
from keras.preprocessing.image import load_img | |
from keras.preprocessing.image import img_to_array | |
from keras.applications.imagenet_utils import preprocess_input | |
def preprocess_image(image_path): | |
img = load_img(image_path, target_size=(224, 224)) | |
img = img_to_array(img) | |
img = np.expand_dims(img, axis=0) | |
img = preprocess_input(img) | |
return img | |
def prepare_model(): | |
model = Sequential() | |
model.add(ZeroPadding2D((1,1),input_shape=(224,224, 3))) | |
model.add(Convolution2D(64, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(64, (3, 3), activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(128, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(128, (3, 3), activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(256, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(256, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(256, (3, 3), activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, (3, 3), activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, (3, 3), activation='relu')) | |
model.add(ZeroPadding2D((1,1))) | |
model.add(Convolution2D(512, (3, 3), activation='relu')) | |
model.add(MaxPooling2D((2,2), strides=(2,2))) | |
model.add(Convolution2D(4096, (7, 7), activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Convolution2D(4096, (1, 1), activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Convolution2D(2622, (1, 1))) | |
model.add(Flatten()) | |
model.add(Activation('softmax')) | |
model.load_weights('vgg_face_weights.h5') | |
vgg_face_descriptor = Model(inputs=model.layers[0].input, outputs=model.layers[-2].output) | |
return vgg_face_descriptor | |
def same_person(model, img1, img2): | |
img1_representation = model.predict(preprocess_image(img1))[0,:] | |
img2_representation = model.predict(preprocess_image(img2))[0,:] | |
a = np.matmul(np.transpose(img1_representation), img2_representation) | |
b = np.sum(np.multiply(img1_representation, img1_representation)) | |
c = np.sum(np.multiply(img2_representation, img2_representation)) | |
cosine_similarity = 1 - (a / (np.sqrt(b) * np.sqrt(c))) | |
print(cosine_similarity) | |
if cosine_similarity < 0.4: | |
return True | |
return False | |
model = prepare_model() | |
if same_person(model, 'bengio1.jpg', 'bengio2.jpg'): | |
print("Same person!") | |
else: | |
print(":(") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment