From b8adc23d04a06da1b9ee6d932ded545f3f56d279 Mon Sep 17 00:00:00 2001 From: Jiahao Yao Date: Thu, 3 May 2018 17:27:32 +0800 Subject: [PATCH 1/2] modifed to issue #85 --- mmdnn/conversion/mxnet/mxnet_parser.py | 3 ++ .../tensorflow/tensorflow_emitter.py | 35 ++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/mmdnn/conversion/mxnet/mxnet_parser.py b/mmdnn/conversion/mxnet/mxnet_parser.py index 83f358f5..bf922074 100644 --- a/mmdnn/conversion/mxnet/mxnet_parser.py +++ b/mmdnn/conversion/mxnet/mxnet_parser.py @@ -898,6 +898,9 @@ def _convert_scalar_operator(self, source_node, new_op): value = source_node.get_attr('scalar') value_node = self.IR_graph.node.add() value_node.name = source_node.real_name + "_second" + # left strip the "_" at the beginning of the name + # Issue #85, #135 + value_node.name = value_node.name.lstrip('_') value_node.op = 'Constant' self.set_weight(value_node.name, 'value', np.array([value], np.float32)) diff --git a/mmdnn/conversion/tensorflow/tensorflow_emitter.py b/mmdnn/conversion/tensorflow/tensorflow_emitter.py index d3a2b171..c72f28f1 100644 --- a/mmdnn/conversion/tensorflow/tensorflow_emitter.py +++ b/mmdnn/conversion/tensorflow/tensorflow_emitter.py @@ -240,14 +240,33 @@ def emit_FullyConnected(self, IR_node): bias_str = "bias_initializer = tf.constant_initializer(__weights_dict['{}']['bias']), ".format(IR_node.name) else: bias_str = "" - code = "{:<15} = tf.layers.dense({}, {}, {}{}use_bias = {})".format( - IR_node.variable_name, - self.parent_variable_name(IR_node), - IR_node.layer.attr['units'].i, - kernel_str, - bias_str, - IR_node.layer.attr['use_bias'].b) - self.add_body(1, code) + # check whether flatten operator should be added + parent = self.IR_graph.get_parent(IR_node.name, [0]) + parent_shape = shape_to_list(parent.get_attr('_output_shapes')[0]) + if len(parent_shape) > 2: + # flatten is needed + self.add_body(1, "{:<15} = tf.contrib.layers.flatten({})".format( + IR_node.variable_name + '_flatten', + self.parent_variable_name(IR_node))) + + code = "{:<15} = tf.layers.dense({}, {}, {}{}use_bias = {})".format( + IR_node.variable_name, + IR_node.variable_name + '_flatten', + IR_node.layer.attr['units'].i, + kernel_str, + bias_str, + IR_node.layer.attr['use_bias'].b) + self.add_body(1, code) + + else: + code = "{:<15} = tf.layers.dense({}, {}, {}{}use_bias = {})".format( + IR_node.variable_name, + self.parent_variable_name(IR_node), + IR_node.layer.attr['units'].i, + kernel_str, + bias_str, + IR_node.layer.attr['use_bias'].b) + self.add_body(1, code) def emit_Flatten(self, IR_node): From dec29db0bfe43d6e762f5aa1961a63e0a38750a6 Mon Sep 17 00:00:00 2001 From: Jiahao Yao Date: Thu, 3 May 2018 17:59:44 +0800 Subject: [PATCH 2/2] add Lresnet50e to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 6919433d..144744c2 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,8 @@ $ mmtoir -f keras -w imagenet_inception_v3.h5 -o keras_inception_v3 - [Tensorflow slim model "ResNet V2 152" to PyTorch](https://github.com/Microsoft/MMdnn/blob/master/docs/tf2pytorch.md) +- [Mxnet model "LResNet50E-IR" to Tensorflow](https://github.com/Microsoft/MMdnn/issues/85) and [related issue](https://github.com/Microsoft/MMdnn/issues/135) + ### Users' Examples - [MXNet "ResNet-152-11k" to PyTorch](https://github.com/Microsoft/MMdnn/issues/6)