Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tf2mx/yolov3 #762

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 55 additions & 7 deletions mmdnn/conversion/mxnet/mxnet_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,27 @@ def gen_code(self, phase):
np.save(outfile, self.output_weights)

comment = "\n # if a GPU is available, change mx.cpu() to mx.gpu()"
self.add_body(1, comment)
# We use the real_name for specifying the input layer in data_names
# since MXNet API wants the actual name of the layer. On the other
# hand, the module API wants the last symbol in the symbol chain, so
# for the output node we need to use the actual python variable name
# of the last layer (real_variable_name).
last_line = "{:<15} = mx.mod.Module(symbol = {}, context = mx.cpu(), data_names = ['{}'])".format(
# of the last layer (real_variable_name)
if len(self.IR_graph.output_layers) == 1:
last_line = "{:<15} = mx.mod.Module(symbol = {}, context = mx.cpu(), data_names = ['{}'])".format(
"model",
', '.join([self.IR_graph.get_node(name).real_variable_name for name in self.IR_graph.output_layers if self.IR_graph.get_node(name).type !='Pack' and self.IR_graph.get_node(name).type != 'Shape']),
', '.join([self.IR_graph.get_node(name).real_name for name in self.IR_graph.input_layers if self.IR_graph.get_node(name).type != 'Const']))
else:
group_line = "{:<15} = mx.sym.Group([{}])".format(
"group",
', '.join([self.IR_graph.get_node(name).real_variable_name for name in self.IR_graph.output_layers if self.IR_graph.get_node(name).type !='Pack' and self.IR_graph.get_node(name).type != 'Shape']))
last_line = "{:<15} = mx.mod.Module(symbol = {}, context = mx.cpu(), data_names = ['{}'])".format(
"model",
"group",
', '.join([self.IR_graph.get_node(name).real_name for name in self.IR_graph.input_layers if self.IR_graph.get_node(name).type != 'Const']))
self.add_body(1, group_line)

self.add_body(1, comment)
self.add_body(1, last_line)
self.add_body(1, "return model")

Expand Down Expand Up @@ -946,7 +956,13 @@ def emit_Mul(self, IR_node):
self.parent_variable_name(IR_node, [1]))

return code


def emit_RealDiv(self, IR_node):
code = "{:<15} = mx.sym.broadcast_div({}, {})".format(
IR_node.variable_name,
self.parent_variable_name(IR_node),
self.parent_variable_name(IR_node, [1]))
return code

def emit_ReduceMean(self, IR_node):
axes = IR_node.layer.attr['axes'].list.i[:]
Expand Down Expand Up @@ -1109,7 +1125,16 @@ def emit_Split(self, IR_node):
axis)

return code


def emit_SplitV(self, IR_node):
axis = IR_node.get_attr('axis')
indices_or_sections = tuple(IR_node.get_attr('indices_or_sections'))
code = "{:<15} = mx.sym.split_v2({}, {}, axis={})".format(
IR_node.variable_name,
self.parent_variable_name(IR_node),
indices_or_sections,
axis)
return code

def emit_Sigmoid(self, IR_node):
code = "{:<15} = mx.sym.sigmoid(data={}, name='{}')".format(
Expand All @@ -1118,7 +1143,15 @@ def emit_Sigmoid(self, IR_node):
IR_node.name
)
return code

def emit_ResizeNearestNeighbor(self, IR_node):
scale = int(IR_node.get_attr('upSample_scale'))
code = "{:<15} = mx.sym.UpSampling({}, scale={}, sample_type='nearest')".format(
IR_node.variable_name,
self.parent_variable_name(IR_node),
scale)

return code

def emit_Tanh(self, IR_node):
code = "{:<15} = mx.sym.tanh(data={}, name='{}')".format(
Expand All @@ -1128,7 +1161,14 @@ def emit_Tanh(self, IR_node):
)
return code


def emit_Exp(self, IR_node):
code = "{:<15} = mx.sym.exp(data={}, name='{}')".format(
IR_node.variable_name,
self.parent_variable_name(IR_node),
IR_node.name
)
return code

def emit_Maxmum(self, IR_node):
code = "{:<15} = mx.sym.maxmum({}, {}, name='{}')".format(
IR_node.variable_name,
Expand All @@ -1137,7 +1177,15 @@ def emit_Maxmum(self, IR_node):
IR_node.name
)
return code


