Browse Source

fix pylint warning.

tags/v1.2.0-rc1
liuchongming 4 years ago
parent
commit
6d4145c931
5 changed files with 28 additions and 130 deletions
  1. +1
    -1
      mindinsight/mindconverter/graph_based_converter/generator/generator.py
  2. +0
    -34
      mindinsight/mindconverter/graph_based_converter/mapper/gen_setting.py
  3. +3
    -4
      mindinsight/mindconverter/graph_based_converter/mapper/impl/nn/pool_mapper.py
  4. +1
    -1
      mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/slice_mapper.py
  5. +23
    -90
      tests/ut/mindconverter/graph_based_converter/mapper/test_mapper.py

+ 1
- 1
mindinsight/mindconverter/graph_based_converter/generator/generator.py View File

@@ -105,7 +105,7 @@ class CodeStruct:
init_lines += init_str init_lines += init_str
cons_lines += cons_str cons_lines += cons_str


else: # is ModuleStruct
else: # is ModuleStruct
# check if this instance generated CodeStruct # check if this instance generated CodeStruct
if GlobalContext().code_structs.get(struct.pattern_id) is None: if GlobalContext().code_structs.get(struct.pattern_id) is None:
CodeStruct(struct, repeated_submodules) CodeStruct(struct, repeated_submodules)


+ 0
- 34
mindinsight/mindconverter/graph_based_converter/mapper/gen_setting.py View File

@@ -1,34 +0,0 @@
# Copyright 2020 Huawei Technologies Co., Ltd.All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Operation mapping setting."""
from collections import namedtuple
import numpy as np

from mindinsight.mindconverter.graph_based_converter.constant import InputType

Tensor = namedtuple("Tensor", ["shape", "dtype", "reference"])

Setting = namedtuple("Setting", ["opt_vars_suffix",
"op_ipt_type",
"op_extra_input",
"op_extra_tensor"])
Setting.__new__.__defaults__ = ("_opt", InputType.TENSOR.value, dict(), None)


def get_dtype(tensor: np.ndarray):
"""Get tensor dtype."""
if tensor.dtype == np.float16:
return "mindspore.float16"
return "mindspore.float32"

+ 3
- 4
mindinsight/mindconverter/graph_based_converter/mapper/impl/nn/pool_mapper.py View File

@@ -54,7 +54,6 @@ class PoolMapper(ONNXToMindSporeMapper):
kernel_shape = params['kernel_shape'] kernel_shape = params['kernel_shape']
strides = params['strides'] strides = params['strides']
dilations = params.get('dilations', (1, 1)) dilations = params.get('dilations', (1, 1))

