This repo contains face_verify.py and app.py which is able to perform the following task -
- Detect faces from an image, video or in webcam and perform face recogntion.
- app.py was used to deploy the project.
- requirements.txt
- pretrained model IR-SE50 @ Onedrive or Mobilefacenet @ OneDrive.
- Custom dataset
- Newly Trained model (facebank.pth and names.npy)
After downloading the project first you have to install the following libraries.
You can install all the dependencies at once by running the following command from your terminal.
$ pip install -r requirements.txt
$ pip3 install torch===1.2.0 torchvision===0.4.0 -f https://download.pytorch.org/whl/torch_stable.html
Although i provided the pretrained model in the work_space/model and work_space/save folder, if you want to download the models you can follow the following url:
I have used the IR-SE50 as the pretrained model to train with my custom dataset. You need to copy the pretrained model and save it under the work_space/save folder as model_final.pth
In the data/facebank you will find a trained model named "facebank.pth" which contains the related weights and "names.npy" contains the corresponding labels of the users that are avialable in the facebank folder. For instance in this case the facebank folder will look like this :-
facebank/
---> Chandler
---> Joey
---> Monica
---> Phoebe
---> Pias
---> Rachel
---> Raihan
---> Ross
---> Samiur
---> Shakil
---> facebank.pth
---> names.npy
If you have the "facebank.pth" and "names.npy" files in the data/facebank you can execute the following command to see the demo.
$ python app.py
and go to the following url from your web browser.
http://localhost:5000
Note: If you want to run the inference on a video, download a video of related persons (Person that you trained the model with) and replace 0 in the line number 43 of face_verify.py with the path of your video file. For this code you can run the inference on any video of Friends tv series.
First organize your images within the following manner-
data/
raw/
name1/
photo1.jpg
photo2.jpg
...
name2/
photo1.jpg
photo2.jpg
...
.....
now run the following command
$ python .\create-dataset\align_dataset_mtcnn.py data/raw/ data/processed --image_size 112
You will see a new folder inside the data directory named "processed" which will hold all the images that contains only faces of each user. If more than 1 image appears in any folder for a person, average embedding will be calculated.
After executing the script new images for each user in the processed folder will look something like this.
Copy all the folders of the users under the data/processed folder and paste in the data/facebank folder.
Now to train with your dataset, you need to set args.update == True in line 35 of face_verify.py . After training you will get a new facebank.pth and names.npy in your data/facebank folder which will now only holds the weights and labels of your newly trained dataset. Once the training is done you need to reset args.update==False. However, if this doesn't work change the code in following manner-
if args.update:
targets, names = prepare_facebank(conf, learner.model, mtcnn, tta = args.tta)
print('facebank updated')
else:
targets, names = load_facebank(conf)
print('facebank loaded')
Only keep the follwing lines for training, once the training is done just replace it with the old code.
targets, names = prepare_facebank(conf, learner.model, mtcnn, tta = args.tta)
print('facebank updated')
Or you can simply pass a command line arguement such as below if there is new data to train.
$python face_verify.py -u
Here the -u parse the command to update the facebank.pth and names.npy.
Now you are ready to test the systen with your newly trained users by running-
$ python app.py
Note: You can train with custom dataset as many time as you want, you will only require any of the pre-trained model to train with your custom dataset to generate the facebank.pth and names.npy. Once you get this two files you are ready to test the face recogniton.
If you want to build a new pre-trained model like IR-SE50 @ Onedrive and reproduce the result, you will need the large files which contains several dataset of faces under the data/faces_emore .
To get these files, first you need to download the MS1M dataset from any of the following url-
After unzipping the downloaded file execute the following command. It will take few hours depending on your system configuration.
$ python prepare_data.py
After that you will see the following files to the "data/faces_emore" folder.
faces_emore/
---> agedb_30
---> calfw
---> cfp_ff
---> cfp_fp
---> cfp_fp
---> cplfw
---> imgs
---> lfw
---> vgg2_fp
To know the further training procedure you can see the details in this InsightFace_Pytorch repository.