question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mxnet --> onnx converted insightface arcface model generates incorrect inference results

See original GitHub issue

I am trying to convert the Insightface arcface LResNet100E-IR,ArcFace@ms1m-refine-v2 mxnet model to work with onnx.

Based on this issue, it looks like mxnet only supports up to onnx v1.3.0. I am therefore using the following library verions:

mxnet==1.7.0.post1
onnx==1.3.0
onnxruntime==1.6.0

It looks like out of the box the arcface model is not support by onnx, but I came across this issue which which links a script for properly converting the arcface model to onnx.

Running the script converts the model from mxnet format to onnx format successfully, however the output is no longer correct. Here is my mxnet script I use for running inference (using a pre-aligned face chip):

#############################
# Inference with mxnet
#############################
import numpy as np
import cv2
import mxnet as mx
from collections import namedtuple

import pkg_resources
print("mxnet version:", pkg_resources.get_distribution("mxnet").version)

Batch = namedtuple('Batch', ['data'])
def getEmbedding(face_chip):
    face_chip = np.swapaxes(face_chip, 0, 2)
    face_chip = np.swapaxes(face_chip, 1, 2)
    face_chip = face_chip[np.newaxis, :]
    array = mx.nd.array(face_chip)
    mod.forward(Batch([array]))
    out = mod.get_outputs()[0].asnumpy()
    return out


prefix = "./models/insightface/model"
sym, arg, aux = mx.model.load_checkpoint(prefix, 0)
ctx = mx.cpu()
mod = mx.mod.Module(symbol=sym, context=ctx, label_names=None)
mod.bind(for_training=False, data_shapes=[('data', (1,3,112,112))])
mod.set_params(arg, aux)

img = cv2.imread("face_chip.jpg")

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)


res = getEmbedding(img)
print(res[0][0])

I print the first element of the feature vector, which is: 2.216425

Now here is my script for running inference with onnxruntime:

#############################
# Inference with onnx runtime
#############################
import json
import numpy as np
import cv2
import onnx
import onnxruntime
from onnx import numpy_helper

import pkg_resources
print("onnx-runtime version:", pkg_resources.get_distribution("onnxruntime").version)
print("onnx version:", pkg_resources.get_distribution("onnx").version)


model="./models/insightface/arcface_r100.onnx"
 
#Preprocess the image
img = cv2.imread("face_chip.jpg")

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img.resize((1, 3, 112, 112))
data = json.dumps({'data': img.tolist()})
data = np.array(json.loads(data)['data']).astype('float32')
 
session = onnxruntime.InferenceSession(model, None)
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
 
result = session.run([output_name], {input_name: data})
print(result[0][0][0])

Again, I print the first element of the feature vector, which is now: -0.19618489.

This is clearly not correct. What have I done wrong? Has the conversion failed?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
SthPhoenixcommented, Mar 8, 2021

@cyrusbehr it looks like you are using different code for image preprocessing for mxnet version and ONNX version. Try using same preprocessing in both cases, something like this:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.transpose(img, (2, 0, 1))
img = np.expand_dims(img, axis=0)
img = img.astype(np.float32)

You can also check my scripts here: insight_tester.py But don’t forget to change ‘trt’ backend to ‘onnx’

2reactions
eericcommented, Aug 7, 2021

图像张量不减均值除方差吗?

That’s true for newer models trained with pytorch

no pytorch, while tensor was minus and mul in mxnet, because model-symbol.json of mxnet was marked as below:

{
  "op": "_minus_scalar", 
  "name": "_minusscalar0", 
  "attrs": {"scalar": "127.5"}, 
  "inputs": [[1, 0, 0]]
}, 
{
  "op": "_mul_scalar", 
  "name": "_mulscalar0", 
  "attrs": {"scalar": "0.0078125"}, 
  "inputs": [[2, 0, 0]]
},
Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras_insightface TensorFlow Model - Model Zoo
This is the keras implementation of deepinsight/insightface, and is released under the MIT License. There is no limitation for both academic and commercial ......
Read more >
Building A Face Recognition System Using Scikit Learn In ...
InsightFace is an open-sourced deep face analysis model for face recognition, face detection and face align-ment tasks. 4. Comparing the ...
Read more >
deepinsight - Bountysource
Created 1 year ago in deepinsight/insightface with 15 comments. Hi! I'm testing your new SCRFD face detector and have noticed some issues with...
Read more >
insightface - PyPI
For ``insightface<=0.1.5``, we use MXNet as inference backend. Starting from insightface>=0.2, we use onnxruntime as inference backend.
Read more >
Untitled
ONNX Runtime can be used with models from PyTorch, Tensorflow/Keras, ... people who use PyTorch Insightface Pytorch Face Recognition Project on MXNet Face ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found