ms_opt_shape = np.true_divide(np.subtract(np.array(input_shape[-len(kernel_shape):], dtype=np.float32), ms_opt_shape = np.true_divide(np.subtract(np.array(input_shape[-len(kernel_shape):], dtype=np.float32),
((np.array(kernel_shape, dtype=np.float32) - 1) * ((np.array(kernel_shape, dtype=np.float32) - 1) *
np.array(dilations, dtype=np.float32) + 1)) + 1, np.array(dilations, dtype=np.float32) + 1)) + 1,
@@ -123,9 +122,9 @@ class PoolMapper(ONNXToMindSporeMapper):
if np.any(np.array(ms_opt_shape) > np.array(onnx_opt_shape)): if np.any(np.array(ms_opt_shape) > np.array(onnx_opt_shape)):
raise ValueError(f"ms_opt_shape[{ms_opt_shape}] should be no larger than onnx_opt_shape[{onnx_opt_shape}].") raise ValueError(f"ms_opt_shape[{ms_opt_shape}] should be no larger than onnx_opt_shape[{onnx_opt_shape}].")


shape_diff = np.subtract((np.array(onnx_opt_shape) - 1)*np.array(strides),
shape_diff = np.subtract((np.array(onnx_opt_shape) - 1) * np.array(strides),
np.subtract(np.array(onnx_ipt_shape), np.subtract(np.array(onnx_ipt_shape),
(np.array(kernel_shape) - 1)*np.array(dilations) + 1)).tolist()
(np.array(kernel_shape) - 1) * np.array(dilations) + 1)).tolist()


zero_pad_single = (0, 0) zero_pad_single = (0, 0)
paddings = [zero_pad_single] paddings = [zero_pad_single]
@@ -134,7 +133,7 @@ class PoolMapper(ONNXToMindSporeMapper):
paddings.append(zero_pad_single) paddings.append(zero_pad_single)


for axis_diff in shape_diff: for axis_diff in shape_diff:
paddings.append((int(axis_diff//2), int(axis_diff//2 + axis_diff % 2)))
paddings.append((int(axis_diff // 2), int(axis_diff // 2 + axis_diff % 2)))


init_template_pad = f"self.pad_{{{variable_slot}}} = nn.Pad(paddings={{paddings}})" init_template_pad = f"self.pad_{{{variable_slot}}} = nn.Pad(paddings={{paddings}})"
construct_template_pad = f"opt_{{{variable_slot}}} = self.pad_{{{variable_slot}}}" \ construct_template_pad = f"opt_{{{variable_slot}}} = self.pad_{{{variable_slot}}}" \


+ 1
- 1
mindinsight/mindconverter/graph_based_converter/mapper/impl/ops/slice_mapper.py View File

@@ -37,7 +37,7 @@ class SliceMapper(ONNXToMindSporeMapper):
def _generate_snippet_template(**kwargs): def _generate_snippet_template(**kwargs):
template, exchange_msg, outputs_list, outputs_mapping = ONNXToMindSporeMapper._generate_snippet_template( template, exchange_msg, outputs_list, outputs_mapping = ONNXToMindSporeMapper._generate_snippet_template(
**kwargs) **kwargs)
weights = [weight.value for weight in kwargs.get('weights')] # start, end, axis
weights = [weight.value for weight in kwargs.get('weights')] # start, end, axis
opt_shape = kwargs["raw_params"]["output_shape"] opt_shape = kwargs["raw_params"]["output_shape"]
if not weights: if not weights:
raise ValueError("Cannot get required params from slice.") raise ValueError("Cannot get required params from slice.")


+ 23
- 90
tests/ut/mindconverter/graph_based_converter/mapper/test_mapper.py View File

@@ -17,7 +17,6 @@ import numpy as np
import pytest import pytest


from mindinsight.mindconverter.graph_based_converter.mapper.base import ONNXToMindSporeMapper from mindinsight.mindconverter.graph_based_converter.mapper.base import ONNXToMindSporeMapper
from mindinsight.mindconverter.graph_based_converter.mapper.gen_setting import Setting




class TestMappers: class TestMappers:
@@ -30,16 +29,7 @@ class TestMappers:
'pads': [1, 2, 3, 4], 'pads': [1, 2, 3, 4],
'strides': [1, 1]}, 'strides': [1, 1]},
'weights': {'weight': np.zeros((64, 3, 1, 1), dtype=np.int32)}}, 'weights': {'weight': np.zeros((64, 3, 1, 1), dtype=np.int32)}},
'expected_output': {'converter_name': 'nn.Conv2d',
'converted_params': {'in_channels': 3,
'out_channels': 64,
'kernel_size': (1, 1),
'stride': (1, 1),
'padding': (1, 3, 2, 4),
'pad_mode': '\"pad\"',
'dilation': (1, 1),
'group': 1},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Conv', 'input': {'op_name': 'onnx::Conv',
'params': {'dilations': [1, 1], 'params': {'dilations': [1, 1],
@@ -47,44 +37,25 @@ class TestMappers:
'pads': [0, 0, 0, 0], 'pads': [0, 0, 0, 0],
'strides': [1, 1]}, 'strides': [1, 1]},
'weights': {'weight': np.zeros((64, 3, 2, 2), dtype=np.int32)}}, 'weights': {'weight': np.zeros((64, 3, 2, 2), dtype=np.int32)}},
'expected_output': {'converter_name': 'nn.Conv2d',
'converted_params': {'in_channels': 3,
'out_channels': 64,
'kernel_size': (2, 2),
'stride': (1, 1),
'padding': 0,
'pad_mode': '\"valid\"',
'dilation': (1, 1),
'group': 1},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Gemm', 'input': {'op_name': 'onnx::Gemm',
'params': dict(), 'params': dict(),
'weights': {'weight': np.zeros((10, 3), dtype=np.int32), 'weights': {'weight': np.zeros((10, 3), dtype=np.int32),
'bias': np.zeros((10, 1), dtype=np.int32)}}, 'bias': np.zeros((10, 1), dtype=np.int32)}},
'expected_output': {'converter_name': 'nn.Dense',
'converted_params': {'in_channels': 3,
'out_channels': 10,
'has_bias': True},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::BatchNormalization', 'input': {'op_name': 'onnx::BatchNormalization',
'params': {'epsilon': 1e-5, 'params': {'epsilon': 1e-5,
'momentum': 0.9, 'momentum': 0.9,
'output_shape': (1, 6, 224, 224)}, 'output_shape': (1, 6, 224, 224)},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.BatchNorm2d',
'converted_params': {'num_features': 6,
'eps': 1e-5,
'momentum': 0.9},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Relu', 'input': {'op_name': 'onnx::Relu',
'params': dict(), 'params': dict(),
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.ReLU',
'converted_params': dict(),
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::MaxPool', 'input': {'op_name': 'onnx::MaxPool',
'params': {'kernel_shape': [3, 3], 'params': {'kernel_shape': [3, 3],
@@ -93,11 +64,7 @@ class TestMappers:
'input_shape': (1, 3, 224, 224), 'input_shape': (1, 3, 224, 224),
'output_shape': (1, 3, 112, 112)}, 'output_shape': (1, 3, 112, 112)},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.MaxPool2d',
'converted_params': {'kernel_size': (3, 3),
'stride': (2, 2),
'pad_mode': '"same"'},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::AveragePool', 'input': {'op_name': 'onnx::AveragePool',
'params': {'kernel_shape': [3, 3], 'params': {'kernel_shape': [3, 3],
@@ -106,120 +73,86 @@ class TestMappers:
'input_shape': (1, 3, 224, 224), 'input_shape': (1, 3, 224, 224),
'output_shape': (1, 3, 112, 112)}, 'output_shape': (1, 3, 112, 112)},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.AvgPool2d',
'converted_params': {'kernel_size': (3, 3),
'stride': (2, 2),
'pad_mode': '"same"'},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::GlobalAveragePool', 'input': {'op_name': 'onnx::GlobalAveragePool',
'params': {'input_shape': (1, 3, 10, 10), 'params': {'input_shape': (1, 3, 10, 10),
'output_shape': (1, 3, 1, 1)}, 'output_shape': (1, 3, 1, 1)},
'weights': ''}, 'weights': ''},
'expected_output': {'converter_name': 'nn.AvgPool2d',
'converted_params': {'kernel_size': (10, 10)},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Flatten', 'input': {'op_name': 'onnx::Flatten',
'params': dict(), 'params': dict(),
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.Flatten',
'converted_params': dict(),
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Add', 'input': {'op_name': 'onnx::Add',
'params': dict(), 'params': dict(),
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'P.TensorAdd',
'converted_params': dict(),
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Pad', 'input': {'op_name': 'onnx::Pad',
'params': {'pads': [0, 1, 2, 3], 'params': {'pads': [0, 1, 2, 3],
'value': 0, 'value': 0,
'mode': 'constant'}, 'mode': 'constant'},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.Pad',
'converted_params': {'paddings': ((0, 2), (1, 3)),
'mode': '\"CONSTANT\"'},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Pad', 'input': {'op_name': 'onnx::Pad',
'params': {'pads': [0, 1, 2, 3], 'params': {'pads': [0, 1, 2, 3],
'mode': 'reflect'}, 'mode': 'reflect'},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.Pad',
'converted_params': {'paddings': ((0, 2), (1, 3)),
'mode': '\"REFLECT\"'},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Pad', 'input': {'op_name': 'onnx::Pad',
'params': {'pads': [0, 1, 2, 3], 'params': {'pads': [0, 1, 2, 3],
'value': 1, 'value': 1,
'mode': 'constant'}, 'mode': 'constant'},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.Pad',
'converted_params': {'paddings': ((0, 2), (1, 3)),
'mode': '{UNSUPPORTED: value is NOT 0}\"CONSTANT\"'},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Pad', 'input': {'op_name': 'onnx::Pad',
'params': {'pads': [0, 1, 2, 3], 'params': {'pads': [0, 1, 2, 3],
'mode': 'edge'}, 'mode': 'edge'},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.Pad',
'converted_params': {'paddings': ((0, 2), (1, 3)),
'mode': '{UNSUPPORTED: \"edge\"}\"UNKNOWN\"'},
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::ReduceMean', 'input': {'op_name': 'onnx::ReduceMean',
'params': {'keepdims': 0, 'params': {'keepdims': 0,
'axes': [1, 2]}, 'axes': [1, 2]},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'P.ReduceMean',
'converted_params': {'keep_dims': False},
'converted_settings': {'values': {'axis': (1, 2)}}}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::ReduceMean', 'input': {'op_name': 'onnx::ReduceMean',
'params': {'keepdims': 1, 'params': {'keepdims': 1,
'axes': [1]}, 'axes': [1]},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'P.ReduceMean',
'converted_params': {'keep_dims': True},
'converted_settings': {'values': {'axis': 1}}}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Concat', 'input': {'op_name': 'onnx::Concat',
'params': {'axis': 0}, 'params': {'axis': 0},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'P.Concat',
'converted_params': {'axis': 0},
'converted_settings': {'input_type': "list"}}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Clip', 'input': {'op_name': 'onnx::Clip',
'params': {'max': 6, 'params': {'max': 6,
'min': 0}, 'min': 0},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.ReLU6',
'converted_params': dict(),
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Clip', 'input': {'op_name': 'onnx::Clip',
'params': dict(), 'params': dict(),
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': 'nn.ReLU',
'converted_params': dict(),
'converted_settings': Setting()}
'expected_output': {}
}, { }, {
'input': {'op_name': 'onnx::Clip', 'input': {'op_name': 'onnx::Clip',
'params': {'max': 3, 'params': {'max': 3,
'min': 2}, 'min': 2},
'weights': dict()}, 'weights': dict()},
'expected_output': {'converter_name': None,
'converted_params': dict(),
'converted_settings': Setting()}
'expected_output': {}
}]) }])
def test_mapper(self, params): def test_mapper(self, params):
"""Test mapper function.""" """Test mapper function."""
mapper = ONNXToMindSporeMapper()
_, _, _, _ = \
mapper.convert(params['input']['op_name'], params['input']['params'], params['input']['weights'])
_, _, _, _ = ONNXToMindSporeMapper.convert(params['input']['op_name'],
params['input']['params'],
params['input']['weights'])

Loading…
Cancel
Save