This repository provides two equivalent scripts to convert the VGG-Face model into a truncated version saved in the ONNX format. This conversion is useful for optimizing face representation tasks (verification). You can choose to run the script either as a Python file for local execution or as a Jupyter Notebook for interactive use.
To run the scripts, you need to have the following installed:
- Python (3.6 or later)
- TensorFlow (version 2.x preferred)
- tf2onnx (for converting the model to ONNX format)
You can install the required Python packages with the following command:
pip install -U tensorflow tf2onnx
-
Download the VGG-Face Weights:
Download the pre-trained VGG-Face model weights using the command:
wget https://github.com/serengil/deepface_models/releases/download/v1.0/vgg_face_weights.h5
-
Run the Script:
There are two options to run the script, providing flexibility based on your environment:
-
Python Script: Execute the
model_generator_vgg_face_to_onnx_truncated.py
file in your local Python environment:python model_generator_vgg_face_to_onnx_truncated.py
-
Jupyter Notebook: Open and run the
Model_Generator_VGG_Face_to_ONNX_TRUNCATED.ipynb
file using Jupyter Notebook for an interactive experience.
-
-
Convert Model to ONNX:
To convert the model to the ONNX format, use the following command (assumed to be handled within the scripts):
python -m tf2onnx.convert --saved-model saved_vgg_face_model_truncated --output saved_vgg_face_model_truncated.onnx --opset 13
-
Saving the Model:
After conversion, you will have the generated
saved_vgg_face_model_truncated.onnx
file.
The VGG-Face model architecture is defined as a Sequential model in TensorFlow. The model is originally designed to classify 2622 identities. The conversion process truncates the model to output embeddings instead of classifications.
- Convolutional Layers: The model includes several convolutional layers with
ReLU
activations. - Pooling Layers: Max-pooling layers reduce spatial dimensions.
- Dense Layers: Fully connected layers are followed by dropout for regularization.
The truncated model outputs embeddings from the second-to-last layer, bypassing the final softmax layer used for classification.
Model_Generator_VGG_Face_to_ONNX_TRUNCATED.ipynb
: Jupyter Notebook for interactive execution.model_generator_vgg_face_to_onnx_truncated.py
: Python script for local execution.
- VGG-Face: GitHub Repository
- TensorFlow: Official Site
- tf2onnx: GitHub Repository
This project is open-source and available under the MIT License.
For more information, refer to the original implementation by 2060.io and their open-source tools for building verifiable credential-based services.