microsoft / MMdnn

MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.
MIT License
5.8k stars 965 forks source link

'NoneType' object has no attribute 'get_attr' when convert from tf to pytorch #401

Open CaichaoGitHub opened 6 years ago

CaichaoGitHub commented 6 years ago

Platform ubuntu

Python version:2.7

Source framework with version Tensorflow 1.4.1 with GPU

Destination framework with version Pytorch

Pre-trained model path (webpath or webdisk path):

Running scripts:

mmconvert -sf tensorflow -in /home/uc/project/football2/test_tensorflow_retrain/hand_writing_modeo.ckpt.meta -iw /home/uc/project/football2/test_tensorflow_retrain/hand_writing_modeo.ckpt --inputShape 8,8,1 --inNodeName tf_x --dstNodeName outputnode -df pytorch -om /home/uc/project/football2/test_tensorflow_retrain

command line outputs:

Parse file [/home/uc/project/football2/test_tensorflow_retrain/hand_writing_modeo.ckpt.meta] with binary format successfully. Tensorflow model file [/home/uc/project/football2/test_tensorflow_retrain/hand_writing_modeo.ckpt.meta] loaded successfully. Tensorflow checkpoint file [/home/uc/project/football2/test_tensorflow_retrain/hand_writing_modeo.ckpt] loaded successfully. [32] variables loaded. Tensorflow has not supported operator [StopGradient] with name [moments/StopGradient]. Tensorflow has not supported operator [SquaredDifference] with name [moments/SquaredDifference]. Tensorflow has not supported operator [Rsqrt] with name [batchnorm/Rsqrt]. Tensorflow has not supported operator [Softmax] with name [Softmax]. Tensorflow has not supported operator [ArgMax] with name [outputnode]. IR network structure is saved as [4b5a35eb3cfa476496fc7d3cc80dfccb.json]. IR network structure is saved as [4b5a35eb3cfa476496fc7d3cc80dfccb.pb]. IR weights are saved as [4b5a35eb3cfa476496fc7d3cc80dfccb.npy]. Parse file [4b5a35eb3cfa476496fc7d3cc80dfccb.pb] with binary format successfully. Warning: Graph Construct a self-loop node moments/SquaredDifference. Ignored. Warning: Graph Construct a self-loop node batchnorm/add/y. Ignored. Warning: Graph Construct a self-loop node batchnorm/Rsqrt. Ignored. Warning: Graph Construct a self-loop node Variable_5. Ignored. Warning: Graph Construct a self-loop node Variable_4. Ignored. Traceback (most recent call last): File "/home/uc/project/football2/venv/bin/mmconvert", line 11, in sys.exit(_main()) File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/_script/convert.py", line 108, in _main ret = IRToCode._convert(code_args) File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/_script/IRToCode.py", line 61, in _convert emitter.run(args.dstModelPath, args.dstWeightPath, args.phase) File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/pytorch/pytorch_emitter.py", line 44, in run super(PytorchEmitter, self).run(dstNetworkPath, dstWeightPath, phase) File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/common/DataStructure/emitter.py", line 21, in run self.save_code(dstNetworkPath, phase) File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/common/DataStructure/emitter.py", line 53, in save_code code = self.gen_code(phase) File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/pytorch/pytorch_emitter.py", line 95, in gen_code line = func(current_node) File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/pytorch/pytorch_emitter.py", line 526, in emit_ReduceMean axes = [self._convert_axis(IR_node, x) for x in IR_node.get_attr('axes')] File "/home/uc/project/football2/venv/local/lib/python2.7/site-packages/mmdnn/conversion/pytorch/pytorch_emitter.py", line 436, in _convert_axis ndim = len(self.IR_graph.get_parent(IR_node.name, [0]).get_attr('_output_shapes')[0].dim) AttributeError: 'NoneType' object has no attribute 'get_attr'

namizzz commented 6 years ago

