- Perform Face Recognition with FaceNet on Avengers Chris Evans, Mark Ruffalo, Chris Hemsworth, Robert Downey Jr, Scarlett Johansson.
Hi everyone, let's walkthrough this project of Face Recognition.
- I'm sorry for not able to solve the problem persisting with Docker push and pull (Unsolved 'tensorflow.python.keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'), as I'm very new to Docker. Today only I have viewed some lectures to implement this.
- But, the code is all fine. And I will let you know what are the Dependencies required for this task and what steps we to follow to train our model and predict all the images in test set of
20%
volume.
- I have used pretrained FaceNet model (available at this link by Hiroki Taniai trained on MS-Celeb-1M dataset with model weights and architecture available at here). This model has input dimension of
160 * 160
, while the output is an embedding vector of size128
. - Used Triplet Loss as the loss function with
margin = 0.9
. - Except the last
Convolution2D
block, I freezed all the blocks while trainng so that the weights can get acclimatized to the new data through some fine changes in weights of top block where complex features are learned (also callled fine tuning). - Beforehand, we chose one image from each class as Anchor image and created their embeddings in
train.py
. This way we would prevent computing the embeddings of those images while performing face recognition of some test images. - While testing for the test set, we compute the euclidean distance of embedding vector of each test image from the embedding vectors we have in our Anchor dictionary. If the lowest distance among all those 5 classes is less than some threshold (in our case it is
threshold = 3.5
), then the class having the lowest distance from the given test embedding vector is assigned to that vector (or person's image).
pip install -r requirements.txt
- Please find the Dockerfile at here where everything along with a bash script, that run
train.py
andtest.py
sequentially, is being added to a docker image .
Disclaimer: But for some reason,
keras
is not installing properly for whichtrain.py
throws some error. But we can manually do it as follows.
- In
train.py
we have code to access GPU with90%
capacity as we want to preventOOM Error
. - Open your terminal in the current directory and type
python3 train.py
. Make sure that you have installed the requirements. - The terminal will prompt you to enter
batch_size
(take it 128) andno. of epochs
(take it to 20). - Now run
python3 test.py
and see the predictions for each test image with total accuracy at end.
helper_func.py
contains those functions that we use in bothtrain.py
andtest.py
.- labelled_pkl cotnains the preprocessed trainng data with their labels in class-wise fashion. If not available we can create it in our
train.py
at line 90 and save it at line 93. - train_test.txt contains the name of images that fall in training set and test set in
80:20
ratio. Also we can generate our own set of train and test just by uncommenting the code intrain.py
at line 83 and saving it using line 86. triplet_loss()
function with margin0.9
is responsible for our algorithmFaceNet
training.get_batch_random()
is again a nice function to generate triplets from the data (labelled_pkl).
See the below gif file to have a taste of our implementation.