def emit_Maximum(self, IR_node):
code = "{:<15} = mx.sym.broadcast_maximum({}, {}, name='{}')".format(
IR_node.variable_name,
self.parent_variable_name(IR_node),
self.parent_variable_name(IR_node, [1]),
IR_node.name
)
return code

def emit_Minimum(self, IR_node):
code = "{:<15} = mx.sym.minimum({}, {}, name='{}')".format(
Expand Down
3 changes: 2 additions & 1 deletion mmdnn/conversion/tensorflow/tensorflow_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class TensorflowGraph(Graph):
multi_tensor_type = [
"Slice",
"Split",
"Unpack"
"Unpack",
"SplitV"
]


Expand Down
42 changes: 39 additions & 3 deletions mmdnn/conversion/tensorflow/tensorflow_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ def rename_UNKNOWN(self, source_node):
% (source_node.type, source_node.name))
return

def rename_ResizeNearestNeighbor(self, source_node):
IR_node = self._convert_identity_operation(source_node)
input_shape = self.get_parent(source_node.name,[0]).layer.attr['_output_shapes'].list.shape[0].dim[1].size
output_shape = source_node.layer.attr['_output_shapes'].list.shape[0].dim[1].size
upSample_scale = output_shape/input_shape
kwargs = {}
kwargs['upSample_scale'] = upSample_scale
assign_IRnode_values(IR_node, kwargs)

def rename_Placeholder(self, source_node):
IR_node = self._convert_identity_operation(source_node, new_op='DataInput')
Expand Down Expand Up @@ -565,7 +573,12 @@ def _convert_identity_operation(self, source_node, start_edge_id = 0, in_edge_co

def rename_Relu(self, source_node):
self._convert_identity_operation(source_node)


def rename_LeakyRelu(self, source_node): # 191011 sfj add
IR_node = self._convert_identity_operation(source_node)
kwargs = {}
kwargs['alpha'] = source_node.get_attr('alpha') # 191014 sfj add
assign_IRnode_values(IR_node, kwargs)

def rename_Softmax(self, source_node):
self._convert_identity_operation(source_node)
Expand Down Expand Up @@ -612,7 +625,9 @@ def rename_Abs(self, source_node):

def rename_Square(self, source_node):
IR_node = self._convert_identity_operation(source_node, in_edge_count = 1, new_op = 'Square')


def rename_Exp(self, source_node): # 191013 sfj add
IR_node = self._convert_identity_operation(source_node, in_edge_count = 1, new_op = 'Exp')

def rename_MatMul(self, source_node):

Expand Down Expand Up @@ -685,7 +700,10 @@ def rename_RealDiv(self, source_node):
# print (source_node)
# print (source_node.layer)
# assert False
self._convert_identity_operation(source_node, new_op='Div')
#self._convert_identity_operation(source_node, new_op='Div')
self._convert_identity_operation(source_node, new_op='RealDiv')
self._add_constant_node(source_node)



def rename_Floor(self, source_node):
Expand Down Expand Up @@ -928,7 +946,21 @@ def rename_Split(self, source_node):
'split' : source_node.get_attr('num_split')
}
assign_IRnode_values(IR_node, kwargs)

def rename_SplitV(self, source_node): # 191013 sfj add, const is [2,2,1,0]
IR_node = self._convert_identity_operation(source_node)
sizes = tensor_util.MakeNdarray(self.get_parent(source_node.name, [1]).layer.attr['value'].tensor)
indices_or_sections = []
target = 0
for size in sizes:
target += size
indices_or_sections.append(target)

kwargs = {
'indices_or_sections' : indices_or_sections,
'axis' : self.get_parent(source_node.name, [2]).layer.attr['value'].tensor.int_val[0]
}
assign_IRnode_values(IR_node, kwargs)

def rename_StridedSlice(self, source_node):
# TODO: Current it is only for slice
Expand Down Expand Up @@ -1054,6 +1086,10 @@ def rename_Minimum(self, source_node):
def rename_Maxmum(self, source_node):
self._add_constant_node(source_node)
self._convert_identity_operation(source_node)

def rename_Maximum(self, source_node): # 191016 sfj add
self._add_constant_node(source_node)
IR_node = self._convert_identity_operation(source_node)

def rename_Cast(self, source_node):
IR_node = self._convert_identity_operation(source_node)
Expand Down