提交 1ac6b979 编写于 作者: L Liangliang He

Merge branch 'master' into 'master'

Fold BN with convolution if using nnlib

See merge request !79
...@@ -150,7 +150,6 @@ DEF_OP_WREF(QuantizedSigmoid_8) ...@@ -150,7 +150,6 @@ DEF_OP_WREF(QuantizedSigmoid_8)
DEF_OP_WREF(QuantizedTanh_8) DEF_OP_WREF(QuantizedTanh_8)
DEF_OP_WREF(QuantizedSoftmax_8) DEF_OP_WREF(QuantizedSoftmax_8)
DEF_OP_WREF(QuantizedLRN_8) DEF_OP_WREF(QuantizedLRN_8)
DEF_OP_WREF(Quantizedpad2d_frame_8p)
DEF_OP_WREF(QuantizedSub_8p8to32) DEF_OP_WREF(QuantizedSub_8p8to32)
DEF_OP_WREF(QuantizedMaximum_8) DEF_OP_WREF(QuantizedMaximum_8)
DEF_OP_WREF(QuantizedMinimum_8) DEF_OP_WREF(QuantizedMinimum_8)
......
py_library( py_library(
name = "tf_converter_lib", name = "tf_converter_lib",
srcs = ["tf_converter_lib.py", "tf_dsp_converter_lib.py"], srcs = [
"tf_converter_lib.py",
"tf_dsp_converter_lib.py",
"tf_graph_util.py"],
srcs_version = "PY2AND3", srcs_version = "PY2AND3",
deps = [ deps = [
"//mace/proto:mace_py", "//mace/proto:mace_py",
......
...@@ -47,13 +47,14 @@ class DspOps(object): ...@@ -47,13 +47,14 @@ class DspOps(object):
'Split': 'Split_f', 'Split': 'Split_f',
'Transpose': 'Transpose_f', 'Transpose': 'Transpose_f',
'Concat': 'Concat_f', 'Concat': 'Concat_f',
'AddN': 'AddN_f',
} }
def has_op(self, tf_op): def has_op(self, tf_op):
return tf_op in self.dsp_ops return tf_op in self.dsp_ops
def map_nn_op(self, tf_op): def map_nn_op(self, tf_op):
if tf_op not in self.dsp_ops: if tf_op not in self.dsp_ops:
raise Exception('Could not map nn op') raise Exception('Could not map nn op for: ', tf_op)
return self.dsp_ops[tf_op] return self.dsp_ops[tf_op]
...@@ -4,6 +4,7 @@ import tensorflow as tf ...@@ -4,6 +4,7 @@ import tensorflow as tf
import numpy as np import numpy as np
from operator import mul from operator import mul
from dsp_ops import DspOps from dsp_ops import DspOps
from mace.python.tools import tf_graph_util
padding_mode = { padding_mode = {
'NA': 0, 'NA': 0,
...@@ -162,6 +163,12 @@ def add_output_node(net_def, output_node): ...@@ -162,6 +163,12 @@ def add_output_node(net_def, output_node):
node_input.output_port = 0 node_input.output_port = 0
def convert_to_mace_pb(input_graph_def, input_dim, output_node): def convert_to_mace_pb(input_graph_def, input_dim, output_node):
"""
nnlib does not have batch norm, so use tensorflow optimizer to fold
batch norm with convolution. The fold optimization reorders ops, so
we sort ops first by topology.
"""
input_graph_def = tf_graph_util.sort_graph(input_graph_def)
inputs = input_dim.split(';') inputs = input_dim.split(';')
input_shape = {} input_shape = {}
for input in inputs: for input in inputs:
......
import tensorflow as tf
from collections import OrderedDict
def sort_graph_node(node, nodes_map, ordered_nodes_map):
if node.name not in ordered_nodes_map:
for input_tensor_name in node.input:
input_node_name = input_tensor_name.split(':')[
0] if ':' in input_tensor_name else input_tensor_name
if input_node_name not in nodes_map or input_node_name in ordered_nodes_map:
continue
input_node = nodes_map[input_node_name]
sort_graph_node(input_node, nodes_map, ordered_nodes_map)
ordered_nodes_map[input_node_name] = input_node
ordered_nodes_map[node.name] = node
def sort_graph(graph_def):
nodes_map = {}
ordered_nodes_map = OrderedDict()
for node in graph_def.node:
nodes_map[node.name] = node
for node in graph_def.node:
sort_graph_node(node, nodes_map, ordered_nodes_map)
sorted_graph = tf.GraphDef()
sorted_graph.node.extend([node for _, node in ordered_nodes_map.iteritems()])
return sorted_graph
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册