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

Fixed mapping of input name #3737

Merged
merged 10 commits into from
Dec 28, 2020
3 changes: 3 additions & 0 deletions model-optimizer/extensions/middle/AddMeanScaleValues.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def insert_pre_processing(graph: Graph, input_node: Node, node_mean_scale_values

for dst in input_node.out_port(0).get_destinations():
if dst.node.soft_get('type') != 'ShapeOf':
tmp = input_node.out_node(0)['fw_tensor_debug_info']
popovaan marked this conversation as resolved.
Show resolved Hide resolved
popovaan marked this conversation as resolved.
Show resolved Hide resolved
dst.get_connection().set_source(preprocessing.out_port(0))
input_node.out_node(0)['fw_tensor_debug_info'] = tmp
del preprocessing.out_node(0)['fw_tensor_debug_info']

input_node.out_port(0).connect(preprocessing.in_port(0))

Expand Down
46 changes: 43 additions & 3 deletions model-optimizer/extensions/middle/AddMeanScaleValues_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@

nodes = {
**regular_op_with_shaped_data('parameter', [1, 3, 227, 227],
{'type': 'Parameter', 'op': 'Parameter', 'shape': [1, 3, 227, 227]}),
{'type': 'Parameter', 'op': 'Parameter', 'shape': [1, 3, 227, 227],
'fw_tensor_debug_info': 'Placeholder'}),
popovaan marked this conversation as resolved.
Show resolved Hide resolved
**regular_op_with_shaped_data('parameter_2', [1, 3, 227, 227],
{'type': 'Parameter', 'op': 'Parameter', 'shape': [1, 3, 227, 227]}),
{'type': 'Parameter', 'op': 'Parameter', 'shape': [1, 3, 227, 227],
'fw_tensor_debug_info': 'Placeholder'}),

**regular_op_with_shaped_data('mul_scale', [1, 3, 227, 227], {'type': 'Multiply', 'op': 'Mul'}),
**regular_op_with_shaped_data('add_mean', [1, 3, 227, 227], {'type': 'Add', 'op': 'Add'}),
Expand All @@ -44,7 +46,19 @@
}


def set_graph_attrs(graph, graph_ref):
popovaan marked this conversation as resolved.
Show resolved Hide resolved
for node in graph.get_data_nodes():
node['fw_tensor_debug_info'] = ['data_attributes', 0]
for graph_ref in graph_ref.get_data_nodes():
graph_ref['fw_tensor_debug_info'] = ['data_attributes', 0]
popovaan marked this conversation as resolved.
Show resolved Hide resolved


class AddMeanScaleValuesTest(unittest.TestCase):
def check_graph_attrs(self, graph, graph_ref):
for node_name in graph.get_nodes_with_attributes():
if 'fw_tensor_debug_info' in graph.node[node_name]:
self.assertTrue(graph.node[node_name]['fw_tensor_debug_info'] == graph_ref.node[node_name]['fw_tensor_debug_info'])
popovaan marked this conversation as resolved.
Show resolved Hide resolved