Hi @CaichaoGitHub ,

  1. MMdnn focus on the network architecture part , so it doesn't support the operators like StopGradient, SquaredDifference ...
  2. please provide your model file for us to debug. It seems a bug about reducemean operator.
nataliaShepeleva commented 6 years ago

Hi @namizzz

I have same error trying to import tensorflow model to pytorch.

platform: win10 tensorflow-gpu==1.8 torch==0.4.1

Simple CNN trained on MNIST:

    with tf.name_scope('s_conv_1'):
        conv_1_1 = tf.layers.conv2d(X, filters=1 * self.num_filters, kernel_size=3,
                                    activation=None, padding="valid", name='conv_' + '1_1')
        batch_norm_1_1 = tf.layers.batch_normalization(conv_1_1, training=self.is_training, fused=False,
                                                       name='batch_' + '1_1')
        activ_1_1 = tf.nn.relu(batch_norm_1_1, name='activation_' + '1_1')
        pooling_1 = tf.layers.max_pooling2d(activ_1_1, pool_size=2, strides=2, padding='same', name='pooling_1')
        drop_1 = tf.layers.dropout(pooling_1, rate=self.dropout, name='drop_1')

    with tf.name_scope('s_conv_2'):
        conv_2_1 = tf.layers.conv2d(drop_1, filters=2 * self.num_filters, kernel_size=3,
                                    activation=None, padding="valid", name='conv_' + '2_1')
        batch_norm_2_1 = tf.layers.batch_normalization(conv_2_1, training=self.is_training, fused=False,
                                                       name='batch_' + '2_1')
        activ_2_1 =  tf.nn.relu(batch_norm_2_1, name='activation_' + '2_1')
        pooling_2 = tf.layers.max_pooling2d(activ_2_1, pool_size=2, strides=2, padding='same', name='pooling_2')
        drop_2 = tf.layers.dropout(pooling_2, rate=self.dropout, name='drop_2')

    with tf.name_scope('s_conv_3'):
        conv_3_1 = tf.layers.conv2d(drop_2, filters=4 * self.num_filters, kernel_size=3,
                                    activation=None, padding="valid", name='conv_' + '3_1')
        batch_norm_3_1 = tf.layers.batch_normalization(conv_3_1, training=self.is_training, fused=False,
                                                       name='batch_' + '3_1')
        activ_3_1 =  tf.nn.relu(batch_norm_3_1, name='activation_' + '3_1')
        pooling_3 = tf.layers.max_pooling2d(activ_3_1, pool_size=3, strides=2, padding='same', name='pooling_3')
        drop_3 = tf.layers.dropout(pooling_3, rate=self.dropout, name='drop_3')

    with tf.name_scope('s_outputs'):
        flat = tf.layers.flatten(drop_3, name='flatten')
        fc_1 = tf.layers.dense(flat, units=int(flat.shape[-1]), activation=None, name='fc_1')
        drop_5 = tf.layers.dropout(fc_1, rate=self.dropout, name='drop_5')
        fc_2 = tf.layers.dense(drop_5, units=int(drop_5.shape[-1]), activation=None, name='fc_2')
        drop_6 = tf.layers.dropout(fc_2, rate=self.dropout, name='drop_6')
        output = tf.layers.dense(drop_6, units=self.num_classes, activation=None, name='output')
    labels = tf.identity(output, name='MMdnn_Output')
    return output

Convertion to MMdnn format via: mmtoir -f tensorflow -in model.ckpt.meta -iw model.ckpt --inNodeName queue_Dequeue --inputShape 28,28,1 --dstNodeName MMdnn_Output -o model gives the following output:

