AMB82 MobileFaceNet Convert To ONNX
MobileFaceNet
MobileFaceNet is an efficient Convolutional Neural Network (CNN) model and it uses more than 1 million parameters.
MobileFaceNet is used for feature extractions. Since MobileFaceNet is one of the types of light weights models, we can apply this face recognition system on mobile and embedded devices.
We can download the source code and pretrained model from GitHub:
git clone https://github.com/foamliu/MobileFaceNet.git.
cd MobileFaceNet
mkdir weights
cd weights
wget https://github.com/foamliu/MobileFaceNet/releases/download/v1.0/mobilefacenet.pt
For better performance and compatibility, makers can convert .pt to .onnx with following example:
convert2onnx.py:
Code Reference
from mobilefacenet import MobileFaceNet import torch import time if __name__ == '__main__': filename = 'weights/mobilefacenet.pt' print('loading {}...'.format(filename)) start = time.time() model = MobileFaceNet() model.load_state_dict(torch.load(filename, map_location=torch.device('cpu'))) print('elapsed {} sec'.format(time.time() - start)) print(model) output_onnx = 'weights/MobileFaceNet.onnx' print("==> Exporting model to ONNX format at '{}'".format(output_onnx)) input_names = ["input0"] output_names = ["output0"] inputs = torch.randn(1, 3, 112, 112) torch_out = torch.onnx._export(model, inputs, output_onnx, export_params=True, verbose=False, input_names=input_names, output_names=output_names, opset_version=10)
python3 convert2onnx.py
The converted onnx will be located at weights/MobileFaceNet.onnx.