def test_mean_values_with_data_name(self):
graph_ref = build_graph(nodes, [
*connect('parameter', '0:add_mean'),
Expand All @@ -58,11 +72,13 @@ def test_mean_values_with_data_name(self):
argv = Namespace(mean_scale_values=mean_scale)

graph = build_graph(nodes, [*connect('parameter', 'result')], nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

AddMeanScaleValues().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_mean_values_without_data_name(self):
graph_ref = build_graph(nodes, [
Expand All @@ -78,11 +94,13 @@ def test_mean_values_without_data_name(self):

graph = build_graph(nodes, [*connect('parameter', 'result')], {'parameter': {'name': 'None'}},
nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

AddMeanScaleValues().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_mean_values_explicit_and_optimized(self):
graph_ref = build_graph(nodes, [
Expand All @@ -96,13 +114,15 @@ def test_mean_values_explicit_and_optimized(self):
'parameter_2': {'mean': np.array([0., 0., 0.])}})
graph = build_graph(nodes, [*connect('parameter', 'result'), *connect('parameter_2', 'result_2')],
nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

AddMeanScaleValues().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
(flag, resp) = compare_graphs(graph, graph_ref, 'result_2', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_mean_values_explicit_and_scale_values_optimized(self):
graph_ref = build_graph(nodes, [
Expand All @@ -113,11 +133,13 @@ def test_mean_values_explicit_and_scale_values_optimized(self):

argv = Namespace(mean_scale_values={'parameter': {'scale': np.array([1.]), 'mean': np.array([1., 2., 3.])}})
graph = build_graph(nodes, [*connect('parameter', 'result')], nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

AddMeanScaleValues().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_mean_values_optimized_and_scale_values_explicit(self):
graph_ref = build_graph(nodes, [
Expand All @@ -129,11 +151,13 @@ def test_mean_values_optimized_and_scale_values_explicit(self):
argv = Namespace(
mean_scale_values={'parameter': {'scale': np.array([1., 2., 3.]), 'mean': np.array([0., 0., 0.])}})
graph = build_graph(nodes, [*connect('parameter', 'result')], nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

AddMeanScaleValues().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_mean_values_explicit_and_scale_values_explicit(self):
graph_ref = build_graph(nodes, [
Expand All @@ -147,11 +171,13 @@ def test_mean_values_explicit_and_scale_values_explicit(self):
argv = Namespace(mean_scale_values=[[np.array([1., 2., 3.]), np.array([1., 2., 3.])]])
graph = build_graph(nodes, [*connect('parameter', 'result')],
nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

AddMeanScaleValues().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_mean_values_explicit_and_scale_values_explicit_on_cutted_graph(self):
"""
Expand All @@ -173,13 +199,15 @@ def test_mean_values_explicit_and_scale_values_explicit_on_cutted_graph(self):
graph = build_graph(
nodes, [*connect('parameter', 'result'), *connect('parameter_2', 'op'), *connect('op', 'result_2')],
{'parameter_2': {'initial_node_name': 'op'}}, nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'
AddMeanScaleValues().find_and_replace_pattern(graph)

(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
(flag, resp) = compare_graphs(graph, graph_ref, 'result_2', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_mean_values_explicit_and_scale_values_explicit_with_shape_of(self):
graph_ref = build_graph(nodes,
Expand All @@ -203,16 +231,23 @@ def test_mean_values_explicit_and_scale_values_explicit_with_shape_of(self):
*connect('shape_of', 'result_2'),
],
nodes_with_edges_only=True, cli=argv)
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

AddMeanScaleValues().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result', check_op_attrs=True)
self.assertTrue(flag, resp)
(flag, resp) = compare_graphs(graph, graph_ref, 'result_2', check_op_attrs=True)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)


class ScaleInputTests(unittest.TestCase):
def check_graph_attrs(self, graph, graph_ref):
for node_name in graph.get_nodes_with_attributes():
if 'fw_tensor_debug_info' in graph.node[node_name]:
self.assertTrue(graph.node[node_name]['fw_tensor_debug_info'] == graph_ref.node[node_name]['fw_tensor_debug_info'])

def test_scale_input(self):
graph_ref = build_graph(nodes, [
*connect('parameter', '0:mul_scale'),
Expand All @@ -222,17 +257,22 @@ def test_scale_input(self):
'scale_d': {'shape': [1, 1, 1, 1], 'value': np.array(1/255)}})

graph = build_graph(nodes, connect('parameter', 'result'), nodes_with_edges_only=True, cli=Namespace(scale=255))
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

ScaleInput().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result')
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)

def test_scale_input_2(self):
graph_ref = build_graph(nodes, connect('parameter', 'result'), nodes_with_edges_only=True)
graph = build_graph(nodes, connect('parameter', 'result'), nodes_with_edges_only=True, cli=Namespace(scale=1))
set_graph_attrs(graph, graph_ref)
graph.graph['layout'] = 'NCHW'

ScaleInput().find_and_replace_pattern(graph)
(flag, resp) = compare_graphs(graph, graph_ref, 'result')
self.assertTrue(flag, resp)
self.assertTrue(flag, resp)
self.check_graph_attrs(graph, graph_ref)