DeepFace Integration Tutorial#
In this tutorial we use the MXA to accelerate a popular face recognition app
DeepFace
!
Note
This application assumes the MXA driver/runtimes/compilers have been successfully installed.
Install Deepface#
Clone and install DeepFace repository:
git clone https://github.com/serengil/deepface.git
cd deepface
pip install -e .
Next we will download some sample images to use for verification. Here are some
sample images of Brad and George <3. Click to download db
and
unzip.
unzip db.zip
Next lets write a (tiny) script and run face verification! Open run.py
and write the following:
from deepface import DeepFace
MODEL_NAME = 'ArcFaceMX'
def verify(img1_path, img2_path):
# Run face recognition between the two images
results = DeepFace.verify(img1_path, img2_path, model_name=MODEL_NAME)
# Print the result
qualifier = " " if results['verified'] else " do not "
print(f'{img1_path} and {img2_path}{qualifier}contain the same person')
def main():
# Set both images to brad.
verify('db/brad/brad_0.jpg', 'db/brad/brad_1.jpg')
# Use deepface to verify the two images
verify('db/george/george_0.jpg', 'db/brad/brad_1.jpg')
main()
Run the script and it will print the following results:
python3 run.py
db/brad/brad_0.jpg and db/brad/brad_1.jpg contain the same person
db/george/george_0.jpg and db/brad/brad_1.jpg do not contain the same person
This script can distiguish Brad from George!
Accelerate#
In the next step we will accelerate face_recognition with the MXA! This involes two steps:
compile a model
integrate the runtime into the application.
Compile a model#
For simplicty, lets export the ArcFace model built into this app and compile it. Download and export the model:
python3 -c "DeepFace.build_model('ArcFace').model.save('ArcFace.h5')"
Compile the model using the CLI NeuralCompiler tool (mix_nc
).
mix_nc -v -m ArcFace.h5
The compiler will create ArcFace.dfp
to be used with the accelerator.
Integrate Runtime#
Now we have to integrate the MemryX runtime into this application. DeepFace provides a very nice API to easily add new models, we will leverage this to create a new facial recognition ‘Client’ which will target the MXA to perform inference.
Important
We are currently integrating with a non-streaming application that doesn’t rely on a camera feed or data stream. However, for streaming applications, it is strongly recommended to use the AsyncAccl API or Multi-threading. For additional details, refer to the Inference Tutorials.
In deepface/models/facial_recognition/ArcFace.py
add the following code
from typing import Any, Union, List, Tuple
import numpy as np
import memryx as mx
class ArcFaceMXClient(FacialRecognition):
"""
ArcFace running on the MXA
"""
def __init__(self):
self.accl = mx.SyncAccl('ArcFace.dfp')
self.model_name = "ArcFace"
self.input_shape = (112, 112)
self.output_shape = 512
def forward(self, img: np.ndarray) -> List[float]:
# Reshape inputs and Perform inference
ifmap = np.squeeze(img)[:,:,None,:]
outputs = accl.run(img)
return np.squeeze(outputs).tolist()
Next, lets register this model in the deepface/modules/modeling.py
. Find the following lines and add an entry for the MXClient.
models = {
"facial_recognition": {
"VGG-Face": VGGFace.VggFaceClient,
...
"ArcFaceMX": ArcFace.ArcFaceMXClient, # Add this line
...
}
Finally, in the run.py script change the MODEL_NAME
to use the accelerated ArcFace.
MODEL_NAME = 'ArcFaceMX'
Rerun the script.
python3 run.py
db/brad/brad_0.jpg and db/brad/brad_1.jpg contain the same person
db/george/george_0.jpg and db/brad/brad_1.jpg do not contain the same person
This script can distiguish Brad from George… but faster!
Final Thoughts#
This example shows the ease of integration using the MemryX SDK using simple CLI tools and runtime APIs.