You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2019/03/06 10:15:16 UTC

[GitHub] [incubator-mxnet] XinyuDu opened a new issue #14345: Output inconsistency between mxnet model and converted onnx model

XinyuDu opened a new issue #14345: Output inconsistency between mxnet model and converted onnx model
URL: https://github.com/apache/incubator-mxnet/issues/14345
 
 
   ## Description
   The inference result of a converted onnx model is not identical with the original mxnet model.
   
   ## Environment info 
   Ubuntu 18.04
   python 3.6
   mxnet 1.3.1
   onnx 1.2.1
   
   ## Mxnet model
   The light weight (1M) gender-age mxnet model can be download [here](https://github.com/deepinsight/insightface/tree/master/gender-age/model)
   
   ## Converting script
   The mxnet model has been successfully converted to onnx model.
   The script for converting a mxnet model to onnx model is:
   ```
   import mxnet as mx
   import numpy as np
   from mxnet.contrib import onnx as onnx_mxnet
   
   sym = './model-symbol.json'
   params = './model-0000.params'
   
   input_shape = (1,3,112,112)
   # Path of the output file
   onnx_file = './mxnet_exported_ga1m.onnx'
   
   # Invoke export model API. It returns path of the converted onnx model
   converted_model_path = onnx_mxnet.export_model(sym, params, [input_shape], np.float32, onnx_file)
   
   print(converted_model_path)
   ```
   
   ## Inference script
   #mxnet model script
   ```
   import numpy as np
   import mxnet as mx
   from PIL import Image
   import cv2
   
   #load test image
   img = cv2.imread('./images/112image.png')
   img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
   img = np.transpose(img, (2,0,1)).reshape([1,3,112,112])
   data = mx.nd.array(img)
   db = mx.io.DataBatch(data=(data,))
   
   #load mxnet model
   prefix = './model'
   epoch = 0
   sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
   model = mx.mod.Module(symbol=sym, label_names = None)
   model.bind(data_shapes=[('data', (1, 3, 112, 112))])
   model.set_params(arg_params, aux_params)
   
   #inference
   model.forward(db, is_train=False)
   ret = model.get_outputs()[0].asnumpy()
   
   print(ret)
   ```
   # onnx model script
   ```
   import mxnet as mx
   import numpy as np
   import mxnet.contrib.onnx as onnx_mxnet
   import cv2
   
   #load test image
   img = cv2.imread('./images/112image.png')
   img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
   img = np.transpose(img, (2,0,1)).reshape([1,3,112,112])
   data = mx.nd.array(img)
   db = mx.io.DataBatch(data=(data,))
   
   #load onnx model
   model_path = './mxnet_exported_ga1m.onnx'
   sym, arg_params, aux_params = onnx_mxnet.import_model(model_path)
   model = mx.mod.Module(symbol=sym, label_names = None)
   model.bind(data_shapes=[('data', (1, 3, 112, 112))])
   model.set_params(arg_params, aux_params)
   
   #inference
   model.forward(db, is_train=False)
   ret = model.get_outputs()[0].asnumpy()
   
   print(ret)
   ```
   
   The output of the above two scripts is inconsistent.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services