MMdnn icon indicating copy to clipboard operation
MMdnn copied to clipboard

MXNet convertorToIR don't parse 'attrs' layer parameters

Open belgraviton opened this issue 8 years ago • 43 comments

Platform: Ununtu 16.04

Python version: 3.6

Source framework with version: MXNet 1.0.0 with GPU

Destination framework with version: TensorFlow 1.4.1 with GPU

Pre-trained model path: LResNet50E-IR

Model json-file contain layers in the following form: { "op": "Convolution", "name": "conv0", "attrs": { "kernel": "(3, 3)", "no_bias": "True", "num_filter": "64", "pad": "(1, 1)", "stride": "(1, 1)", "workspace": "256" }, "inputs": [[3, 0, 0], [4, 0, 0]] }

Running scripts: python -m mmdnn.conversion._script.convertToIR -f mxnet -n model-symbol.json -w model-0000.params -d model-descr512 --inputShape 3 112 112

Error:

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/opt/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/opt/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 140, in <module>
    _main()
  File "/opt/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 135, in _main
    ret = _convert(args)
  File "/opt/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 72, in _convert
    parser.run(args.dstPath)
  File "/opt/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/common/DataStructure/parser.py", line 22, in run
    self.gen_IR()
  File "/opt/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/mxnet/mxnet_parser.py", line 266, in gen_IR
    func(current_node)
  File "/opt/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/mxnet/mxnet_parser.py", line 457, in rename_Convolution
    assert "attr" in source_node.layer or "param" in source_node.layer
AssertionError

belgraviton avatar Feb 27 '18 12:02 belgraviton

I change functions rename_FullyConnected, rename_LeakyReLU and rename_Convolution in mxnet_parser.py in the following way: change string assert "attr" in source_node.layer or "attrs" in source_node.layer or "param" in source_node.layer and add new condition elif "attrs" in source_node.layer: layer_attr = source_node.layer["attrs"]

this solve this issue. These changes should be applied to all mxnet functions.

belgraviton avatar Feb 28 '18 06:02 belgraviton

Hi @belgraviton , cool! I fixed it and checked it in. Could you help to try if it works in newest code now? Thanks!

kitstar avatar Feb 28 '18 06:02 kitstar

I use already implemented '_get_layer_attr' function in all 'rename' functions. Now it's OK

belgraviton avatar Mar 05 '18 14:03 belgraviton

Appreciate what you have done!

kitstar avatar Mar 05 '18 15:03 kitstar

I'm trying to use @belgraviton PR to convert the same model LResNet50E-IR but I am getting the following error:

Warning: MXNet Parser has not supported operator null with name data.
Warning: convert the null operator with name [data] into input layer.
Warning: MXNet Parser has not supported operator _minus_scalar with name _minusscalar0.
Warning: MXNet Parser has not supported operator _mul_scalar with name _mulscalar0.
Traceback (most recent call last):  
  File "/home/daniel/anaconda3/lib/python3.6/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)  
  File "/home/daniel/anaconda3/lib/python3.6/runpy.py", line 85, in _run_code
      exec(code, run_globals)
  File "/home/daniel/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 140, in <module>
      _main()  
  File "/home/daniel/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 135, in _main
      ret = _convert(args)  
  File "/home/daniel/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/_script/convertToIR.py", line 72, in _convert
      parser.run(args.dstPath)  
  File "/home/daniel/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/common/DataStructure/parser.py", line 22, in run
      self.gen_IR()  
  File "/home/daniel/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/mxnet/mxnet_parser.py", line 273, in gen_IR
      func(current_node)  
  File "/home/daniel/anaconda3/lib/python3.6/site-packages/mmdnn/conversion/mxnet/mxnet_parser.py", line 478, in rename_Convolution
    in_channel = self.IR_layer_map[IR_node.input[0]].attr["_output_shapes"].list.shape[0].dim[-1].size
KeyError: '_mulscalar0

d4nst avatar Mar 05 '18 15:03 d4nst

Hi @d4nst , there are two operators not support by current framework.

Warning: MXNet Parser has not supported operator _minus_scalar with name _minusscalar0.
Warning: MXNet Parser has not supported operator _mul_scalar with name _mulscalar0.

Could you provide the related files for us to implement and test it? Thanks.

kitstar avatar Mar 06 '18 02:03 kitstar

I used the same model posted by @belgraviton: LResNet50E-IR

d4nst avatar Mar 06 '18 17:03 d4nst

_minusscalar0 and _mulscalar0 can be replaced to _copy in json file. There are some other problems during 'LResNet50E-IR' MXNet to IR conversion. One of them is solved by PR #96

