Skip to content
Permalink
Browse files

fix coreml, tf parser init, mxnet bn (#598)

fix coreml, tf parser init, mxnet bn
  • Loading branch information...
rainLiuplus committed Mar 3, 2019
1 parent f62a33a commit 350aa53c6c0d4034aa821d797572c629919e9552
@@ -51,7 +51,7 @@ def __init__(self, model):
# load model file into Coreml Graph
if isinstance(model, _string_types):
# model.encode() convert to str --- python2 may crash due to type 'unicode'
model = _MLModel(model.encode())
model = _MLModel(model)
model = model.get_spec()
self.weight_loaded = True
else:
@@ -772,6 +772,8 @@ def rename_scale(self, coreml_node):


self.set_weight(coreml_node_layer.name, "scale", np.array(coreml_node_scale.scale.floatValue).astype(np.float32))
self.set_weight(coreml_node_layer.name, "scale_mean", np.zeros_like(coreml_node_scale.scale.floatValue).astype(np.float32))
self.set_weight(coreml_node_layer.name, "scale_var", np.ones_like(coreml_node_scale.scale.floatValue).astype(np.float32))

self.set_weight(coreml_node_layer.name, "shapeScale", coreml_node_scale.shapeScale[0])

@@ -887,7 +889,9 @@ def rename_innerProduct(self, source_node):
if IR_node.attr['use_bias'].b:
self.set_weight(source_node_layer.name, 'bias', np.array(source_node_inner.bias.floatValue).astype(np.float32) )
# change to single because of the tf matmul


# in features
IR_node.attr['in_features'].i = source_node_inner.inputChannels

def rename_Padding(self, source_node):
IR_node = self.IR_graph.node.add()
@@ -93,7 +93,7 @@ def inference(cls, architecture, model_path, image_path):

prob = coreml_output[output_name]
if isinstance(prob, dict):
prob = coreml_output[output_name].values()
prob = list(coreml_output[output_name].values())
prob = np.array(prob).squeeze()

return prob
@@ -520,8 +520,10 @@ def emit_BatchNorm(self, IR_node):

if self.weight_loaded:
if not fix_gamma:
self.output_weights[IR_node.name + "_gamma"] = np.multiply(weight_dict['scale'], weight_dict_scale['scale'])
self.output_weights[IR_node.name + "_beta"] = np.multiply(weight_dict['bias'], weight_dict_scale['scale']) + weight_dict_scale['bias']
# self.output_weights[IR_node.name + "_gamma"] = np.multiply(weight_dict['scale'], weight_dict_scale['scale'])
# self.output_weights[IR_node.name + "_beta"] = np.multiply(weight_dict['bias'], weight_dict_scale['scale']) + weight_dict_scale['bias']
self.output_weights[IR_node.name + "_gamma"] = weight_dict['scale']
self.output_weights[IR_node.name + "_beta"] = weight_dict['bias']

# not supported yet
use_global_stats = "False"
@@ -304,6 +304,9 @@ def emit_FullyConnected(self, IR_node):
for i in self.IR_graph.get_parent(IR_node.name, [0]).layer.attr['_output_shapes'].list.shape[0].dim[1:]:
in_features *= i.size

if IR_node.get_attr('in_features') != None:
in_features = IR_node.get_attr('in_features')

self.add_init(2, "self.{} = self.__dense(name = '{}', in_features = {}, out_features = {}, bias = {})".format(
IR_node.variable_name,
IR_node.name,
@@ -253,7 +253,7 @@ def __init__(self, meta_file, checkpoint_file, dest_nodes, inputShape = None, in
self.tf_graph = TensorflowGraph(model)
for node in self.tf_graph.model.node:
if node.name in in_nodes:
node.attr['shape'].list.shape.extend([tensor_input.as_proto()])
node.attr['shape'].shape.CopyFrom(tensor_input.as_proto())
node.attr['_output_shapes'].list.shape.pop() #unknown_rank pop
node.attr['_output_shapes'].list.shape.extend([tensor_input.as_proto()])

@@ -272,21 +272,23 @@ def __init__(self, meta_file, checkpoint_file, dest_nodes, inputShape = None, in

# Get input node name
if not in_nodes:
in_nodes = {}
in_nodes = []
for node in model.node:
if node.op == 'Placeholder':
in_node_name = str(node.name)
in_node_shape = node.attr['_output_shapes'].list.shape[0]
in_node_shape_str = self._shapeToStr(in_node_shape)
in_nodes[in_node_name] = in_node_shape_str
in_nodes.append(node.name)

transformed_graph_def = TransformGraph(model, in_nodes.keys(),
transformed_graph_def = TransformGraph(model, in_nodes,
dest_nodes, transforms)
in_type_list = {}
in_shape_list = {}

for n in transformed_graph_def.node:
if n.name in in_nodes:
in_type_list[n.name] = n.attr['dtype'].type

in_node_shape = n.attr['shape'].shape
in_node_shape_str = self._shapeToStr(in_node_shape)
in_shape_list[n.name] = in_node_shape_str

dtype = tensorflow.float32
with tensorflow.Graph().as_default() as g:
input_map = {}
@@ -300,7 +302,7 @@ def __init__(self, meta_file, checkpoint_file, dest_nodes, inputShape = None, in
elif in_type_list[in_node] == 10:
dtype = tensorflow.bool

x = tensorflow.placeholder(dtype, shape = in_nodes[in_node])
x = tensorflow.placeholder(dtype, shape = in_shape_list[in_node])
input_map[in_node] = x

tensorflow.import_graph_def(transformed_graph_def, name='', input_map=input_map)
@@ -309,7 +311,7 @@ def __init__(self, meta_file, checkpoint_file, dest_nodes, inputShape = None, in

meta_graph_def = tensorflow.train.export_meta_graph(filename='./my-model.meta')
model = meta_graph_def.graph_def

self.tf_graph = TensorflowGraph(model)
self.tf_graph.build()

@@ -12,7 +12,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.2.3',
version='0.2.4',

description='Deep learning model converter, visualization and editor.',
long_description=long_description,
@@ -6,7 +6,6 @@
import sys
import imp
import numpy as np

from mmdnn.conversion.examples.imagenet_test import TestKit
import utils
from utils import *
@@ -653,7 +652,7 @@ def prep_for_coreml(prename, BGRTranspose):
else:

from PIL import Image as pil_image
img = pil_image.open(test_input_path)
img = pil_image.open(test_input_path(architecture_name))
img = img.resize((size, size))

# inference
@@ -887,7 +886,7 @@ def onnx_emit(original_framework, architecture_name, architecture_path, weight_p
'inception_v3' : [caffe_emit, coreml_emit, keras_emit, mxnet_emit, pytorch_emit, tensorflow_emit],
'mobilenet' : [caffe_emit, coreml_emit, keras_emit, mxnet_emit, pytorch_emit, tensorflow_emit],
'resnet50' : [caffe_emit, coreml_emit, keras_emit, mxnet_emit, pytorch_emit, tensorflow_emit],
'tinyyolo' : [coreml_emit, keras_emit, mxnet_emit, pytorch_emit, tensorflow_emit],
# 'tinyyolo' : [coreml_emit, keras_emit, mxnet_emit, pytorch_emit, tensorflow_emit],
'vgg16' : [caffe_emit, coreml_emit, keras_emit, mxnet_emit, pytorch_emit, tensorflow_emit],
},

@@ -9,7 +9,7 @@
def test_coreml():
if is_coreml_supported():
tester = TestModels()
tester._test_function('coreml', tester.CoreMLParse)
tester._test_function('coreml', tester.coreml_parse)


if __name__ == '__main__':

0 comments on commit 350aa53

Please sign in to comment.
You can’t perform that action at this time.