AMB82 MobileFaceNet Convert To ONNX


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
cd MobileFaceNet
mkdir weights
cd weights

For better performance and compatibility, makers can convert .pt to .onnx with following example:

Code Reference

from mobilefacenet import MobileFaceNet
import torch
import time

if __name__ == '__main__':
    filename = 'weights/'
    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))

    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)

The converted onnx will be located at weights/MobileFaceNet.onnx.

Please confirm that QQ communication software is installed