belgraviton avatar Mar 07 '18 08:03 belgraviton

Hi @belgraviton @d4nst I am working on these two op -> IR stuff. Will ping you if it is done. Thanks.

kitstar avatar Mar 07 '18 09:03 kitstar

@belgraviton @d4nst . Fixed. Please check the newest code and recent commit for detail info. But I didn't check the correctness. Thanks.

kitstar avatar Mar 08 '18 06:03 kitstar

It now runs without any errors. Thanks!

d4nst avatar Mar 08 '18 14:03 d4nst

There is no errors. OK. Thank you

belgraviton avatar Mar 14 '18 11:03 belgraviton

@d4nst @belgraviton Were you guys able to convert the IR representation to any other framework? I tried converting it (for the same model) to CoreML, but that gives vastly different outputs. When trying to convert the IR to Tensorflow, I get the following errors:

python -m mmdnn.conversion.examples.tensorflow.imagenet_test -n ir.py -w ir.npy --dump tf_ir.ckpt
/Library/Python/2.7/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "build/bdist.macosx-10.13-intel/egg/mmdnn/conversion/examples/tensorflow/imagenet_test.py", line 71, in <module>
  File "build/bdist.macosx-10.13-intel/egg/mmdnn/conversion/examples/tensorflow/imagenet_test.py", line 20, in __init__
  File "ir.py", line 255, in KitModel
    pre_fc1         = tf.layers.dense(bn1, 512, kernel_initializer = tf.constant_initializer(__weights_dict['pre_fc1']['weights']), bias_initializer = tf.constant_initializer(__weights_dict['pre_fc1']['bias']), use_bias = True)
  File "/Library/Python/2.7/site-packages/tensorflow/python/layers/core.py", line 215, in dense
    return layer.apply(inputs)
  File "/Library/Python/2.7/site-packages/tensorflow/python/layers/base.py", line 492, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "/Library/Python/2.7/site-packages/tensorflow/python/layers/base.py", line 434, in __call__
    self.build(input_shapes[0])
  File "/Library/Python/2.7/site-packages/tensorflow/python/layers/core.py", line 118, in build
    trainable=True)
  File "/Library/Python/2.7/site-packages/tensorflow/python/layers/base.py", line 374, in add_variable
    trainable=trainable and self.trainable)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1065, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 962, in get_variable
    use_resource=use_resource, custom_getter=custom_getter)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 367, in get_variable
    validate_shape=validate_shape, use_resource=use_resource)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 352, in _true_getter
    use_resource=use_resource)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 725, in _get_single_variable
    validate_shape=validate_shape)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variables.py", line 200, in __init__
    expected_shape=expected_shape)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variables.py", line 278, in _init_from_args
    initial_value(), name="initial_value", dtype=dtype)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 701, in <lambda>
    shape.as_list(), dtype=dtype, partition_info=partition_info)
  File "/Library/Python/2.7/site-packages/tensorflow/python/ops/init_ops.py", line 203, in __call__
    verify_shape=verify_shape)
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/constant_op.py", line 102, in constant
    tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/Library/Python/2.7/site-packages/tensorflow/python/framework/tensor_util.py", line 428, in make_tensor_proto
    (shape_size, nparray.size))
ValueError: Too many elements provided. Needed at most 262144, but received 12845056

galli-leo avatar Mar 15 '18 10:03 galli-leo

hi,i also use the LResNet50E-IR mxnet model,and i success to convert it to IR,and then i convert models from IR to Tensorflow code snippet use follow script like that: python -m mmdnn.conversion._script.IRToCode -f tensorflow --IRModelPath model-descr512.pb --IRWeightPath model-descr512.npy --dstModelPath tf_resnet50.py

log show that:

Parse file [model-descr512.pb] with binary format successfully.
TensorflowEmitter has not supported operator [_copy].
id
Target network code snippet is saved as [tf_resnet50.py].

but when i convert models from IR to Tensorflow model,i met the below error:

(python3) andy@andy:~/DISK-DATA/datasets/insightface/resnet-r50-am-lfw/model-r50-am-lfw$ python -m mmdnn.conversion.examples.tensorflow.imagenet_test -n tf_resnet50.py -w model-descr512.npy --dump tf_resnet152.ckpt
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/andy/python3/lib/python3.6/site-packages/mmdnn/conversion/examples/tensorflow/imagenet_test.py", line 71, in <module>
    tester = TestTF()
  File "/home/andy/python3/lib/python3.6/site-packages/mmdnn/conversion/examples/tensorflow/imagenet_test.py", line 20, in __init__
    self.input, self.model = self.MainModel.KitModel(self.args.w)
  File "/home/andy/DISK-DATA/datasets/insightface/resnet-r50-am-lfw/model-r50-am-lfw/tf_resnet50.py", line 26, in KitModel
    _minusscalar0_second = tf.constant(__weights_dict['_minusscalar0_second']['value'], name='_minusscalar0_second')
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 220, in constant
    name=name).outputs[0]
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3271, in create_op
    op_def=op_def)
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1589, in __init__
    raise ValueError("'%s' is not a valid node name" % node_def.name)
