Cannot compile mxnet models

Actually I have submitted on github, I compiled tvm on ubuntu16.04 with cuda10.0 without llvm. The mxnet version is 1.6.0-20191010, and the tvm commit id is: a7404230695bf774be26a25a30fd9b3

The code is like this:

import tvm
import tvm.relay as relay
from gluoncv import model_zoo

#  block = model_zoo.get_model('resnet18_v1', pretrained=True)
block = model_zoo.get_model('faster_rcnn_fpn_resnet50_v1b_coco', pretrained=True)
block.hybridize()
#  input_shape = (1, 3, 224, 224)
input_shape = (1, 3, 800, 1333)
shape_dict = {'data', input_shape}
mod, relay_params = relay.frontend.from_mxnet(block, shape_dict)

Which gives me the error of:

/miniconda/envs/py36/lib/python3.6/site-packages/mxnet/gluon/block.py:1191: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
data: None
input_sym_arg_type = in_param.infer_type()[0]
Traceback (most recent call last):

File “debug.py”, line 28, in
mod, relay_params = relay.frontend.from_mxnet(block, shape_dict)

File “/miniconda/envs/py36/lib/python3.6/site-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/relay/frontend/mxnet.py”, line 1360, in from_mxnet
inputs.append(mx.sym.Variable(name))

File “/miniconda/envs/py36/lib/python3.6/site-packages/mxnet/symbol/symbol.py”, line 2685, in var
raise TypeError('Expect a string for variable name ')

TypeError: Expect a string for variable name
/miniconda/envs/py36/lib/python3.6/site-packages/mxnet/gluon/block.py:1191: UserWarning: Cannot decide type for the following arguments. Consider providing them as input:
data: None
input_sym_arg_type = in_param.infer_type()[0]
Traceback (most recent call last):

File “debug.py”, line 28, in
mod, relay_params = relay.frontend.from_mxnet(block, shape_dict)

File “/miniconda/envs/py36/lib/python3.6/site-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/relay/frontend/mxnet.py”, line 1360, in from_mxnet
inputs.append(mx.sym.Variable(name))

File “/miniconda/envs/py36/lib/python3.6/site-packages/mxnet/symbol/symbol.py”, line 2685, in var
raise TypeError('Expect a string for variable name ')

TypeError: Expect a string for variable name

=====

If I write my code like this:

import mxnet as mx
import tvm
import tvm.relay as relay
from gluoncv import model_zoo
from mxnet.gluon.data.vision import transforms as T

name = './tmp/resnet'
input_shape = (1, 3, 224, 224)

model = model_zoo.get_model('resnet18_v1', 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)


sym, args, auxs = mx.model.load_checkpoint(name, 0)
shape_dict = {'data', input_shape}
mod, relay_params = relay.frontend.from_mxnet(sym, shape_dict, arg_params=args, aux_params=auxs)

I got the error of:

Traceback (most recent call last):

File “debug.py”, line 42, in
mod, relay_params = relay.frontend.from_mxnet(sym, shape_dict, arg_params=args, aux_params=auxs)

File “/miniconda/envs/py36/lib/python3.6/site-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/relay/frontend/mxnet.py”, line 1351, in from_mxnet
func = _from_mxnet_impl(symbol, shape, dtype, mod)

File “/miniconda/envs/py36/lib/python3.6/site-packages/tvm-0.6.dev0-py3.6-linux-x86_64.egg/tvm/relay/frontend/mxnet.py”, line 1258, in _from_mxnet_impl
shape = shape_dict[node_name] if node_name in shape_dict else None

TypeError: ‘set’ object is not subscriptable

How could I make it work ?

change shape_dict = {‘data’, input_shape}
to shape_dict = {‘data’: input_shape}

1 Like

Thanks, I was too careless.

By the way, after compiling the model, could I dump the compiled model for future reuse. I find that compiling the computation graph is time-consuming, and I could do better if I use the compiled model directly.

Perhaps go through Tutorials first:
https://docs.tvm.ai/tutorials/relay_quick_start.html#save-and-load-compiled-module