Parse file [model.ckpt.meta] with binary format successfully. Tensorflow model file [model.ckpt.meta] loaded successfully. Tensorflow checkpoint file [model.ckpt] loaded successfully. [63] variables loaded. Tensorflow has not supported operator [StopGradient] with name [s_conv_1/batch_1_1/moments/StopGradient]. Tensorflow has not supported operator [SquaredDifference] with name [s_conv_1/batch_1_1/moments/SquaredDifference]. Tensorflow has not supported operator [Elu] with name [s_conv_1/activation_1_1]. Tensorflow has not supported operator [StopGradient] with name [s_conv_2/batch_2_1/moments/StopGradient]. Tensorflow has not supported operator [SquaredDifference] with name [s_conv_2/batch_2_1/moments/SquaredDifference]. Tensorflow has not supported operator [Elu] with name [s_conv_2/activation_2_1]. Tensorflow has not supported operator [StopGradient] with name [s_conv_3/batch_3_1/moments/StopGradient]. Tensorflow has not supported operator [SquaredDifference] with name [s_conv_3/batch_3_1/moments/SquaredDifference]. Tensorflow has not supported operator [Elu] with name [s_conv_3/activation_3_1]. IR network structure is saved as [model.json]. IR network structure is saved as [model.pb]. IR weights are saved as [model.npy].

Regardless of the activation (tf.nn.elu or tf.nn.relu) the output will be Tensorflow has not supported operator [Elu]. Also not really clear why dos problems with batch normalization appear.

Conversion from MMdnn to pytorch as mmtocode -f pytorch -n model.pb -w model.npy -d converted_pytorch.py -dw converted_pytorch.npy bringss the error:

Parse file [model.pb] with binary format successfully. Warning: Graph Construct a self-loop node s_conv_1/batch_1_1/moments/SquaredDifference. Ignored. Warning: Graph Construct a self-loop node s_conv_1/activation_1_1. Ignored. Warning: Graph Construct a self-loop node s_conv_2/batch_2_1/moments/SquaredDifference. Ignored. Warning: Graph Construct a self-loop node s_conv_2/activation_2_1. Ignored. Warning: Graph Construct a self-loop node s_conv_3/batch_3_1/moments/SquaredDifference. Ignored. Warning: Graph Construct a self-loop node s_conv_3/activation_3_1. Ignored. Traceback (most recent call last): File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\runpy.py", line 193, in _run_module_as_main "main", mod_spec) File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Users\shepeleva\AppData\Local\conda\conda\envs\py36-tf18\Scripts\mmtocode.exe__main__.py", line 9, in File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion_script\IRToCode.py", line 117, in _main ret = _convert(args) File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion_script\IRToCode.py", line 61, in _convert emitter.run(args.dstModelPath, args.dstWeightPath, args.phase) File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion\pytorch\pytorch_emitter.py", line 44, in run super(PytorchEmitter, self).run(dstNetworkPath, dstWeightPath, phase) File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion\common\DataStructure\emitter.py", line 21, in run self.save_code(dstNetworkPath, phase) File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion\common\DataStructure\emitter.py", line 53, in save_code code = self.gen_code(phase) File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion\pytorch\pytorch_emitter.py", line 95, in gen_code line = func(current_node) File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion\pytorch\pytorch_emitter.py", line 526, in emit_ReduceMean axes = [self._convert_axis(IR_node, x) for x in IR_node.get_attr('axes')] File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion\pytorch\pytorch_emitter.py", line 526, in axes = [self._convert_axis(IR_node, x) for x in IR_node.get_attr('axes')] File "c:\users\shepeleva\appdata\local\conda\conda\envs\py36-tf18\lib\site-packages\mmdnn\conversion\pytorch\pytorch_emitter.py", line 436, in _convert_axis ndim = len(self.IR_graph.get_parent(IR_node.name, [0]).get_attr('_output_shapes')[0].dim) AttributeError: 'NoneType' object has no attribute 'get_attr'

will be glad if you can help. thanks

YLuc commented 4 years ago

I encountered the same issue:

ndim = len(self.IR_graph.get_parent(IR_node.name, [0]).get_attr('_output_shapes')[0].dim) AttributeError: 'NoneType' object has no attribute 'get_attr'

when I convert tensorflow to pytorch. Problematic place is batch_normalization.

Any one has any solutions?