ValueError: '_minusscalar0_second' is not a valid node name

for resolve the raise error, then i modified the tf_resnet50.py:

_minusscalar0_second = tf.constant(__weights_dict['_minusscalar0_second']['value'], name='_minusscalar0_second')
_mulscalar0_second = tf.constant(__weights_dict['_mulscalar0_second']['value'], name='_mulscalar0_second')

to

_minusscalar0_second = tf.constant(__weights_dict['_minusscalar0_second']['value'], name='const_minusscalar0_second')
_mulscalar0_second = tf.constant(__weights_dict['_mulscalar0_second']['value'], name='const_mulscalar0_second')

that help to fix the raise ValueError,but another error raise,like that:

(python3) andy@andy:~/DISK-DATA/datasets/insightface/resnet-r50-am-lfw/model-r50-am-lfw$ python -m mmdnn.conversion.examples.tensorflow.imagenet_test -n tf_resnet50.py -w model-descr512.npy --dump tf_resnet152.ckpt
Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/andy/python3/lib/python3.6/site-packages/mmdnn/conversion/examples/tensorflow/imagenet_test.py", line 71, in <module>
    tester = TestTF()
  File "/home/andy/python3/lib/python3.6/site-packages/mmdnn/conversion/examples/tensorflow/imagenet_test.py", line 20, in __init__
    self.input, self.model = self.MainModel.KitModel(self.args.w)
  File "/home/andy/DISK-DATA/datasets/insightface/resnet-r50-am-lfw/model-r50-am-lfw/tf_resnet50.py", line 28, in KitModel
    minusscalar0    = id - _minusscalar0_second
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 949, in r_binary_op_wrapper
    x = ops.convert_to_tensor(x, dtype=y.dtype.base_dtype, name="x")
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 946, in convert_to_tensor
    as_ref=False)
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1036, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 235, in _constant_tensor_conversion_function
    return constant(v, dtype=dtype, name=name)
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 214, in constant
    value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 433, in make_tensor_proto
    _AssertCompatible(values, dtype)
  File "/home/andy/python3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 344, in _AssertCompatible
    (dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected float32, got <built-in function id> of type 'builtin_function_or_method' instead.

it show that the file tf_resnet50.py have a bud,local in file: minusscalar0 = id - _minusscalar0_second

consider all of the scripts and executed log, maybe error bring by the step “convert models from IR to Tensorflow code snippet”,and the log “TensorflowEmitter has not supported operator [_copy]. ” couldn't ignore. finally,how we to fix that bug?Best wish to you.

update:

if i modifiy the code "minusscalar0 = id - _minusscalar0_second" to "minusscalar0 = data - _minusscalar0_second", that will help to solve the error, whitch 'data' is a placeholder of input, but then i met the error same as @galli-leo.

sirius-ai avatar Mar 23 '18 07:03 sirius-ai

@xsr-ai I just removed the whole minus and mul scalar nodes, since they are just preprocessing the images. But yeah, stuck on the error I received then. When I get some more time, I will try and debug it, I am guessing somehow the weights are wrongly saved.

galli-leo avatar Mar 23 '18 07:03 galli-leo

@galli-leo i add “bn1 = tf.reshape(bn1, (1, 25088))” before tf.layers.dense in the tf_resnet50.py,then fix the error “ValueError: Too many elements provided. Needed at most 262144, but received 12845056”, but when i test the converted tf model it have a poor performance,lfw Acc just reach to 0.573+-0.021,i don‘t know why the test performance so poor.

sirius-ai avatar Mar 23 '18 11:03 sirius-ai

@xsr-ai We probably need to fix the weights themselves not the layer. I should get some time next week to look at this.

galli-leo avatar Mar 23 '18 12:03 galli-leo

@galli-leo look forward to your great work.

sirius-ai avatar Mar 23 '18 14:03 sirius-ai

@galli-leo hi, gali, are you get any progress?

sirius-ai avatar Apr 02 '18 02:04 sirius-ai

@xsr-ai @galli-leo Could you provide your test code? I guess I know how to handle it, just try transpose the weight. Thanks.

kitstar avatar Apr 06 '18 09:04 kitstar