-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Face Preparation for Model Prediction #1
Comments
You need to use the # 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) If there are any issues regarding the fine tuning step, please do let me know. |
For training the DeepFace network, 2D aligned (2D frontalisation using 5 fiducial points similar to the one specified in paper) images from the VGGFace2 dataset have been used. It is expected that we use the same alignment technique (2D alignment of tight face crops) to make accurate predictions when using DeepFace feature vectors. Shortly, will update the repository with the code required to obtain 2D aligned tight face crops from images in dataset. 2D-alignment in our case had been achieved using Currently, I'm working on the 3D frontalisation step to replicate the exact 3D alignment step as specified in paper. Hope to add include those changes and train the network using 3D frontalised images soon. |
Thank you 👍 Now I have code like this: from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
import numpy as np
import dlib;
from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor('shape_predictor_5_face_landmarks.dat')
def processdeepface(path):
img = dlib.load_rgb_image(path)
dets = detector(img, 1)
for k, d in enumerate(dets):
shape = sp(img, d)
face_chip = dlib.get_face_chip(img, shape, size=152)
image = preprocess_image(face_chip)
img_array = img_to_array(image)
img_array = img_array.reshape((1, IMAGE_SIZE[0], IMAGE_SIZE[1], 3))
res = model.predict(img_array) Am I doing it in proper way? Thanks |
The code for the 2D face alignment seems fine. The original implementation (which actually represents the pretrained weights) makes use of a softmax classifier trained on 8631 classes from the VGGFace2 dataset. So, it is unlikely to make predictions the same class label unless the face crop being used belongs to one of those classes. DeepFace doesn't use a triplet loss based implementation unlike FaceNet by Scroff et al. which can be directly used for a face verification task. Instead, the DeepFace feature vector obtained from the second last dense layer from the network can be effectively used to learn a face verification problem. |
@swghosh @lukkowal5 Hi,how can i process the 8631 output to a image? Thanks. |
@ZHUANG-JLU Sorry for the delay in reply. |
@ZHUANG-JLU, Is it a face verification task (relating to matching a pair of images whether they belong to same person or not) or a face recognition task (relating to a multi class classification problem with some k number of people/identities in dataset)?
Please confirm about the same then I can get back to you with some code that can perform either based on your requirement. |
Face Detection/Recognition/Alignments.Thanks for your reply. |
@ZHUANG-JLU, |
I will see it.Thanks. |
Hey there, I am new in this field can you tell me how to run this code. |
Also is there any way I can run it on my custom dataset. |
Hi @thepranaygaur, Can you refer to the notebook provided in the link to run the face recognition model on a custom dataset? Assuming that you've a directory containing pictures of faces of people (tight face crops, aligned) segregated into sub directories basis their class labels / identity of the person, you can use the notebook for end to end code. Do let me know if you have any queries. For face detection and alignment please use |
Hello, thanks for your work first. |
Hi @FunkyKoki, thanks for reaching out! In my repository, the DeepFace model which have been trained on VGGFace2 dataset uses tight face crops with 2D alignment only (atleast, as of now) using The methodology discussed uses 3D to 2D mapping of face keypoints with the help of an affine camera (not really sure how that can be done with regular 2D face crops) and a trained 3D face model. As a starting point, I'd recommend you to get started with these resources which should help you develop more clarity on this topic.
Let me know if you are willing to collaborate with me on this at a suitable time / looking forward to contributing some code that is related to 3D face frontalization and related model training. Will be more than happy to be of any help. Good luck! |
Thank you so much for your sharing. I would check the resources you shared and try to re-implement the whole process. By the way, actually, in the modern network, the strong 3D alignment seems not to be a necessity. I am interested in the 3D alignment only because that I am working in the 3D reconstruction of face. I would let you know when I solve the two questions above. Thanks again. |
@swghosh Hello, I'm trying to use https://github.com/serengil/deepface deepFace facial attribute analysis, but the performance toward certain age group is bad. So I'm looking for a way to train/ tune the age/gender/race classifier with my own dataset. I'm wondering if this is possible to do? I saw this repo is using VGG face for pretrain weight. Can I retrain with my dataset? May you give me some insight? Thank you! |
Hey @zzzzmoya, Unfortunately a no to using those weights for facial attribute analysis as I'm not sure if it's a great fit with the existing weights ATM. (as the weights are classification task specific; also I'm not sure about the effects of using the DeepFace architecture for such tasks)
|
Hey @swghosh, awesome post! I have a question: Could the classification task be repurposed to say facial analysis? Eg: Using the same data hierarchical structure, what if instead of matching (one to many) subjects, the labels would be race-ethnicity identification where folders would be labeled into a limited number of categories? From your reply to @ZHUANG-JLU, I'm thinking of creating a new F8 layer with the number of categories for my task and train the model first as a classifier (i.e. training just the F8 layer) using VGGFace2 pre-trained weights and then fine-tuning setting a portion of the DeepFace model to trainable. Curious to know your thoughts? Would you by any chance have any sample code for creating an additional F8 layer? Thanks! |
|
hi , |
Hi @saidafef: Apologies for the delayed response. You could probably check for any red flags in any of the following aspects:
From my personal standpoint I have been able to achieve 95+% accuracy on the test by training it on a custom face dataset created from scratch, with very different set of occlusions on the train and testing sets. (training set had very less variety in terms of occlusions/protrusions whereas the test set had a lot of them and were quite different in terms of lighting as well; suprisingly the features transfer learned from DeepFace were able to capture correlation in facial structure giving good results even on a very different test set) Feel free to ping with any additional details. |
Hi @swghosh , I am looking for a notebook of face verification problem (given two images identify if they are of the same individual or of different people). Do you have a similar implementation like this (https://colab.research.google.com/drive/1otl-4tJaCfyCJF33bvnjzeuUdldcWysA) for my case. We want to fine tune the model to identify images of same people in a given dataset. |
Hi,
How can I read/align/prepare face for making predict on trained model?
I made code like this:
I get different results for every face belonging to the same person.
Thanks a lot for a help
The text was updated successfully, but these errors were encountered: