swghosh / DeepFace

Keras implementation of the renowned publication "DeepFace: Closing the Gap to Human-Level Performance in Face Verification" by Taigman et al. Pre-trained weights on VGGFace2 dataset.
https://research.fb.com/publications/deepface-closing-the-gap-to-human-level-performance-in-face-verification/
MIT License
194 stars 61 forks source link

Face Verification using Pre-Trained Model #4

Closed ajinkya933 closed 4 years ago

ajinkya933 commented 4 years ago

My question is concerned about face verification testing after I have a model file.

I have downloaded VGGFace2 data set, and have manually added more images to its training set. The orignal dataset is of around 9,000 people. I have added more people and now I have 11,000 people.

I want to train this model of 11,000 people and use it for face verification . I am referring to issue #1 and took this code

# required imports
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from deepface import create_deepface
from deepface import Dataset

model = create_deepface()
model.load_weights('VGGFace2_DeepFace_weights_val-0.9034.h5')

# fine tune the deepface model for your dataset
# for fine tuning, remove last dense layer only; (as per paper)
# set all pretrained parameters to non trainable
# add a dense layer with dim = num_classes (your dataset)
# set dense layer to trainable and train the model

img = load_img(path)
img = img.resize((152,152))
img_array = img_to_array(img)
img_array = img_array.reshape((-1, 152, 152, 3))
res = model.predict(img_array)

Can this code be used for face verification in my case as I am adding more dataset to the training data itself? Also, how do I tell that 2 faces match, should I look for both of their res values?

ajinkya933 commented 4 years ago

I observed the output of res. and found out res is a list of lists :[[0.0, 0.0, ...... 0.0]] so I printed each item inside this list using:

l1=[]
for term in res:
    for item in term:
        l1.append(item)
print(l1)

output:

[0.0, 0.0, ... 1.0, 0.0, 0.0, ..., 2.244257e-26, 0.0, ..., 0.0, 0.0, 0.0]

These are the non zero terms appearing in later stages of this list :

1.0
2.244257e-26

How do I interpreat these terms? is there a way to find similarity between two images using these values.

swghosh commented 4 years ago

This issue is possibly a duplicate of Issue #1. Please give the following a read and let me know if there are further queries.

https://github.com/swghosh/DeepFace/issues/1#issuecomment-555747408

I'll share the code for finetuning the pre-trained network based on the needs that you have specified. As per the paper, the network is designed to carry out face recognition task specifically.

A face verification task can be carried using DeepFace feature vector extracted from particular images. Such a feature vector can be obtained from the second last dense layer of the model. The paper also discusses a face verification technique based on use of Chi² similarity distance based SVMs. (either can be used: fine-tuning the network for new classes / chi² kernel SVM for verification)

swghosh commented 4 years ago

Hi @ajinkya933, Any updates?

ajinkya933 commented 4 years ago

@swghosh I switched to facenet, as I needed face comparision and facenet was good at doing this