My model is exported from gluon model zoo like this:
name = './tmp/faster'
input_shape = (1, 3, 800, 1333)
model = model_zoo.get_model('faster_rcnn_fpn_resnet50_v1b_coco', pretrained=True)
model.hybridize()
# prepare image
impth = './000000000139.jpg'
ctx = mx.cpu()
T_fun = T.Compose([
T.Resize(input_shape[2:]),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
img = mx.image.imread(impth)
img = T_fun(img).expand_dims(axis=0).copyto(ctx)
out = model(img)
model.export(name)
Then inference code with gluon is like this:
import cv2
import numpy as np
import warnings
import mxnet.gluon as gluon
from gluoncv import model_zoo
import mxnet as mx
from mxnet.contrib import onnx as mx_onnx
im = cv2.imread('000000000139.jpg')
input_shape = (1, 3, 700, 1333)
ctx = mx.gpu()
with warnings.catch_warnings():
warnings.simplefilter('ignore')
net = gluon.nn.SymbolBlock.imports(
'./tmp/faster-symbol.json',
['data'],
'./tmp/faster-0000.params',
ctx=ctx,
)
im = cv2.resize(im, (input_shape[2:][::-1]))
im = (im - np.array([123, 117, 104])) / np.array([58.4, 57.1, 57.4])
im = im.transpose((2, 0, 1))[np.newaxis, :]
im = mx.nd.array(im).as_in_context(ctx)
out = net(im)
print(out)
And I also run inference with tvm like this:
import cv2
import numpy as np
import mxnet as mx
import tvm
import tvm.relay as relay
from tvm.contrib import graph_runtime
name = 'tmp/faster'
epoch = 0
sym, args, auxs = mx.model.load_checkpoint(name, epoch)
input_shape = (1, 3, 700, 1333)
shape_dict = {'data': input_shape}
mod, relay_params = relay.frontend.from_mxnet(sym, shape_dict, arg_params=args, aux_params=auxs)
func = mod['main']
target = 'cuda'
opt_level = 3
ctx = tvm.gpu(0)
with relay.build_config(opt_level=opt_level):
graph, lib, params = relay.build(func, target, params=relay_params)
m = graph_runtime.create(graph, lib, ctx)
im = cv2.imread('000000000139.jpg')
im = cv2.resize(im, (input_shape[2:][::-1]))
im = (im - np.array([123, 117, 104])) / np.array([58.4, 57.1, 57.4])
im = im.transpose((2, 0, 1))[np.newaxis, :]
dtype = 'float32'
m.set_input('data', tvm.nd.array(im.astype(dtype)))
m.set_input(**params)
m.run()
output0 = m.get_output(0)
output1 = m.get_output(1)
output2 = m.get_output(2).asnumpy()
print(output0)
print(output1)
print(output2)
print(type(output2))
The problem is that the output of the two method is not same, please tell me which output is correct and how I could make the other correct, thanks!!