reported at https://discuss.gluon.ai/t/topic/7396/3, the loading of shufflenet from onnx model zoo into mxnet fails.
The reported error message:
KeyError Traceback (most recent call last)
<ipython-input-5-7912d84df342> in <module>()
1 if __name__ == '__main__':
2 #print(50000/16*15)
----> 3 loadShuffleNet()
4
<ipython-input-4-c7bde65da337> in loadShuffleNet()
22 path = '/media/jiaming/Seagate Backup Plus Drive/pretrain_model/shufflenet/'
23 onnx_path = path + 'model.onnx'
---> 24 sym, arg_params, aux_params = onnx_mxnet.import_model(onnx_path)
25 (new_sym, new_args) = get_sn_finetune(sym, arg_params, 7)
26
~/.local/lib/python3.6/site-packages/mxnet/contrib/onnx/_import/import_model.py in import_model(model_file)
51 # loads model file and returns ONNX protobuf object
52 model_proto = onnx.load(model_file)
---> 53 sym, arg_params, aux_params = graph.from_onnx(model_proto.graph)
54 return sym, arg_params, aux_params
55
~/.local/lib/python3.6/site-packages/mxnet/contrib/onnx/_import/import_onnx.py in from_onnx(self, graph)
112 onnx_attr = self._parse_attr(node.attribute)
113 inputs = [self._nodes[i] for i in node.input]
--> 114 mxnet_sym = self._convert_operator(node_name, op_name, onnx_attr, inputs)
115
116 for k, i in zip(list(node.output), range(len(mxnet_sym.list_outputs()))):
~/.local/lib/python3.6/site-packages/mxnet/contrib/onnx/_import/import_onnx.py in _convert_operator(self, node_name, op_name, attrs, inputs)
56 """
57 if op_name in convert_map:
---> 58 op_name, new_attrs, inputs = convert_map[op_name](attrs, inputs, self)
59 else:
60 raise NotImplementedError("Operator {} not implemented.".format(op_name))
~/.local/lib/python3.6/site-packages/mxnet/contrib/onnx/_import/op_translations.py in batch_norm(attrs, inputs, cls)
172
173 # in test mode "fix_gamma" should be unset.
--> 174 new_attrs['fix_gamma'] = 0 if new_attrs['fix_gamma'] == 1 else 1
175 return 'BatchNorm', new_attrs, inputs
176
KeyError: 'fix_gamma'
The ONNX specification recently changed the definition of the BatchNorm operator, see here opset_version7 and op_setversion6.
So currently there are many models that use opset_version6 and some that use opset_version7. This error is caused due to this discrepancy in the operator definition. I will try to prioritize and get a patch fix to handle both versions of the operator and merge it into the master.
This issue for BatchNorm operator is fixed in latest code. User is using old code. Ran both opset 6 and 7 of shufflenet model. It works fine. Cannot reproduce the issue. Added comment on forum: https://discuss.mxnet.io/t/error-in-loading-pretrained-shufflenet-onnx-model/1520/3?u=roshrini
@szha please close this issue if resolved.
@anirudhacharya Let's wait for @jmnie's confirmation
@anirudhacharya @szha Sorry for late reply. I just load the model using the latest mxnet. It works. Thank you very much.
Have solved? I met the same issue.
If i let mx.sym.BatchNorm(data=fc1, fix_gamma=True, eps=2e-5, momentum=bn_mom, name='bn3')
and save to onnx model,but when i load onnx mode ,find that mxnet default set fix_gamma to False in _op_translations.py # in test mode "fix_gamma" should be unset.
new_attrs['fix_gamma'] = not attrs.get('is_test', 1) ,so my predict results are diffrent, how to fix it?
Most helpful comment
@anirudhacharya @szha Sorry for late reply. I just load the model using the latest mxnet. It works. Thank you very much.