未验证 提交 58e1668e 编写于 作者: J Jason 提交者: GitHub

Merge pull request #291 from PaddlePaddle/develop_code-format

code format
- repo: local - repo: https://github.com/PaddlePaddle/mirrors-yapf.git
sha: 0d79c0c469bab64f7229c9aca2b1186ef47f0e37
hooks: hooks:
- id: yapf - id: yapf
name: yapf
entry: yapf
language: system
args: [-i, --style .style.yapf]
files: \.py$ files: \.py$
- repo: https://github.com/pre-commit/pre-commit-hooks - repo: https://github.com/pre-commit/pre-commit-hooks
sha: a11d9314b22d8f8c7556443875b731ef05965464 sha: a11d9314b22d8f8c7556443875b731ef05965464
hooks: hooks:
...@@ -18,6 +14,7 @@ ...@@ -18,6 +14,7 @@
- id: check-symlinks - id: check-symlinks
- id: check-added-large-files - id: check-added-large-files
- repo: local - repo: local
hooks: hooks:
- id: copyright_checker - id: copyright_checker
name: copyright_checker name: copyright_checker
......
language: python language: python
python: python:
- '2.7'
- '3.5' - '3.5'
- '3.6'
script: script:
- if [[ $TRAVIS_PYTHON_VERSION != 2.7 ]]; then /bin/bash ./tools/check_code_style.sh; fi - if [[ $TRAVIS_PYTHON_VERSION != 2.7 ]]; then /bin/bash ./tools/check_code_style.sh; fi
......
...@@ -11,8 +11,7 @@ setuptools.setup( ...@@ -11,8 +11,7 @@ setuptools.setup(
version=x2paddle.__version__, version=x2paddle.__version__,
author="dltp-sz", author="dltp-sz",
author_email="dltp-sz@baidu.com", author_email="dltp-sz@baidu.com",
description= description="a toolkit for converting trained model to PaddlePaddle from other deep learning frameworks.",
"a toolkit for converting trained model to PaddlePaddle from other deep learning frameworks.",
long_description=long_description, long_description=long_description,
long_description_content_type="text/plain", long_description_content_type="text/plain",
url="https://github.com/PaddlePaddle/x2paddle", url="https://github.com/PaddlePaddle/x2paddle",
...@@ -23,6 +22,4 @@ setuptools.setup( ...@@ -23,6 +22,4 @@ setuptools.setup(
"Operating System :: OS Independent", "Operating System :: OS Independent",
], ],
license='Apache 2.0', license='Apache 2.0',
entry_points={'console_scripts': [ entry_points={'console_scripts': ['x2paddle=x2paddle.convert:main', ]})
'x2paddle=x2paddle.convert:main',
]})
...@@ -5,10 +5,12 @@ model_dir = sys.argv[1] ...@@ -5,10 +5,12 @@ model_dir = sys.argv[1]
new_model_dir = sys.argv[2] new_model_dir = sys.argv[2]
exe = fluid.Executor(fluid.CPUPlace()) exe = fluid.Executor(fluid.CPUPlace())
[inference_program, feed_target_names, [inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(dirname=model_dir, executor=exe) fetch_targets] = fluid.io.load_inference_model(
dirname=model_dir, executor=exe)
print(feed_target_names) print(feed_target_names)
fluid.io.save_inference_model(dirname=new_model_dir, fluid.io.save_inference_model(
dirname=new_model_dir,
feeded_var_names=feed_target_names, feeded_var_names=feed_target_names,
target_vars=fetch_targets, target_vars=fetch_targets,
executor=exe, executor=exe,
......
...@@ -48,8 +48,7 @@ def arg_parser(): ...@@ -48,8 +48,7 @@ def arg_parser():
"-f", "-f",
type=_text_type, type=_text_type,
default=None, default=None,
help= help="define which deeplearning framework(tensorflow/caffe/onnx/paddle2onnx)"
"define which deeplearning framework(tensorflow/caffe/onnx/paddle2onnx)"
) )
parser.add_argument( parser.add_argument(
"--caffe_proto", "--caffe_proto",
...@@ -126,7 +125,6 @@ def tf2paddle(model_path, ...@@ -126,7 +125,6 @@ def tf2paddle(model_path,
optimizer.merge_bias() optimizer.merge_bias()
optimizer.optimize_sub_graph() optimizer.optimize_sub_graph()
# optimizer.merge_batch_norm() # optimizer.merge_batch_norm()
# optimizer.merge_prelu() # optimizer.merge_prelu()
else: else:
......
...@@ -46,8 +46,9 @@ class Layer(object): ...@@ -46,8 +46,9 @@ class Layer(object):
for input in self.inputs: for input in self.inputs:
if isinstance(input, GraphNode): if isinstance(input, GraphNode):
if hasattr(input, "index"): if hasattr(input, "index"):
in_list += (input.layer_name + in_list += (
"[{}]".format(input.index) + ", ") input.layer_name + "[{}]".format(input.index) + ", "
)
else: else:
in_list += (input.layer_name + ", ") in_list += (input.layer_name + ", ")
elif isinstance(input, six.string_types): elif isinstance(input, six.string_types):
...@@ -71,8 +72,8 @@ class Layer(object): ...@@ -71,8 +72,8 @@ class Layer(object):
layer_code = layer_code + key + "={}, ".format(input) layer_code = layer_code + key + "={}, ".format(input)
elif isinstance(self.inputs, GraphNode): elif isinstance(self.inputs, GraphNode):
if hasattr(self.inputs, "index"): if hasattr(self.inputs, "index"):
layer_code += (self.inputs.layer_name + layer_code += (
"[{}]".format(self.inputs.index)) self.inputs.layer_name + "[{}]".format(self.inputs.index))
else: else:
layer_code += (self.inputs.layer_name) layer_code += (self.inputs.layer_name)
if self.op != "=": if self.op != "=":
......
...@@ -64,10 +64,8 @@ def run_net(param_dir="./"): ...@@ -64,10 +64,8 @@ def run_net(param_dir="./"):
b = os.path.exists(os.path.join(param_dir, var.name)) b = os.path.exists(os.path.join(param_dir, var.name))
return b return b
fluid.io.load_vars(exe, fluid.io.load_vars(
param_dir, exe, param_dir, fluid.default_main_program(), predicate=if_exist)
fluid.default_main_program(),
predicate=if_exist)
class OpMapper(object): class OpMapper(object):
...@@ -98,8 +96,8 @@ class OpMapper(object): ...@@ -98,8 +96,8 @@ class OpMapper(object):
def add_codes(self, codes, indent=0): def add_codes(self, codes, indent=0):
if isinstance(codes, list): if isinstance(codes, list):
for code in codes: for code in codes:
self.paddle_codes += (self.tab * indent + code.strip('\n') + self.paddle_codes += (
'\n') self.tab * indent + code.strip('\n') + '\n')
elif isinstance(codes, str): elif isinstance(codes, str):
self.paddle_codes += (self.tab * indent + codes.strip('\n') + '\n') self.paddle_codes += (self.tab * indent + codes.strip('\n') + '\n')
else: else:
...@@ -135,20 +133,21 @@ class OpMapper(object): ...@@ -135,20 +133,21 @@ class OpMapper(object):
os.path.join(os.path.join(py_code_dir, var.name))) os.path.join(os.path.join(py_code_dir, var.name)))
return b return b
fluid.io.load_vars(exe, fluid.io.load_vars(
exe,
py_code_dir, py_code_dir,
fluid.default_main_program(), fluid.default_main_program(),
predicate=if_exist) predicate=if_exist)
if params_merge: if params_merge:
fluid.io.save_inference_model(dirname=os.path.join( fluid.io.save_inference_model(
save_dir, "inference_model"), dirname=os.path.join(save_dir, "inference_model"),
feeded_var_names=input_names, feeded_var_names=input_names,
target_vars=outputs, target_vars=outputs,
executor=exe, executor=exe,
params_filename="__params__") params_filename="__params__")
else: else:
fluid.io.save_inference_model(dirname=os.path.join( fluid.io.save_inference_model(
save_dir, "inference_model"), dirname=os.path.join(save_dir, "inference_model"),
feeded_var_names=input_names, feeded_var_names=input_names,
target_vars=outputs, target_vars=outputs,
executor=exe, executor=exe,
......
...@@ -49,13 +49,11 @@ class CaffeResolver(object): ...@@ -49,13 +49,11 @@ class CaffeResolver(object):
class CaffeGraphNode(GraphNode): class CaffeGraphNode(GraphNode):
def __init__(self, layer, type_str, layer_name=None): def __init__(self, layer, type_str, layer_name=None):
if layer_name is None: if layer_name is None:
super(CaffeGraphNode, super(CaffeGraphNode, self).__init__(
self).__init__(layer, layer, layer.name.replace('/', '_').replace('-', '_'))
layer.name.replace('/', '_').replace('-', '_'))
else: else:
super(CaffeGraphNode, super(CaffeGraphNode, self).__init__(
self).__init__(layer, layer, layer_name.replace('/', '_').replace('-', '_'))
layer_name.replace('/', '_').replace('-', '_'))
self.layer_type = type_str self.layer_type = type_str
self.fluid_code = FluidCode() self.fluid_code = FluidCode()
self.data = None self.data = None
...@@ -268,8 +266,8 @@ class CaffeDecoder(object): ...@@ -268,8 +266,8 @@ class CaffeDecoder(object):
c_i = blob.channels c_i = blob.channels
h = blob.height h = blob.height
w = blob.width w = blob.width
data = np.asarray(list(blob.data), data = np.asarray(
dtype=np.float32).reshape(c_o, c_i, h, w) list(blob.data), dtype=np.float32).reshape(c_o, c_i, h, w)
transformed.append(data) transformed.append(data)
return transformed return transformed
因为 它太大了无法显示 source diff 。你可以改为 查看blob
...@@ -71,9 +71,8 @@ class ONNXGraphNode(GraphNode): ...@@ -71,9 +71,8 @@ class ONNXGraphNode(GraphNode):
if attr.type == onnx.AttributeProto.TENSOR: if attr.type == onnx.AttributeProto.TENSOR:
dtype = np.dtype(TENSOR_TYPE_TO_NP_TYPE[attr.t.data_type]) dtype = np.dtype(TENSOR_TYPE_TO_NP_TYPE[attr.t.data_type])
data = attr.t.raw_data data = attr.t.raw_data
value = np.frombuffer(data, value = np.frombuffer(
dtype=dtype, data, dtype=dtype, count=(len(data) // dtype.itemsize))
count=(len(data) // dtype.itemsize))
elif attr.type == onnx.AttributeProto.STRING: elif attr.type == onnx.AttributeProto.STRING:
value = attr.s value = attr.s
value = value.decode() if isinstance(value, bytes) else value value = value.decode() if isinstance(value, bytes) else value
...@@ -205,9 +204,8 @@ class ONNXGraph(Graph): ...@@ -205,9 +204,8 @@ class ONNXGraph(Graph):
self.node_map[name].weight = weight self.node_map[name].weight = weight
self.node_map[name].embeded_as = [] self.node_map[name].embeded_as = []
else: else:
self.node_map[name] = ONNXGraphDataNode(initializer, self.node_map[name] = ONNXGraphDataNode(
layer_name=name, initializer, layer_name=name, is_global_input=False)
is_global_input=False)
self.node_map[name].weight = weight self.node_map[name].weight = weight
self.node_map[name].embeded_as = [] self.node_map[name].embeded_as = []
...@@ -494,8 +492,8 @@ class ONNXDecoder(object): ...@@ -494,8 +492,8 @@ class ONNXDecoder(object):
sess = rt.InferenceSession(model_path) sess = rt.InferenceSession(model_path)
for ipt in sess.get_inputs(): for ipt in sess.get_inputs():
datatype = datatype_map[ipt.type] datatype = datatype_map[ipt.type]
input_dict[ipt.name] = np.random.random( input_dict[ipt.name] = np.random.random(ipt.shape).astype(
ipt.shape).astype(datatype) datatype)
res = sess.run(None, input_feed=input_dict) res = sess.run(None, input_feed=input_dict)
except: except:
......
...@@ -120,13 +120,13 @@ class TFGraph(Graph): ...@@ -120,13 +120,13 @@ class TFGraph(Graph):
def build(self): def build(self):
for layer in self.model.node: for layer in self.model.node:
self.node_map[layer.name.replace('/', '_').replace( self.node_map[layer.name.replace('/', '_').replace(
'-', '_')] = TFGraphNode(layer, data_format=self.tf_data_format) '-', '_')] = TFGraphNode(
layer, data_format=self.tf_data_format)
for layer_name, node in self.node_map.items(): for layer_name, node in self.node_map.items():
for in_node in node.layer.input: for in_node in node.layer.input:
in_node = in_node.replace('/', in_node = in_node.replace('/', '_').replace('-', '_').replace(
'_').replace('-', '^', '')
'_').replace('^', '')
if in_node not in self.node_map: if in_node not in self.node_map:
if in_node.strip().split(':')[0] in self.node_map: if in_node.strip().split(':')[0] in self.node_map:
self.connect(in_node.strip().split(':')[0], layer_name) self.connect(in_node.strip().split(':')[0], layer_name)
...@@ -390,10 +390,10 @@ class TFDecoder(object): ...@@ -390,10 +390,10 @@ class TFDecoder(object):
shape=shape, shape=shape,
name="x2paddle_{}".format(layer.name)) name="x2paddle_{}".format(layer.name))
except: except:
x2paddle_input = tf.placeholder(dtype=dtype, x2paddle_input = tf.placeholder(
dtype=dtype,
shape=shape, shape=shape,
name="x2paddle_{}".format( name="x2paddle_{}".format(layer.name))
layer.name))
input_map["{}:0".format(layer.name)] = x2paddle_input input_map["{}:0".format(layer.name)] = x2paddle_input
if shape.count(None) > 0: if shape.count(None) > 0:
......
...@@ -122,7 +122,8 @@ def convolutiondepthwise_layer(inputs, ...@@ -122,7 +122,8 @@ def convolutiondepthwise_layer(inputs,
c_out = num_output if num_output is not None else input_shape[0][1] c_out = num_output if num_output is not None else input_shape[0][1]
group = int(c_in / (c_in / c_out)) if c_in > c_out else int(c_in / group = int(c_in / (c_in / c_out)) if c_in > c_out else int(c_in /
(c_out / c_in)) (c_out / c_in))
out = fluid.layers.conv2d(input, out = fluid.layers.conv2d(
input,
dilation=[dila_h, dila_w], dilation=[dila_h, dila_w],
filter_size=[k_h, k_w], filter_size=[k_h, k_w],
stride=[s_h, s_w], stride=[s_h, s_w],
...@@ -142,7 +143,8 @@ def convolutiondepthwise_weights(name, data=None): ...@@ -142,7 +143,8 @@ def convolutiondepthwise_weights(name, data=None):
return weights_name return weights_name
register(kind='ConvolutionDepthwise', register(
kind='ConvolutionDepthwise',
shape=convolutiondepthwise_shape, shape=convolutiondepthwise_shape,
layer=convolutiondepthwise_layer, layer=convolutiondepthwise_layer,
weights=convolutiondepthwise_weights) weights=convolutiondepthwise_weights)
...@@ -37,8 +37,8 @@ def detectionoutput_layer(inputs, ...@@ -37,8 +37,8 @@ def detectionoutput_layer(inputs,
pbv = fluid.layers.reshape(x=pbv, shape=[-1, 4]) pbv = fluid.layers.reshape(x=pbv, shape=[-1, 4])
mbox_loc = inputs[0] mbox_loc = inputs[0]
mbox_loc = fluid.layers.reshape(x=mbox_loc, shape=[-1, pb.shape[0], 4]) mbox_loc = fluid.layers.reshape(x=mbox_loc, shape=[-1, pb.shape[0], 4])
mbox_conf_flatten = fluid.layers.reshape(x=mbox_conf_flatten, mbox_conf_flatten = fluid.layers.reshape(
shape=[0, pb.shape[0], -1]) x=mbox_conf_flatten, shape=[0, pb.shape[0], -1])
default = {"nms_threshold": 0.3, "top_k": 10, "eta": 1.0} default = {"nms_threshold": 0.3, "top_k": 10, "eta": 1.0}
fields = ['eta', 'top_k', 'nms_threshold'] fields = ['eta', 'top_k', 'nms_threshold']
...@@ -64,7 +64,8 @@ def detectionoutput_weights(name, data=None): ...@@ -64,7 +64,8 @@ def detectionoutput_weights(name, data=None):
return weights_name return weights_name
register(kind='DetectionOutput', register(
kind='DetectionOutput',
shape=detectionoutput_shape, shape=detectionoutput_shape,
layer=detectionoutput_layer, layer=detectionoutput_layer,
weights=detectionoutput_weights) weights=detectionoutput_weights)
...@@ -20,9 +20,8 @@ def normalize_layer(inputs, ...@@ -20,9 +20,8 @@ def normalize_layer(inputs,
attr=name + '_scale') attr=name + '_scale')
scale_param = fluid.layers.reshape(x=scale_param, \ scale_param = fluid.layers.reshape(x=scale_param, \
shape=[1] if channel_shared else [input_shape[0][1]]) shape=[1] if channel_shared else [input_shape[0][1]])
out = fluid.layers.elementwise_mul(x=l2_norm, out = fluid.layers.elementwise_mul(
y=scale_param, x=l2_norm, y=scale_param, axis=-1 if channel_shared else 1)
axis=-1 if channel_shared else 1)
return out return out
...@@ -31,7 +30,8 @@ def normalize_weights(name, data=None): ...@@ -31,7 +30,8 @@ def normalize_weights(name, data=None):
return weights_name return weights_name
register(kind='Normalize', register(
kind='Normalize',
shape=normalize_shape, shape=normalize_shape,
layer=normalize_layer, layer=normalize_layer,
weights=normalize_weights) weights=normalize_weights)
...@@ -23,7 +23,8 @@ def permute_weights(name, data=None): ...@@ -23,7 +23,8 @@ def permute_weights(name, data=None):
return weights_name return weights_name
register(kind='Permute', register(
kind='Permute',
shape=permute_shape, shape=permute_shape,
layer=permute_layer, layer=permute_layer,
weights=permute_weights) weights=permute_weights)
...@@ -30,7 +30,8 @@ def priorbox_layer(inputs, ...@@ -30,7 +30,8 @@ def priorbox_layer(inputs,
steps = tuple(step) if type(step) is list or type(step) is tuple else (step, steps = tuple(step) if type(step) is list or type(step) is tuple else (step,
step) step)
box, variance_ = fluid.layers.prior_box(input, box, variance_ = fluid.layers.prior_box(
input,
image, image,
min_sizes=min_size, min_sizes=min_size,
max_sizes=max_size, max_sizes=max_size,
...@@ -53,7 +54,8 @@ def priorbox_weights(name, data=None): ...@@ -53,7 +54,8 @@ def priorbox_weights(name, data=None):
return weights_name return weights_name
register(kind='PriorBox', register(
kind='PriorBox',
shape=priorbox_shape, shape=priorbox_shape,
layer=priorbox_layer, layer=priorbox_layer,
weights=priorbox_weights) weights=priorbox_weights)
...@@ -23,8 +23,7 @@ def register(kind, shape, layer, weights): ...@@ -23,8 +23,7 @@ def register(kind, shape, layer, weights):
kind = [kind] kind = [kind]
else: else:
assert type( assert type(
kind kind) is list, 'invalid param "kind" for register, not a list or str'
) is list, 'invalid param "kind" for register, not a list or str'
for k in kind: for k in kind:
assert type( assert type(
......
...@@ -21,7 +21,8 @@ def roipooling_layer(inputs, ...@@ -21,7 +21,8 @@ def roipooling_layer(inputs,
input = inputs[0] input = inputs[0]
roi = inputs[1] roi = inputs[1]
roi = fluid.layers.slice(roi, axes=[1], starts=[1], ends=[5]) roi = fluid.layers.slice(roi, axes=[1], starts=[1], ends=[5])
out = fluid.layers.roi_pool(input, out = fluid.layers.roi_pool(
input,
roi, roi,
pooled_height=pooled_h, pooled_height=pooled_h,
pooled_width=pooled_w, pooled_width=pooled_w,
...@@ -34,7 +35,8 @@ def roipooling_weights(name, data=None): ...@@ -34,7 +35,8 @@ def roipooling_weights(name, data=None):
return weights_name return weights_name
register(kind='ROIPooling', register(
kind='ROIPooling',
shape=roipooling_shape, shape=roipooling_shape,
layer=roipooling_layer, layer=roipooling_layer,
weights=roipooling_weights) weights=roipooling_weights)
...@@ -30,7 +30,8 @@ def select_layer(inputs, ...@@ -30,7 +30,8 @@ def select_layer(inputs,
out = [] out = []
for i in range(len(slice_point)): for i in range(len(slice_point)):
out.append( out.append(
fluid.layers.slice(input, fluid.layers.slice(
input,
axes=[axis], axes=[axis],
starts=[slice_point[i]], starts=[slice_point[i]],
ends=[slice_point[i + 1]], ends=[slice_point[i + 1]],
...@@ -45,7 +46,8 @@ def select_weights(name, data=None): ...@@ -45,7 +46,8 @@ def select_weights(name, data=None):
return weights_name return weights_name
register(kind='Select', register(
kind='Select',
shape=select_shape, shape=select_shape,
layer=select_layer, layer=select_layer,
weights=select_weights) weights=select_weights)
...@@ -17,7 +17,8 @@ def shufflechannel_weights(name, data=None): ...@@ -17,7 +17,8 @@ def shufflechannel_weights(name, data=None):
return weights_name return weights_name
register(kind='ShuffleChannel', register(
kind='ShuffleChannel',
shape=shufflechannel_shape, shape=shufflechannel_shape,
layer=shufflechannel_layer, layer=shufflechannel_layer,
weights=shufflechannel_weights) weights=shufflechannel_weights)
...@@ -144,8 +144,8 @@ class CaffeOpMapper(OpMapper): ...@@ -144,8 +144,8 @@ class CaffeOpMapper(OpMapper):
[s_h, s_w] = [params.stride] * 2 [s_h, s_w] = [params.stride] * 2
elif len(params.stride) > 0: elif len(params.stride) > 0:
s_h = params.stride_h if params.stride_h > 0 else params.stride[0] s_h = params.stride_h if params.stride_h > 0 else params.stride[0]
s_w = params.stride_w if params.stride_w > 0 else params.stride[ s_w = params.stride_w if params.stride_w > 0 else params.stride[len(
len(params.stride) - 1] params.stride) - 1]
elif params.stride_h > 0 or params.stride_w > 0: elif params.stride_h > 0 or params.stride_w > 0:
s_h = params.stride_h s_h = params.stride_h
s_w = params.stride_w s_w = params.stride_w
...@@ -154,8 +154,8 @@ class CaffeOpMapper(OpMapper): ...@@ -154,8 +154,8 @@ class CaffeOpMapper(OpMapper):
[p_h, p_w] = [params.pad] * 2 [p_h, p_w] = [params.pad] * 2
elif len(params.pad) > 0: elif len(params.pad) > 0:
p_h = params.pad_h if params.pad_h > 0 else params.pad[0] p_h = params.pad_h if params.pad_h > 0 else params.pad[0]
p_w = params.pad_w if params.pad_w > 0 else params.pad[ p_w = params.pad_w if params.pad_w > 0 else params.pad[len(
len(params.pad) - 1] params.pad) - 1]
elif params.pad_h > 0 or params.pad_w > 0: elif params.pad_h > 0 or params.pad_w > 0:
p_h = params.pad_h p_h = params.pad_h
p_w = params.pad_w p_w = params.pad_w
...@@ -195,10 +195,8 @@ class CaffeOpMapper(OpMapper): ...@@ -195,10 +195,8 @@ class CaffeOpMapper(OpMapper):
'shape': shape, 'shape': shape,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("data", node.fluid_code.add_layer(
inputs=None, "data", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def MemoryData(self, node): def MemoryData(self, node):
# TODO(syf): Paddlepaddle can't fully support # TODO(syf): Paddlepaddle can't fully support
...@@ -209,10 +207,8 @@ class CaffeOpMapper(OpMapper): ...@@ -209,10 +207,8 @@ class CaffeOpMapper(OpMapper):
'shape': shape, 'shape': shape,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("data", node.fluid_code.add_layer(
inputs=None, "data", inputs=None, output=node.layer_name + '0', param_attr=attr)
output=node.layer_name + '0',
param_attr=attr)
node.fluid_code.add_note('{} = [{}]'.format(node.layer_name, node.fluid_code.add_note('{} = [{}]'.format(node.layer_name,
node.layer_name + '0')) node.layer_name + '0'))
...@@ -229,11 +225,9 @@ class CaffeOpMapper(OpMapper): ...@@ -229,11 +225,9 @@ class CaffeOpMapper(OpMapper):
input_c = node.input_shape[0][1] input_c = node.input_shape[0][1]
output_c = channel output_c = channel
data.append( data.append(
np.zeros([output_c, input_c, kernel[0], np.zeros([output_c, input_c, kernel[0], kernel[1]]).astype(
kernel[1]]).astype('float32')) 'float32'))
data.append(np.zeros([ data.append(np.zeros([output_c, ])).astype('float32')
output_c,
])).astype('float32')
else: else:
data = self.adjust_parameters(node) data = self.adjust_parameters(node)
self.weights[node.layer_name + '_weights'] = data[0] self.weights[node.layer_name + '_weights'] = data[0]
...@@ -244,29 +238,19 @@ class CaffeOpMapper(OpMapper): ...@@ -244,29 +238,19 @@ class CaffeOpMapper(OpMapper):
input = self.graph.get_bottom_node(node, idx=0, copy=True) input = self.graph.get_bottom_node(node, idx=0, copy=True)
attr = { attr = {
'filter_size': 'filter_size': kernel,
kernel, 'num_filters': channel,
'num_filters': 'stride': stride,
channel, 'padding': pad,
'stride': 'dilation': dilation,
stride, 'groups': group,
'padding': 'name': string(node.layer_name),
pad, 'param_attr': string(node.layer_name + '_weights'),
'dilation': 'bias_attr': False
dilation, if len(data) == 1 else string(node.layer_name + '_bias'),
'groups':
group,
'name':
string(node.layer_name),
'param_attr':
string(node.layer_name + '_weights'),
'bias_attr':
False if len(data) == 1 else string(node.layer_name + '_bias'),
} }
node.fluid_code.add_layer("conv2d", node.fluid_code.add_layer(
inputs=input, "conv2d", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Deconvolution(self, node): def Deconvolution(self, node):
data = node.data data = node.data
...@@ -281,11 +265,9 @@ class CaffeOpMapper(OpMapper): ...@@ -281,11 +265,9 @@ class CaffeOpMapper(OpMapper):
input_c = node.input_shape[0][1] input_c = node.input_shape[0][1]
output_c = channel output_c = channel
data.append( data.append(
np.zeros([output_c, input_c, kernel[0], np.zeros([output_c, input_c, kernel[0], kernel[1]]).astype(
kernel[1]]).astype('float32')) 'float32'))
data.append(np.zeros([ data.append(np.zeros([output_c, ]).astype('float32'))
output_c,
]).astype('float32'))
else: else:
data = self.adjust_parameters(node) data = self.adjust_parameters(node)
self.weights[node.layer_name + '_weights'] = data[0] self.weights[node.layer_name + '_weights'] = data[0]
...@@ -295,31 +277,20 @@ class CaffeOpMapper(OpMapper): ...@@ -295,31 +277,20 @@ class CaffeOpMapper(OpMapper):
) == 1, 'The count of Deconvolution node\'s input is not 1.' ) == 1, 'The count of Deconvolution node\'s input is not 1.'
input = self.graph.get_bottom_node(node, idx=0, copy=True) input = self.graph.get_bottom_node(node, idx=0, copy=True)
attr = { attr = {
'output_size': 'output_size': None,
None, 'filter_size': kernel,
'filter_size': 'num_filters': channel,
kernel, 'stride': stride,
'num_filters': 'padding': pad,
channel, 'dilation': dilation,
'stride': 'groups': group,
stride, 'name': string(node.layer_name),
'padding': 'param_attr': string(node.layer_name + '_weights'),
pad, 'bias_attr': False
'dilation': if len(data) == 1 else string(node.layer_name + '_bias')
dilation,
'groups':
group,
'name':
string(node.layer_name),
'param_attr':
string(node.layer_name + '_weights'),
'bias_attr':
False if len(data) == 1 else string(node.layer_name + '_bias')
} }
node.fluid_code.add_layer("conv2d_transpose", node.fluid_code.add_layer(
inputs=input, "conv2d_transpose", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Pooling(self, node): def Pooling(self, node):
params = node.layer.pooling_param params = node.layer.pooling_param
...@@ -345,10 +316,8 @@ class CaffeOpMapper(OpMapper): ...@@ -345,10 +316,8 @@ class CaffeOpMapper(OpMapper):
'global_pooling': global_pool, 'global_pooling': global_pool,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("pool2d", node.fluid_code.add_layer(
inputs=input, "pool2d", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def LRN(self, node): def LRN(self, node):
assert len(node.inputs) == 1, 'The count of LRN node\'s input is not 1.' assert len(node.inputs) == 1, 'The count of LRN node\'s input is not 1.'
...@@ -368,10 +337,8 @@ class CaffeOpMapper(OpMapper): ...@@ -368,10 +337,8 @@ class CaffeOpMapper(OpMapper):
'beta': params.beta, 'beta': params.beta,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("lrn", node.fluid_code.add_layer(
inputs=input, "lrn", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def InnerProduct(self, node): def InnerProduct(self, node):
data = node.data data = node.data
...@@ -384,8 +351,8 @@ class CaffeOpMapper(OpMapper): ...@@ -384,8 +351,8 @@ class CaffeOpMapper(OpMapper):
output_c = params.num_output output_c = params.num_output
data = [] data = []
data.append( data.append(
np.zeros([input_c, np.zeros([input_c, output_c]).astype('float32').astype(
output_c]).astype('float32').astype('float32')) 'float32'))
data.append( data.append(
np.zeros([output_c]).astype('float32').astype('float32')) np.zeros([output_c]).astype('float32').astype('float32'))
else: else:
...@@ -409,21 +376,15 @@ class CaffeOpMapper(OpMapper): ...@@ -409,21 +376,15 @@ class CaffeOpMapper(OpMapper):
assert params.bias_term == True assert params.bias_term == True
input = self.graph.get_bottom_node(node, idx=0, copy=True) input = self.graph.get_bottom_node(node, idx=0, copy=True)
attr = { attr = {
'size': 'size': params.num_output,
params.num_output, 'name': string(node.layer_name),
'name': 'act': None,
string(node.layer_name), 'param_attr': string(node.layer_name + '_weights'),
'act': 'bias_attr': False
None, if len(data) == 1 else string(node.layer_name + '_bias')
'param_attr':
string(node.layer_name + '_weights'),
'bias_attr':
False if len(data) == 1 else string(node.layer_name + '_bias')
} }
node.fluid_code.add_layer("fc", node.fluid_code.add_layer(
inputs=input, "fc", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Softmax(self, node): def Softmax(self, node):
assert len( assert len(
...@@ -435,10 +396,8 @@ class CaffeOpMapper(OpMapper): ...@@ -435,10 +396,8 @@ class CaffeOpMapper(OpMapper):
dims = len(shape) dims = len(shape)
axis = axis + dims if axis < 0 else axis axis = axis + dims if axis < 0 else axis
attr = {'axis': axis, 'name': string(node.layer_name + '_softmax')} attr = {'axis': axis, 'name': string(node.layer_name + '_softmax')}
node.fluid_code.add_layer("softmax", node.fluid_code.add_layer(
inputs=input, "softmax", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Slice(self, node): def Slice(self, node):
assert len( assert len(
...@@ -459,10 +418,8 @@ class CaffeOpMapper(OpMapper): ...@@ -459,10 +418,8 @@ class CaffeOpMapper(OpMapper):
'dim': axis, 'dim': axis,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("split", node.fluid_code.add_layer(
inputs=input, "split", inputs=input, output=node.layer_name, param_attr=attr)
output=node.layer_name,
param_attr=attr)
def Concat(self, node): def Concat(self, node):
assert len( assert len(
...@@ -475,10 +432,8 @@ class CaffeOpMapper(OpMapper): ...@@ -475,10 +432,8 @@ class CaffeOpMapper(OpMapper):
params = node.layer.concat_param params = node.layer.concat_param
axis = params.axis axis = params.axis
attr = {'axis': axis, 'name': string(node.layer_name)} attr = {'axis': axis, 'name': string(node.layer_name)}
node.fluid_code.add_layer("concat", node.fluid_code.add_layer(
inputs=inputs, "concat", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def PReLU(self, node): def PReLU(self, node):
assert len( assert len(
...@@ -499,10 +454,8 @@ class CaffeOpMapper(OpMapper): ...@@ -499,10 +454,8 @@ class CaffeOpMapper(OpMapper):
'param_attr': string(node.layer_name + '_weights'), 'param_attr': string(node.layer_name + '_weights'),
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("prelu", node.fluid_code.add_layer(
inputs=input, "prelu", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Accuracy(self, node): def Accuracy(self, node):
assert len( assert len(
...@@ -526,10 +479,8 @@ class CaffeOpMapper(OpMapper): ...@@ -526,10 +479,8 @@ class CaffeOpMapper(OpMapper):
assert axis == 1, 'PaddlePaddle can not support the situation when the axis is not 1.' assert axis == 1, 'PaddlePaddle can not support the situation when the axis is not 1.'
assert not ignore_label >= 0, 'PaddlePaddle can not support the situation when the model has ignore label.' assert not ignore_label >= 0, 'PaddlePaddle can not support the situation when the model has ignore label.'
attr = {'k': top_k} attr = {'k': top_k}
node.fluid_code.add_layer("accuracy", node.fluid_code.add_layer(
inputs=inputs, "accuracy", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Eltwise(self, node): def Eltwise(self, node):
assert len( assert len(
...@@ -546,7 +497,8 @@ class CaffeOpMapper(OpMapper): ...@@ -546,7 +497,8 @@ class CaffeOpMapper(OpMapper):
inputs_dict['x'] = inputs[0] inputs_dict['x'] = inputs[0]
inputs_dict['y'] = inputs[1] inputs_dict['y'] = inputs[1]
attr = {'act': None, 'name': string(node.layer_name)} attr = {'act': None, 'name': string(node.layer_name)}
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
"elementwise_mul",
inputs=inputs_dict, inputs=inputs_dict,
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -559,14 +511,15 @@ class CaffeOpMapper(OpMapper): ...@@ -559,14 +511,15 @@ class CaffeOpMapper(OpMapper):
'value': coeff[0], 'value': coeff[0],
'dtype': '{}.dtype'.format(input1_name) 'dtype': '{}.dtype'.format(input1_name)
} }
node.fluid_code.add_layer("fill_constant", node.fluid_code.add_layer(
"fill_constant",
inputs=None, inputs=None,
output=node.layer_name + '_const1', output=node.layer_name + '_const1',
param_attr=attr) param_attr=attr)
attr = {'act': None, 'name': string(node.layer_name + '_mul1')} attr = {'act': None, 'name': string(node.layer_name + '_mul1')}
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
inputs=input1_name + ', ' + "elementwise_mul",
node.layer_name + '_const1', inputs=input1_name + ', ' + node.layer_name + '_const1',
output=node.layer_name + '_mul1', output=node.layer_name + '_mul1',
param_attr=attr) param_attr=attr)
input2_name = self.get_input_name(inputs[1]) input2_name = self.get_input_name(inputs[1])
...@@ -575,21 +528,23 @@ class CaffeOpMapper(OpMapper): ...@@ -575,21 +528,23 @@ class CaffeOpMapper(OpMapper):
'value': coeff[1], 'value': coeff[1],
'dtype': '{}.dtype'.format(input2_name) 'dtype': '{}.dtype'.format(input2_name)
} }
node.fluid_code.add_layer("fill_constant", node.fluid_code.add_layer(
"fill_constant",
inputs=None, inputs=None,
output=node.layer_name + '_const2', output=node.layer_name + '_const2',
param_attr=attr) param_attr=attr)
attr = {'act': None, 'name': string(node.layer_name + '_mul2')} attr = {'act': None, 'name': string(node.layer_name + '_mul2')}
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
inputs=input2_name + ', ' + "elementwise_mul",
node.layer_name + '_const2', inputs=input2_name + ', ' + node.layer_name + '_const2',
output=node.layer_name + '_mul2', output=node.layer_name + '_mul2',
param_attr=attr) param_attr=attr)
attr = {'act': None, 'name': string(node.layer_name)} attr = {'act': None, 'name': string(node.layer_name)}
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
inputs='{}_mul1, {}_mul2'.format( "elementwise_add",
node.layer_name, node.layer_name), inputs='{}_mul1, {}_mul2'.format(node.layer_name,
node.layer_name),
output=node, output=node,
param_attr=attr) param_attr=attr)
else: else:
...@@ -597,7 +552,8 @@ class CaffeOpMapper(OpMapper): ...@@ -597,7 +552,8 @@ class CaffeOpMapper(OpMapper):
inputs_dict['x'] = inputs[0] inputs_dict['x'] = inputs[0]
inputs_dict['y'] = inputs[1] inputs_dict['y'] = inputs[1]
attr = {'act': None, 'name': string(node.layer_name)} attr = {'act': None, 'name': string(node.layer_name)}
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
"elementwise_add",
inputs=inputs_dict, inputs=inputs_dict,
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -606,7 +562,8 @@ class CaffeOpMapper(OpMapper): ...@@ -606,7 +562,8 @@ class CaffeOpMapper(OpMapper):
inputs_dict['x'] = inputs[0] inputs_dict['x'] = inputs[0]
inputs_dict['y'] = inputs[1] inputs_dict['y'] = inputs[1]
attr = {'act': None, 'name': string(node.layer_name)} attr = {'act': None, 'name': string(node.layer_name)}
node.fluid_code.add_layer("elementwise_max", node.fluid_code.add_layer(
"elementwise_max",
inputs=inputs_dict, inputs=inputs_dict,
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -625,12 +582,8 @@ class CaffeOpMapper(OpMapper): ...@@ -625,12 +582,8 @@ class CaffeOpMapper(OpMapper):
'The parameter of {} (type is {}) is not set. So we set the parameters as 0' 'The parameter of {} (type is {}) is not set. So we set the parameters as 0'
.format(node.layer_name, node.layer_type)) .format(node.layer_name, node.layer_type))
input_c = node.input_shape[0][1] input_c = node.input_shape[0][1]
mean = np.zeros([ mean = np.zeros([input_c, ]).astype('float32')
input_c, variance = np.zeros([input_c, ]).astype('float32')
]).astype('float32')
variance = np.zeros([
input_c,
]).astype('float32')
scale = 0 scale = 0
else: else:
...@@ -651,10 +604,8 @@ class CaffeOpMapper(OpMapper): ...@@ -651,10 +604,8 @@ class CaffeOpMapper(OpMapper):
'epsilon': eps, 'epsilon': eps,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("batch_norm", node.fluid_code.add_layer(
inputs=input, "batch_norm", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Scale(self, node): def Scale(self, node):
if node.data is None: if node.data is None:
...@@ -669,10 +620,10 @@ class CaffeOpMapper(OpMapper): ...@@ -669,10 +620,10 @@ class CaffeOpMapper(OpMapper):
input_c, input_c,
]).astype('float32') ]).astype('float32')
else: else:
self.weights[node.layer_name + '_scale'] = np.squeeze( self.weights[node.layer_name + '_scale'] = np.squeeze(node.data[
node.data[0]).astype('float32') 0]).astype('float32')
self.weights[node.layer_name + '_offset'] = np.squeeze( self.weights[node.layer_name + '_offset'] = np.squeeze(node.data[
node.data[1]).astype('float32') 1]).astype('float32')
params = node.layer.scale_param params = node.layer.scale_param
axis = params.axis axis = params.axis
num_axes = params.num_axes num_axes = params.num_axes
...@@ -687,7 +638,8 @@ class CaffeOpMapper(OpMapper): ...@@ -687,7 +638,8 @@ class CaffeOpMapper(OpMapper):
inputs_dict['x'] = input0 inputs_dict['x'] = input0
inputs_dict['y'] = input1 inputs_dict['y'] = input1
attr = {'axis': axis, 'name': string(node.layer_name + '_mul')} attr = {'axis': axis, 'name': string(node.layer_name + '_mul')}
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
"elementwise_mul",
inputs=inputs_dict, inputs=inputs_dict,
output=node.layer_name + '_mul', output=node.layer_name + '_mul',
param_attr=attr) param_attr=attr)
...@@ -703,15 +655,14 @@ class CaffeOpMapper(OpMapper): ...@@ -703,15 +655,14 @@ class CaffeOpMapper(OpMapper):
'is_bias': True, 'is_bias': True,
'default_initializer': 'Constant(value=1.0)' 'default_initializer': 'Constant(value=1.0)'
} }
node.fluid_code.add_layer("create_parameter", node.fluid_code.add_layer(
inputs=None, "create_parameter", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
inputs_dict = {} inputs_dict = {}
inputs_dict['x'] = input0 inputs_dict['x'] = input0
inputs_dict['y'] = node inputs_dict['y'] = node
attr = {'axis': axis, 'name': string(node.layer_name + '_mul')} attr = {'axis': axis, 'name': string(node.layer_name + '_mul')}
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
"elementwise_mul",
inputs=inputs_dict, inputs=inputs_dict,
output=node.layer_name + '_mul', output=node.layer_name + '_mul',
param_attr=attr) param_attr=attr)
...@@ -725,14 +676,16 @@ class CaffeOpMapper(OpMapper): ...@@ -725,14 +676,16 @@ class CaffeOpMapper(OpMapper):
'is_bias': True, 'is_bias': True,
'default_initializer': 'Constant(value=1.0)' 'default_initializer': 'Constant(value=1.0)'
} }
node.fluid_code.add_layer("create_parameter", node.fluid_code.add_layer(
"create_parameter",
inputs=None, inputs=None,
output=node.layer_name + '_offset_param', output=node.layer_name + '_offset_param',
param_attr=attr) param_attr=attr)
attr = {'axis': axis, 'name': string(node.layer_name + '_add')} attr = {'axis': axis, 'name': string(node.layer_name + '_add')}
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
inputs='{}_mul, {}_offset_param'.format( "elementwise_add",
node.layer_name, node.layer_name), inputs='{}_mul, {}_offset_param'.format(node.layer_name,
node.layer_name),
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -747,10 +700,8 @@ class CaffeOpMapper(OpMapper): ...@@ -747,10 +700,8 @@ class CaffeOpMapper(OpMapper):
'act': None, 'act': None,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("reshape", node.fluid_code.add_layer(
inputs=input, "reshape", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def ArgMax(self, node): def ArgMax(self, node):
assert len(node.inputs) == 1 and len( assert len(node.inputs) == 1 and len(
...@@ -767,10 +718,11 @@ class CaffeOpMapper(OpMapper): ...@@ -767,10 +718,11 @@ class CaffeOpMapper(OpMapper):
axis += len(input_shape) axis += len(input_shape)
if out_max_val is True: if out_max_val is True:
attr = {'k': top_k, 'name': string(node.layer_name + '_topk')} attr = {'k': top_k, 'name': string(node.layer_name + '_topk')}
node.fluid_code.add_layer("topk", node.fluid_code.add_layer(
"topk",
inputs=input, inputs=input,
output='{}_topk_var, {}_index_var'.format( output='{}_topk_var, {}_index_var'.format(node.layer_name,
node.layer_name, node.layer_name), node.layer_name),
param_attr=attr) param_attr=attr)
attr = {'dtype': '{}_topk_var.dtype'.format(node.layer_name)} attr = {'dtype': '{}_topk_var.dtype'.format(node.layer_name)}
node.fluid_code.add_layer( node.fluid_code.add_layer(
...@@ -779,14 +731,16 @@ class CaffeOpMapper(OpMapper): ...@@ -779,14 +731,16 @@ class CaffeOpMapper(OpMapper):
output='{}_index_var'.format(node.layer_name), output='{}_index_var'.format(node.layer_name),
param_attr=attr) param_attr=attr)
attr = {'axis': axis, 'name': string(node.layer_name)} attr = {'axis': axis, 'name': string(node.layer_name)}
node.fluid_code.add_layer("concat", node.fluid_code.add_layer(
inputs='{}_topk_var, {}_index_var'.format( "concat",
node.layer_name, node.layer_name), inputs='{}_topk_var, {}_index_var'.format(node.layer_name,
node.layer_name),
output=node, output=node,
param_attr=attr) param_attr=attr)
else: else:
attr = {'k': top_k, 'name': string(node.layer_name)} attr = {'k': top_k, 'name': string(node.layer_name)}
node.fluid_code.add_layer("topk", node.fluid_code.add_layer(
"topk",
inputs=input, inputs=input,
output='_, {}'.format(node.layer_name), output='_, {}'.format(node.layer_name),
param_attr=attr) param_attr=attr)
...@@ -804,29 +758,27 @@ class CaffeOpMapper(OpMapper): ...@@ -804,29 +758,27 @@ class CaffeOpMapper(OpMapper):
offset_real = [0] * len(input_shape) offset_real = [0] * len(input_shape)
if hasattr(params, "offset") and len(params.offset) > 0: if hasattr(params, "offset") and len(params.offset) > 0:
offset = list(params.offset) offset = list(params.offset)
assert (len(input_shape) - axis) == len( assert (len(input_shape) - axis
offset), "invalid offset[%s] in crop layer" % (str(offset)) ) == len(offset), "invalid offset[%s] in crop layer" % (
str(offset))
offset_real = [0] * axis + offset offset_real = [0] * axis + offset
attr = {'offsets': list(offset_real), 'name': string(node.layer_name)} attr = {'offsets': list(offset_real), 'name': string(node.layer_name)}
node.fluid_code.add_layer("crop", node.fluid_code.add_layer(
inputs={ "crop",
'x': input, inputs={'x': input,
'shape': node.input_shape[1] 'shape': node.input_shape[1]},
},
output=node, output=node,
param_attr=attr) param_attr=attr)
def Flatten(self, node): def Flatten(self, node):
assert len( assert len(
node.inputs node.
) == 1, 'The count of DetectionOutput node\'s input is not 1.' inputs) == 1, 'The count of DetectionOutput node\'s input is not 1.'
input = self.graph.get_bottom_node(node, idx=0, copy=True) input = self.graph.get_bottom_node(node, idx=0, copy=True)
shape = node.output_shape[0] shape = node.output_shape[0]
attr = {'shape': shape, 'name': string(node.layer_name)} attr = {'shape': shape, 'name': string(node.layer_name)}
node.fluid_code.add_layer("reshape", node.fluid_code.add_layer(
inputs=input, "reshape", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Power(self, node): def Power(self, node):
assert len( assert len(
...@@ -842,15 +794,11 @@ class CaffeOpMapper(OpMapper): ...@@ -842,15 +794,11 @@ class CaffeOpMapper(OpMapper):
'bias_after_scale': True, 'bias_after_scale': True,
'name': string(node.layer_name + '_scale') 'name': string(node.layer_name + '_scale')
} }
node.fluid_code.add_layer("scale", node.fluid_code.add_layer(
inputs=input, "scale", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
attr = {'factor': power, 'name': string(node.layer_name)} attr = {'factor': power, 'name': string(node.layer_name)}
node.fluid_code.add_layer("pow", node.fluid_code.add_layer(
inputs=node, "pow", inputs=node, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Reduction(self, node): def Reduction(self, node):
assert len( assert len(
...@@ -872,55 +820,41 @@ class CaffeOpMapper(OpMapper): ...@@ -872,55 +820,41 @@ class CaffeOpMapper(OpMapper):
'keep_dim': False, 'keep_dim': False,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("reduce_sum", node.fluid_code.add_layer(
inputs=input, "reduce_sum", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
elif operation == 2: ## operation = ASUM elif operation == 2: ## operation = ASUM
attr = {'name': string(node.layer_name + '_abs')} attr = {'name': string(node.layer_name + '_abs')}
node.fluid_code.add_layer("abs", node.fluid_code.add_layer(
inputs=input, "abs", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
attr = { attr = {
'dim': dim[axis:], 'dim': dim[axis:],
'keep_dim': False, 'keep_dim': False,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("reduce_sum", node.fluid_code.add_layer(
inputs=node, "reduce_sum", inputs=node, output=node, param_attr=attr)
output=node,
param_attr=attr)
elif operation == 3: ## operation = SUMSQ elif operation == 3: ## operation = SUMSQ
attr = {'factor': 2.0, 'name': string(node.layer_name + '_pow')} attr = {'factor': 2.0, 'name': string(node.layer_name + '_pow')}
node.fluid_code.add_layer("pow", node.fluid_code.add_layer(
inputs=input, "pow", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
attr = { attr = {
'dim': dim[axis:], 'dim': dim[axis:],
'keep_dim': False, 'keep_dim': False,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("reduce_sum", node.fluid_code.add_layer(
inputs=node, "reduce_sum", inputs=node, output=node, param_attr=attr)
output=node,
param_attr=attr)
else: ## operation = MEAN else: ## operation = MEAN
attr = { attr = {
'dim': dim[axis:], 'dim': dim[axis:],
'keep_dim': False, 'keep_dim': False,
'name': string(node.layer_name) 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("reduce_mean", node.fluid_code.add_layer(
inputs=node, "reduce_mean", inputs=node, output=node, param_attr=attr)
output=node,
param_attr=attr)
attr = {'scale': coeff} attr = {'scale': coeff}
node.fluid_code.add_layer("scale", node.fluid_code.add_layer(
inputs=node, "scale", inputs=node, output=node, param_attr=attr)
output=node,
param_attr=attr)
def deal_custom_layer(self, node): def deal_custom_layer(self, node):
op = node.layer_type op = node.layer_type
...@@ -947,7 +881,8 @@ class CaffeOpMapper(OpMapper): ...@@ -947,7 +881,8 @@ class CaffeOpMapper(OpMapper):
assert input is not None, 'This kind of DetectionOutput is not supported!' assert input is not None, 'This kind of DetectionOutput is not supported!'
input = self.graph.get_bottom_node(input, idx=0, copy=True) input = self.graph.get_bottom_node(input, idx=0, copy=True)
inputs_node.append(input) inputs_node.append(input)
node.fluid_code.add_layer(func.__code__.co_name, node.fluid_code.add_layer(
func.__code__.co_name,
inputs=inputs_node, inputs=inputs_node,
output=node, output=node,
param_attr=kwargs, param_attr=kwargs,
...@@ -960,7 +895,5 @@ class CaffeOpMapper(OpMapper): ...@@ -960,7 +895,5 @@ class CaffeOpMapper(OpMapper):
op_info = self.directly_map_ops[node.layer_type] op_info = self.directly_map_ops[node.layer_type]
input = self.graph.get_bottom_node(node, idx=0, copy=True) input = self.graph.get_bottom_node(node, idx=0, copy=True)
attr = {'name': string(node.layer_name)} attr = {'name': string(node.layer_name)}
node.fluid_code.add_layer(op_info, node.fluid_code.add_layer(
inputs=input, op_info, inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
...@@ -33,8 +33,8 @@ def get_kernel_parameters(params): ...@@ -33,8 +33,8 @@ def get_kernel_parameters(params):
[s_h, s_w] = [params.stride] * 2 [s_h, s_w] = [params.stride] * 2
elif len(params.stride) > 0: elif len(params.stride) > 0:
s_h = params.stride_h if params.stride_h > 0 else params.stride[0] s_h = params.stride_h if params.stride_h > 0 else params.stride[0]
s_w = params.stride_w if params.stride_w > 0 else params.stride[ s_w = params.stride_w if params.stride_w > 0 else params.stride[len(
len(params.stride) - 1] params.stride) - 1]
elif params.stride_h > 0 or params.stride_w > 0: elif params.stride_h > 0 or params.stride_w > 0:
s_h = params.stride_h s_h = params.stride_h
s_w = params.stride_w s_w = params.stride_w
......
...@@ -24,21 +24,18 @@ def InstanceNormalization_layer(inputs, name=None): ...@@ -24,21 +24,18 @@ def InstanceNormalization_layer(inputs, name=None):
epsilon = 1e-5 epsilon = 1e-5
input_ = inputs[0] input_ = inputs[0]
mean = fluid.layers.reduce_mean(input_, dim=[2, 3], keep_dim=True) mean = fluid.layers.reduce_mean(input_, dim=[2, 3], keep_dim=True)
var = fluid.layers.reduce_mean(fluid.layers.square(input_ - mean), var = fluid.layers.reduce_mean(
dim=[2, 3], fluid.layers.square(input_ - mean), dim=[2, 3], keep_dim=True)
keep_dim=True)
if name is not None: if name is not None:
scale_name = name + "_scale" scale_name = name + "_scale"
offset_name = name + "_offset" offset_name = name + "_offset"
scale_param = inputs[1] scale_param = inputs[1]
offset_param = inputs[2] offset_param = inputs[2]
scale = fluid.layers.create_parameter(name=scale_param.name, scale = fluid.layers.create_parameter(
shape=input_.shape[1:2], name=scale_param.name, shape=input_.shape[1:2], dtype="float32")
dtype="float32") offset = fluid.layers.create_parameter(
offset = fluid.layers.create_parameter(name=offset_param.name, name=offset_param.name, shape=input_.shape[1:2], dtype="float32")
shape=input_.shape[1:2],
dtype="float32")
tmp = fluid.layers.elementwise_mul(x=(input_ - mean), y=scale, axis=1) tmp = fluid.layers.elementwise_mul(x=(input_ - mean), y=scale, axis=1)
tmp = tmp / fluid.layers.sqrt(var + epsilon) tmp = tmp / fluid.layers.sqrt(var + epsilon)
...@@ -51,7 +48,8 @@ def InstanceNormalization_weights(name, data=None): ...@@ -51,7 +48,8 @@ def InstanceNormalization_weights(name, data=None):
return weights_name return weights_name
register(kind='InstanceNormalization', register(
kind='InstanceNormalization',
shape=InstanceNormalization_shape, shape=InstanceNormalization_shape,
layer=InstanceNormalization_layer, layer=InstanceNormalization_layer,
child_func=None, child_func=None,
......
...@@ -36,8 +36,7 @@ def register(kind, shape, layer, child_func, weights): ...@@ -36,8 +36,7 @@ def register(kind, shape, layer, child_func, weights):
kind = [kind] kind = [kind]
else: else:
assert type( assert type(
kind kind) is list, 'invalid param "kind" for register, not a list or str'
) is list, 'invalid param "kind" for register, not a list or str'
for k in kind: for k in kind:
assert type( assert type(
......
...@@ -28,60 +28,49 @@ default_op_mapping_field_values['FILL_NAME_FIELD'] = True ...@@ -28,60 +28,49 @@ default_op_mapping_field_values['FILL_NAME_FIELD'] = True
default_op_mapping = { default_op_mapping = {
'Shape': ['shape', ['X'], ['Out']], 'Shape': ['shape', ['X'], ['Out']],
'Clip': [ 'Clip': [
'clip', ['X'], ['Out'], 'clip', ['X'], ['Out'], dict(), dict(
dict(), min=(_np.asarray(
dict( [255, 255, 127, 255], dtype=_np.uint8).view(_np.float32)[0]),
min=(_np.asarray([255, 255, 127, 255], max=(_np.asarray(
dtype=_np.uint8).view(_np.float32)[0]), [255, 255, 127, 127], dtype=_np.uint8).view(_np.float32)[0]), )
max=(_np.asarray([255, 255, 127, 127],
dtype=_np.uint8).view(_np.float32)[0]),
)
], ],
'Erf': ['erf', ['X'], ['Out']], 'Erf': ['erf', ['X'], ['Out']],
'Ceil': ['ceil', ['X'], ['Out']], 'Ceil': ['ceil', ['X'], ['Out']],
'ReduceMean': [ 'ReduceMean': [
'reduce_mean', ['X'], ['Out'], 'reduce_mean', ['X'], ['Out'], dict(
dict(axes='dim', keepdims='keep_dim'), axes='dim', keepdims='keep_dim'), dict(keep_dim=1)
dict(keep_dim=1)
], ],
'ReduceSum': [ 'ReduceSum': [
'reduce_sum', ['X'], ['Out'], 'reduce_sum', ['X'], ['Out'], dict(
dict(axes='dim', keepdims='keep_dim'), axes='dim', keepdims='keep_dim'), dict(keep_dim=1)
dict(keep_dim=1)
], ],
'ReduceMin': [ 'ReduceMin': [
'reduce_min', ['X'], ['Out'], 'reduce_min', ['X'], ['Out'], dict(
dict(axes='dim', keepdims='keep_dim'), axes='dim', keepdims='keep_dim'), dict(keep_dim=1)
dict(keep_dim=1)
], ],
'ReduceMax': [ 'ReduceMax': [
'reduce_max', ['X'], ['Out'], 'reduce_max', ['X'], ['Out'], dict(
dict(axes='dim', keepdims='keep_dim'), axes='dim', keepdims='keep_dim'), dict(keep_dim=1)
dict(keep_dim=1)
], ],
#active function #active function
'Relu': ['relu', ['X'], ['Out']], 'Relu': ['relu', ['X'], ['Out']],
'LeakyRelu': ['leaky_relu', ['X'], ['Out'], 'LeakyRelu': ['leaky_relu', ['X'], ['Out'], dict(), dict(alpha=.01)],
dict(), dict(alpha=.01)], 'Elu': ['elu', ['X'], ['Out'], dict(), dict(alpha=1.)],
'Elu': ['elu', ['X'], ['Out'],
dict(), dict(alpha=1.)],
'ThresholdedRelu': [ 'ThresholdedRelu': [
'thresholded_relu', ['X'], ['Out'], 'thresholded_relu', ['X'], ['Out'], dict(alpha='threshold'),
dict(alpha='threshold'),
dict(alpha=1.) dict(alpha=1.)
], ],
'Tanh': ['tanh', ['X'], ['Out']], 'Tanh': ['tanh', ['X'], ['Out']],
'Sigmoid': ['sigmoid', ['X'], ['Out']], 'Sigmoid': ['sigmoid', ['X'], ['Out']],
'HardSigmoid': [ 'HardSigmoid': [
'hard_sigmoid', ['X'], ['Out'], 'hard_sigmoid', ['X'], ['Out'], dict(
dict(alpha='slope', beta='offset'), alpha='slope', beta='offset'), dict(
dict(slope=.2, offset=.5) slope=.2, offset=.5)
], ],
'Softsign': ['softsign', ['X'], ['Out']], 'Softsign': ['softsign', ['X'], ['Out']],
'Softplus': ['softplus', ['X'], ['Out']], 'Softplus': ['softplus', ['X'], ['Out']],
'Exp': ['exp', ['X'], ['Out']], 'Exp': ['exp', ['X'], ['Out']],
'Softmax': ['softmax', ['X'], ['Out'], 'Softmax': ['softmax', ['X'], ['Out'], dict(), dict(axis=1)],
dict(), dict(axis=1)],
'Sqrt': ['sqrt', ['X'], ['Out']], 'Sqrt': ['sqrt', ['X'], ['Out']],
'Floor': ['floor', ['X'], ['Out']], 'Floor': ['floor', ['X'], ['Out']],
'Abs': ['abs', ['X'], ['Out']], 'Abs': ['abs', ['X'], ['Out']],
......
...@@ -140,8 +140,8 @@ class ONNXOpMapper(OpMapper): ...@@ -140,8 +140,8 @@ class ONNXOpMapper(OpMapper):
model.graph.ClearField('output') model.graph.ClearField('output')
model.graph.output.MergeFrom(model.graph.value_info) model.graph.output.MergeFrom(model.graph.value_info)
onnx.save(model, os.path.join(self.tmp_data_dir, onnx.save(model,
'onnx_model_infer.onnx')) os.path.join(self.tmp_data_dir, 'onnx_model_infer.onnx'))
sess = rt.InferenceSession( sess = rt.InferenceSession(
os.path.join(self.tmp_data_dir, 'onnx_model_infer.onnx')) os.path.join(self.tmp_data_dir, 'onnx_model_infer.onnx'))
res = sess.run(None, input_feed=inputs_dict) res = sess.run(None, input_feed=inputs_dict)
...@@ -217,8 +217,7 @@ class ONNXOpMapper(OpMapper): ...@@ -217,8 +217,7 @@ class ONNXOpMapper(OpMapper):
default_attrs, default_attrs,
input_perm, input_perm,
output_perm, output_perm,
fill_name_field, fill_name_field, ) = info
) = info
if fluid_op in default_ioa_constraint: if fluid_op in default_ioa_constraint:
for predicate, message in default_ioa_constraint[fluid_op]: for predicate, message in default_ioa_constraint[fluid_op]:
...@@ -429,10 +428,8 @@ class ONNXOpMapper(OpMapper): ...@@ -429,10 +428,8 @@ class ONNXOpMapper(OpMapper):
} }
node.fluid_code.add_layer( node.fluid_code.add_layer(
'roi_align', 'roi_align',
inputs={ inputs={'input': val_x,
'input': val_x, 'rois': val_rois},
'rois': val_rois
},
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -449,10 +446,8 @@ class ONNXOpMapper(OpMapper): ...@@ -449,10 +446,8 @@ class ONNXOpMapper(OpMapper):
} }
node.fluid_code.add_layer( node.fluid_code.add_layer(
'roi_pool', 'roi_pool',
inputs={ inputs={'input': val_x,
'input': val_x, 'rois': val_rois},
'rois': val_rois
},
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -527,10 +522,8 @@ class ONNXOpMapper(OpMapper): ...@@ -527,10 +522,8 @@ class ONNXOpMapper(OpMapper):
val_y = self.graph.get_input_node(node, idx=1, copy=True) val_y = self.graph.get_input_node(node, idx=1, copy=True)
node.fluid_code.add_layer( node.fluid_code.add_layer(
'greater_than', 'greater_than',
inputs={ inputs={'x': val_x,
'x': val_x, 'y': val_y},
'y': val_y
},
output=node, output=node,
param_attr=None) param_attr=None)
...@@ -549,8 +542,7 @@ class ONNXOpMapper(OpMapper): ...@@ -549,8 +542,7 @@ class ONNXOpMapper(OpMapper):
shape = val_output.out_shapes[0] shape = val_output.out_shapes[0]
if shape is None: if shape is None:
shape = list(value.shape) shape = list(value.shape)
_logger.warning( _logger.warning('in (Constant -> %s): '
'in (Constant -> %s): '
'attribute "shape" of %s not inferred, ' 'attribute "shape" of %s not inferred, '
'using value as 1-D tensor may lead to fails', 'using value as 1-D tensor may lead to fails',
val_output.layer_name, val_output.layer_name) val_output.layer_name, val_output.layer_name)
...@@ -616,10 +608,8 @@ class ONNXOpMapper(OpMapper): ...@@ -616,10 +608,8 @@ class ONNXOpMapper(OpMapper):
if axis == 0 and len(indices_shape) <= 1: if axis == 0 and len(indices_shape) <= 1:
node.fluid_code.add_layer( node.fluid_code.add_layer(
'gather', 'gather',
inputs={ inputs={'input': val_x,
'input': val_x, 'index': indices},
'index': indices
},
output=node, output=node,
param_attr=None) param_attr=None)
elif axis > 0 and len(indices_shape) <= 1: elif axis > 0 and len(indices_shape) <= 1:
...@@ -634,10 +624,8 @@ class ONNXOpMapper(OpMapper): ...@@ -634,10 +624,8 @@ class ONNXOpMapper(OpMapper):
param_attr=attr_trans) param_attr=attr_trans)
node.fluid_code.add_layer( node.fluid_code.add_layer(
'gather', 'gather',
inputs={ inputs={'input': name_trans,
'input': name_trans, 'index': indices},
'index': indices
},
output=node, output=node,
param_attr=None) param_attr=None)
node.fluid_code.add_layer( node.fluid_code.add_layer(
...@@ -649,9 +637,7 @@ class ONNXOpMapper(OpMapper): ...@@ -649,9 +637,7 @@ class ONNXOpMapper(OpMapper):
'reshape', 'reshape',
inputs=indices, inputs=indices,
output=indices, output=indices,
param_attr={'shape': [ param_attr={'shape': [reshape_shape, ]})
reshape_shape,
]})
perm = list(range(len(val_x.out_shapes[0]))) perm = list(range(len(val_x.out_shapes[0])))
perm = [axis] + perm[:axis] + perm[axis + 1:] perm = [axis] + perm[:axis] + perm[axis + 1:]
...@@ -664,10 +650,8 @@ class ONNXOpMapper(OpMapper): ...@@ -664,10 +650,8 @@ class ONNXOpMapper(OpMapper):
param_attr=attr_trans) param_attr=attr_trans)
node.fluid_code.add_layer( node.fluid_code.add_layer(
'gather', 'gather',
inputs={ inputs={'input': name_trans,
'input': name_trans, 'index': indices},
'index': indices
},
output=node, output=node,
param_attr=None) param_attr=None)
node.fluid_code.add_layer( node.fluid_code.add_layer(
...@@ -926,8 +910,10 @@ class ONNXOpMapper(OpMapper): ...@@ -926,8 +910,10 @@ class ONNXOpMapper(OpMapper):
def Sum(self, node): def Sum(self, node):
val_inps = node.layer.input val_inps = node.layer.input
inputs = { inputs = {
"x": self.graph.get_input_node(node, idx=0, copy=True), "x": self.graph.get_input_node(
"y": self.graph.get_input_node(node, idx=1, copy=True), node, idx=0, copy=True),
"y": self.graph.get_input_node(
node, idx=1, copy=True),
} }
node.fluid_code.add_layer("elementwise_add", inputs=inputs, output=node) node.fluid_code.add_layer("elementwise_add", inputs=inputs, output=node)
...@@ -1022,10 +1008,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1022,10 +1008,8 @@ class ONNXOpMapper(OpMapper):
val_y = self.graph.get_input_node(node, idx=1, copy=True) val_y = self.graph.get_input_node(node, idx=1, copy=True)
node.fluid_code.add_layer( node.fluid_code.add_layer(
"equal", "equal",
inputs={ inputs={'x': val_x,
'x': val_x, 'y': val_y},
'y': val_y
},
output=node, output=node,
param_attr=None) param_attr=None)
...@@ -1055,29 +1039,23 @@ class ONNXOpMapper(OpMapper): ...@@ -1055,29 +1039,23 @@ class ONNXOpMapper(OpMapper):
mul_val_x = val_x.layer_name + '_mul' mul_val_x = val_x.layer_name + '_mul'
node.fluid_code.add_layer( node.fluid_code.add_layer(
"elementwise_mul", "elementwise_mul",
inputs={ inputs={'x': val_x,
'x': val_x, 'y': cast_condition},
'y': cast_condition
},
output=mul_val_x, output=mul_val_x,
param_attr=None) param_attr=None)
mul_val_y = val_y.layer_name + '_mul' mul_val_y = val_y.layer_name + '_mul'
node.fluid_code.add_layer( node.fluid_code.add_layer(
"elementwise_mul", "elementwise_mul",
inputs={ inputs={'x': val_y,
'x': val_y, 'y': cast_not_condition},
'y': cast_not_condition
},
output=mul_val_y, output=mul_val_y,
param_attr=None) param_attr=None)
node.fluid_code.add_layer( node.fluid_code.add_layer(
"elementwise_add", "elementwise_add",
inputs={ inputs={'x': mul_val_x,
'x': mul_val_x, 'y': mul_val_y},
'y': mul_val_y
},
output=node, output=node,
param_attr=None) param_attr=None)
...@@ -1106,7 +1084,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1106,7 +1084,8 @@ class ONNXOpMapper(OpMapper):
output=flatten_name, output=flatten_name,
param_attr={'axis': 0}) param_attr={'axis': 0})
node.fluid_code.add_layer( node.fluid_code.add_layer(
"concat", inputs=flatten_names, output=node, param_attr={'axis': 0}) "concat", inputs=flatten_names, output=node,
param_attr={'axis': 0})
def Identity(self, node): def Identity(self, node):
val_x = self.graph.get_input_node(node, idx=0, copy=True) val_x = self.graph.get_input_node(node, idx=0, copy=True)
...@@ -1280,11 +1259,11 @@ class ONNXOpMapper(OpMapper): ...@@ -1280,11 +1259,11 @@ class ONNXOpMapper(OpMapper):
output_size = [0, 0] output_size = [0, 0]
output_size[0] = (val_x.out_shapes[0][2] - output_size[0] = (val_x.out_shapes[0][2] - 1
1) * strides[0] - 2 * paddings[0] + dilations[0] * ( ) * strides[0] - 2 * paddings[0] + dilations[0] * (
kernel_shape[0] - 1) + 1 + out_padding[0] kernel_shape[0] - 1) + 1 + out_padding[0]
output_size[1] = (val_x.out_shapes[0][3] - output_size[1] = (val_x.out_shapes[0][3] - 1
1) * strides[1] - 2 * paddings[1] + dilations[1] * ( ) * strides[1] - 2 * paddings[1] + dilations[1] * (
kernel_shape[1] - 1) + 1 + out_padding[1] kernel_shape[1] - 1) + 1 + out_padding[1]
attr = { attr = {
'num_filters': num_out_channels, 'num_filters': num_out_channels,
...@@ -1367,29 +1346,23 @@ class ONNXOpMapper(OpMapper): ...@@ -1367,29 +1346,23 @@ class ONNXOpMapper(OpMapper):
'squeeze', 'squeeze',
inputs=val_x, inputs=val_x,
output=var_x0, output=var_x0,
param_attr={ param_attr={'axes': [1],
'axes': [1], 'name': string(var_x0)})
'name': string(var_x0)
})
var_w0 = node.layer_name + '_w0' var_w0 = node.layer_name + '_w0'
node.fluid_code.add_layer( node.fluid_code.add_layer(
'squeeze', 'squeeze',
inputs=val_w, inputs=val_w,
output=var_w0, output=var_w0,
param_attr={ param_attr={'axes': [0],
'axes': [0], 'name': string(var_w0)})
'name': string(var_w0)
})
var_fc = node.layer_name + '_fc' var_fc = node.layer_name + '_fc'
var_mm = (node.layer_name + '_mm') if val_b else var_fc var_mm = (node.layer_name + '_mm') if val_b else var_fc
node.fluid_code.add_layer( node.fluid_code.add_layer(
'matmul', 'matmul',
inputs={ inputs={'x': var_x0,
'x': var_x0, 'y': var_w0},
'y': var_w0
},
output=var_mm, output=var_mm,
param_attr={ param_attr={
'transpose_x': 0, 'transpose_x': 0,
...@@ -1402,10 +1375,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1402,10 +1375,8 @@ class ONNXOpMapper(OpMapper):
'squeeze', 'squeeze',
inputs=val_r, inputs=val_r,
output=var_r0, output=var_r0,
param_attr={ param_attr={'axes': [0],
'axes': [0], 'name': string(var_r0)})
'name': string(var_r0)
})
var_r0t = node.layer_name + '_r0t' var_r0t = node.layer_name + '_r0t'
...@@ -1413,10 +1384,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1413,10 +1384,8 @@ class ONNXOpMapper(OpMapper):
'transpose', 'transpose',
inputs=var_r0, inputs=var_r0,
output=var_r0t, output=var_r0t,
param_attr={ param_attr={'perm': [1, 0],
'perm': [1, 0], 'name': string(var_r0t)})
'name': string(var_r0t)
})
if val_b: if val_b:
var_bi = node.layer_name + '_bi' var_bi = node.layer_name + '_bi'
var_bh = node.layer_name + '_bh' var_bh = node.layer_name + '_bh'
...@@ -1434,10 +1403,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1434,10 +1403,8 @@ class ONNXOpMapper(OpMapper):
'squeeze', 'squeeze',
inputs=var_bi, inputs=var_bi,
output=var_bi0, output=var_bi0,
param_attr={ param_attr={'axes': [0],
'axes': [0], 'name': string(var_bi0)})
'name': string(var_bi0)
})
node.fluid_code.add_layer( node.fluid_code.add_layer(
'elmentwise_add', 'elmentwise_add',
...@@ -1454,10 +1421,8 @@ class ONNXOpMapper(OpMapper): ...@@ -1454,10 +1421,8 @@ class ONNXOpMapper(OpMapper):
'squeeze', 'squeeze',
inputs=val_xh, inputs=val_xh,
output=var_xh0, output=var_xh0,
param_attr={ param_attr={'axes': [1],
'axes': [1], 'name': string(var_xh0)})
'name': string(var_xh0)
})
var_y00 = node.layer_name + '_y00' var_y00 = node.layer_name + '_y00'
attr = { attr = {
......
...@@ -30,8 +30,8 @@ def im2sequence(op, block): ...@@ -30,8 +30,8 @@ def im2sequence(op, block):
slice_blocks = list() slice_blocks = list()
for i in range(out_h): for i in range(out_h):
for j in range(out_w): for j in range(out_w):
starts_name = "im2sequence.starts.{}.{}.{}".format( starts_name = "im2sequence.starts.{}.{}.{}".format(im2seq_counter,
im2seq_counter, i, j) i, j)
starts_tensor = helper.make_tensor( starts_tensor = helper.make_tensor(
name=starts_name, name=starts_name,
data_type=onnx_pb.TensorProto.INT64, data_type=onnx_pb.TensorProto.INT64,
......
...@@ -44,8 +44,7 @@ def multiclass_nms(op, block): ...@@ -44,8 +44,7 @@ def multiclass_nms(op, block):
if normalized == False: if normalized == False:
warnings.warn( warnings.warn(
'The parameter normalized of multiclass_nms OP of Paddle is False, which has diff with ONNX. \ 'The parameter normalized of multiclass_nms OP of Paddle is False, which has diff with ONNX. \
Please set normalized=True in multiclass_nms of Paddle' Please set normalized=True in multiclass_nms of Paddle')
)
#convert the paddle attribute to onnx tensor #convert the paddle attribute to onnx tensor
name_score_threshold = [outputs['Out'][0] + "@score_threshold"] name_score_threshold = [outputs['Out'][0] + "@score_threshold"]
...@@ -353,7 +352,8 @@ def multiclass_nms(op, block): ...@@ -353,7 +352,8 @@ def multiclass_nms(op, block):
outputs_gather_topk_class = [result_name + "@gather_topk_class"] outputs_gather_topk_class = [result_name + "@gather_topk_class"]
node_gather_topk_class = onnx.helper.make_node( node_gather_topk_class = onnx.helper.make_node(
'Gather', 'Gather',
inputs=outputs_gather_1_nonzero + [outputs_topk_select_topk_indices[1]], inputs=outputs_gather_1_nonzero +
[outputs_topk_select_topk_indices[1]],
outputs=outputs_gather_topk_class, outputs=outputs_gather_topk_class,
axis=1) axis=1)
node_list.append(node_gather_topk_class) node_list.append(node_gather_topk_class)
...@@ -362,7 +362,8 @@ def multiclass_nms(op, block): ...@@ -362,7 +362,8 @@ def multiclass_nms(op, block):
outputs_gather_topk_boxes_id = [result_name + "@gather_topk_boxes_id"] outputs_gather_topk_boxes_id = [result_name + "@gather_topk_boxes_id"]
node_gather_topk_boxes_id = onnx.helper.make_node( node_gather_topk_boxes_id = onnx.helper.make_node(
'Gather', 'Gather',
inputs=outputs_gather_2_nonzero + [outputs_topk_select_topk_indices[1]], inputs=outputs_gather_2_nonzero +
[outputs_topk_select_topk_indices[1]],
outputs=outputs_gather_topk_boxes_id, outputs=outputs_gather_topk_boxes_id,
axis=1) axis=1)
node_list.append(node_gather_topk_boxes_id) node_list.append(node_gather_topk_boxes_id)
......
...@@ -4,8 +4,6 @@ from onnx import onnx_pb, helper ...@@ -4,8 +4,6 @@ from onnx import onnx_pb, helper
def get_old_name(arg, name_prefix=''): def get_old_name(arg, name_prefix=''):
"""Get the old rame for a possible renamed argument
"""
prefix_index = arg.find(name_prefix) prefix_index = arg.find(name_prefix)
if prefix_index != -1: if prefix_index != -1:
...@@ -40,8 +38,8 @@ def yolo_box(op, block): ...@@ -40,8 +38,8 @@ def yolo_box(op, block):
downsample_ratio = attrs['downsample_ratio'] downsample_ratio = attrs['downsample_ratio']
input_size = input_height * downsample_ratio input_size = input_height * downsample_ratio
conf_thresh = attrs['conf_thresh'] conf_thresh = attrs['conf_thresh']
conf_thresh_mat = np.ones([num_anchors * input_height * input_width conf_thresh_mat = np.ones([num_anchors * input_height *
]) * conf_thresh input_width]) * conf_thresh
node_list = [] node_list = []
im_outputs = [] im_outputs = []
......
...@@ -250,8 +250,7 @@ class PaddleOpMapper(object): ...@@ -250,8 +250,7 @@ class PaddleOpMapper(object):
node = helper.make_node( node = helper.make_node(
pool_type[op.attr('pooling_type')][1], pool_type[op.attr('pooling_type')][1],
inputs=op.input('X'), inputs=op.input('X'),
outputs=op.output('Out'), outputs=op.output('Out'), )
)
else: else:
input_shape = block.var(op.input('X')[0]).shape input_shape = block.var(op.input('X')[0]).shape
k_size = op.attr('ksize') k_size = op.attr('ksize')
...@@ -407,8 +406,7 @@ class PaddleOpMapper(object): ...@@ -407,8 +406,7 @@ class PaddleOpMapper(object):
node = helper.make_node( node = helper.make_node(
'Clip', 'Clip',
inputs=[op.input('X')[0], min_name, max_name], inputs=[op.input('X')[0], min_name, max_name],
outputs=op.output('Out'), outputs=op.output('Out'), )
)
return [min_node, max_node, node] return [min_node, max_node, node]
def shape(self, op, block): def shape(self, op, block):
...@@ -450,8 +448,7 @@ class PaddleOpMapper(object): ...@@ -450,8 +448,7 @@ class PaddleOpMapper(object):
node = helper.make_node( node = helper.make_node(
"Slice", "Slice",
inputs=[op.input('Input')[0], starts_name, ends_name, axes_name], inputs=[op.input('Input')[0], starts_name, ends_name, axes_name],
outputs=op.output('Out'), outputs=op.output('Out'), )
)
return [starts_node, ends_node, axes_node, node] return [starts_node, ends_node, axes_node, node]
def fill_constant(self, op, block): def fill_constant(self, op, block):
...@@ -551,8 +548,8 @@ class PaddleOpMapper(object): ...@@ -551,8 +548,8 @@ class PaddleOpMapper(object):
if op.attr('align_corners'): if op.attr('align_corners'):
coordinate_transformation_mode = 'align_corners' coordinate_transformation_mode = 'align_corners'
if ('OutSize' in input_names and len(op.input('OutSize')) > 0) or ( if ('OutSize' in input_names and len(op.input('OutSize')) > 0) or (
'SizeTensor' in input_names 'SizeTensor' in input_names and
and len(op.input('SizeTensor')) > 0): len(op.input('SizeTensor')) > 0):
node_list = list() node_list = list()
roi_node = self.make_constant_node( roi_node = self.make_constant_node(
self.get_name(op.type, 'roi'), onnx_pb.TensorProto.FLOAT, self.get_name(op.type, 'roi'), onnx_pb.TensorProto.FLOAT,
...@@ -631,8 +628,7 @@ class PaddleOpMapper(object): ...@@ -631,8 +628,7 @@ class PaddleOpMapper(object):
elif 'Scale' in input_names and len(op.input('Scale')) > 0: elif 'Scale' in input_names and len(op.input('Scale')) > 0:
node = helper.make_node( node = helper.make_node(
'Resize', 'Resize',
inputs=[op.input('X')[0], inputs=[op.input('X')[0], op.input('Scale')[0]],
op.input('Scale')[0]],
outputs=op.output('Out'), outputs=op.output('Out'),
mode='linear', mode='linear',
coordinate_transformation_mode=coordinate_transformation_mode) coordinate_transformation_mode=coordinate_transformation_mode)
...@@ -641,8 +637,9 @@ class PaddleOpMapper(object): ...@@ -641,8 +637,9 @@ class PaddleOpMapper(object):
scale = op.attr('scale') scale = op.attr('scale')
if out_shape.count(-1) > 0: if out_shape.count(-1) > 0:
scale_name = self.get_name(op.type, 'scale') scale_name = self.get_name(op.type, 'scale')
scale_node = self.make_constant_node( scale_node = self.make_constant_node(scale_name,
scale_name, onnx_pb.TensorProto.FLOAT, [1, 1, scale, scale]) onnx_pb.TensorProto.FLOAT,
[1, 1, scale, scale])
roi_name = self.get_name(op.type, 'roi') roi_name = self.get_name(op.type, 'roi')
roi_node = self.make_constant_node(roi_name, roi_node = self.make_constant_node(roi_name,
onnx_pb.TensorProto.FLOAT, onnx_pb.TensorProto.FLOAT,
...@@ -667,16 +664,14 @@ class PaddleOpMapper(object): ...@@ -667,16 +664,14 @@ class PaddleOpMapper(object):
if 'OutSize' in input_names and len(op.input('OutSize')) > 0: if 'OutSize' in input_names and len(op.input('OutSize')) > 0:
node = helper.make_node( node = helper.make_node(
'Resize', 'Resize',
inputs=[op.input('X')[0], '', inputs=[op.input('X')[0], '', op.input('OutSize')[0]],
op.input('OutSize')[0]],
outputs=op.output('Out'), outputs=op.output('Out'),
mode='nearest', mode='nearest',
coordinate_transformation_mode=coordinate_transformation_mode) coordinate_transformation_mode=coordinate_transformation_mode)
elif 'Scale' in input_names and len(op.input('Scale')) > 0: elif 'Scale' in input_names and len(op.input('Scale')) > 0:
node = helper.make_node( node = helper.make_node(
'Resize', 'Resize',
inputs=[op.input('X')[0], inputs=[op.input('X')[0], op.input('Scale')[0]],
op.input('Scale')[0]],
outputs=op.output('Out'), outputs=op.output('Out'),
mode='nearest', mode='nearest',
coordinate_transformation_mode=coordinate_transformation_mode) coordinate_transformation_mode=coordinate_transformation_mode)
...@@ -685,8 +680,9 @@ class PaddleOpMapper(object): ...@@ -685,8 +680,9 @@ class PaddleOpMapper(object):
scale = op.attr('scale') scale = op.attr('scale')
if out_shape.count(-1) > 0: if out_shape.count(-1) > 0:
scale_name = self.get_name(op.type, 'scale') scale_name = self.get_name(op.type, 'scale')
scale_node = self.make_constant_node( scale_node = self.make_constant_node(scale_name,
scale_name, onnx_pb.TensorProto.FLOAT, [1, 1, scale, scale]) onnx_pb.TensorProto.FLOAT,
[1, 1, scale, scale])
roi_name = self.get_name(op.type, 'roi') roi_name = self.get_name(op.type, 'roi')
roi_node = self.make_constant_node(roi_name, roi_node = self.make_constant_node(roi_name,
onnx_pb.TensorProto.FLOAT, onnx_pb.TensorProto.FLOAT,
...@@ -737,8 +733,7 @@ class PaddleOpMapper(object): ...@@ -737,8 +733,7 @@ class PaddleOpMapper(object):
node1 = helper.make_node( node1 = helper.make_node(
'Clip', 'Clip',
inputs=[name0, min_name, max_name], inputs=[name0, min_name, max_name],
outputs=[name1], outputs=[name1], )
)
name2 = self.get_name(op.type, 'mul') name2 = self.get_name(op.type, 'mul')
node2 = helper.make_node( node2 = helper.make_node(
'Mul', inputs=[op.input('X')[0], name1], outputs=[name2]) 'Mul', inputs=[op.input('X')[0], name1], outputs=[name2])
...@@ -814,14 +809,6 @@ class PaddleOpMapper(object): ...@@ -814,14 +809,6 @@ class PaddleOpMapper(object):
keepdims=0) keepdims=0)
return node return node
def yolo_box(self, op, block):
from .paddle_custom_layer.yolo_box import yolo_box
return yolo_box(op, block)
def multiclass_nms(self, op, block):
from .paddle_custom_layer.multiclass_nms import multiclass_nms
return multiclass_nms(op, block)
def reciprocal(self, op, block): def reciprocal(self, op, block):
inputs = op.input(op.input_names[0]) inputs = op.input(op.input_names[0])
outputs = op.output(op.output_names[0]) outputs = op.output(op.output_names[0])
......
...@@ -114,9 +114,8 @@ class TFOpMapper(OpMapper): ...@@ -114,9 +114,8 @@ class TFOpMapper(OpMapper):
else: else:
unsupported_ops.add(op) unsupported_ops.add(op)
if len(unsupported_ops) > 0: if len(unsupported_ops) > 0:
sys.stderr.write( sys.stderr.write("=========={} Ops are not supported yet======\n".
"=========={} Ops are not supported yet======\n".format( format(len(unsupported_ops)))
len(unsupported_ops)))
for op in unsupported_ops: for op in unsupported_ops:
sys.stderr.write("========== {} ==========\n".format(op)) sys.stderr.write("========== {} ==========\n".format(op))
sys.exit(-1) sys.exit(-1)
...@@ -141,10 +140,8 @@ class TFOpMapper(OpMapper): ...@@ -141,10 +140,8 @@ class TFOpMapper(OpMapper):
pd_param_name = list(param.values())[0] pd_param_name = list(param.values())[0]
tf_param = node.get_attr(tf_param_name) tf_param = node.get_attr(tf_param_name)
attr[pd_param_name] = tf_param attr[pd_param_name] = tf_param
node.fluid_code.add_layer(op_info[0], node.fluid_code.add_layer(
inputs=input, op_info[0], inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def elementwise_map(self, node): def elementwise_map(self, node):
assert node.layer_type in self.elementwise_ops assert node.layer_type in self.elementwise_ops
...@@ -179,21 +176,21 @@ class TFOpMapper(OpMapper): ...@@ -179,21 +176,21 @@ class TFOpMapper(OpMapper):
0] == y_shape[-1] and y_shape.count(-1) < 1: 0] == y_shape[-1] and y_shape.count(-1) < 1:
shape = [1, x_shape[0], 1, 1] shape = [1, x_shape[0], 1, 1]
attr = {"shape": shape} attr = {"shape": shape}
node.fluid_code.add_layer("reshape", node.fluid_code.add_layer(
"reshape",
inputs=x_input, inputs=x_input,
output="reshape_x", output="reshape_x",
param_attr=attr) param_attr=attr)
if y_shape[0] != 1: if y_shape[0] != 1:
attr = {"expand_times": [y_shape[0], 1, 1, 1]} attr = {"expand_times": [y_shape[0], 1, 1, 1]}
node.fluid_code.add_layer("expand", node.fluid_code.add_layer(
"expand",
inputs="reshape_x", inputs="reshape_x",
output="reshape_x", output="reshape_x",
param_attr=attr) param_attr=attr)
inputs = {"x": "reshape_x", "y": y_input} inputs = {"x": "reshape_x", "y": y_input}
node.fluid_code.add_layer(op_type, node.fluid_code.add_layer(
inputs=inputs, op_type, inputs=inputs, output=node, param_attr=None)
output=node,
param_attr=None)
return return
else: else:
raise Exception("Unexpected situation happend") raise Exception("Unexpected situation happend")
...@@ -205,10 +202,8 @@ class TFOpMapper(OpMapper): ...@@ -205,10 +202,8 @@ class TFOpMapper(OpMapper):
axis = -1 axis = -1
attr = {"axis": axis} attr = {"axis": axis}
inputs = {"x": x_input, "y": y_input} inputs = {"x": x_input, "y": y_input}
node.fluid_code.add_layer(op_type, node.fluid_code.add_layer(
inputs=inputs, op_type, inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
return return
is_sub_seq = True is_sub_seq = True
...@@ -242,10 +237,8 @@ class TFOpMapper(OpMapper): ...@@ -242,10 +237,8 @@ class TFOpMapper(OpMapper):
if len(x_expand_times) == 4 and x.tf_data_format == "NHWC": if len(x_expand_times) == 4 and x.tf_data_format == "NHWC":
x_expand_times = [x_expand_times[i] for i in [0, 3, 1, 2]] x_expand_times = [x_expand_times[i] for i in [0, 3, 1, 2]]
attr = {"expand_times": x_expand_times} attr = {"expand_times": x_expand_times}
node.fluid_code.add_layer("expand", node.fluid_code.add_layer(
inputs=x_input, "expand", inputs=x_input, output="x_tmp", param_attr=attr)
output="x_tmp",
param_attr=attr)
x_input = "x_tmp" x_input = "x_tmp"
if y_need_expand: if y_need_expand:
if len(y_expand_times) == 3 and y.tf_data_format == "NHWC": if len(y_expand_times) == 3 and y.tf_data_format == "NHWC":
...@@ -253,16 +246,12 @@ class TFOpMapper(OpMapper): ...@@ -253,16 +246,12 @@ class TFOpMapper(OpMapper):
if len(y_expand_times) == 4 and y.tf_data_format == "NHWC": if len(y_expand_times) == 4 and y.tf_data_format == "NHWC":
y_expand_times = [y_expand_times[i] for i in [0, 3, 1, 2]] y_expand_times = [y_expand_times[i] for i in [0, 3, 1, 2]]
attr = {"expand_times": y_expand_times} attr = {"expand_times": y_expand_times}
node.fluid_code.add_layer("expand", node.fluid_code.add_layer(
inputs=y_input, "expand", inputs=y_input, output="y_tmp", param_attr=attr)
output="y_tmp",
param_attr=attr)
y_input = "y_tmp" y_input = "y_tmp"
inputs = {"x": x_input, "y": y_input} inputs = {"x": x_input, "y": y_input}
node.fluid_code.add_layer(op_type, node.fluid_code.add_layer(
inputs=inputs, op_type, inputs=inputs, output=node, param_attr=None)
output=node,
param_attr=None)
def Placeholder(self, node): def Placeholder(self, node):
shape = node.out_shapes[0] shape = node.out_shapes[0]
...@@ -283,10 +272,8 @@ class TFOpMapper(OpMapper): ...@@ -283,10 +272,8 @@ class TFOpMapper(OpMapper):
if shape[0] < 0: if shape[0] < 0:
self.batch_node = node self.batch_node = node
node.fluid_code.add_layer("data", node.fluid_code.add_layer(
inputs=None, "data", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def OneShotIterator(self, node): def OneShotIterator(self, node):
return self.Placeholder(node) return self.Placeholder(node)
...@@ -308,8 +295,8 @@ class TFOpMapper(OpMapper): ...@@ -308,8 +295,8 @@ class TFOpMapper(OpMapper):
shape = [shape[i] for i in [0, 3, 1, 2]] shape = [shape[i] for i in [0, 3, 1, 2]]
if len(shape) == 3: if len(shape) == 3:
shape = [shape[i] for i in [2, 0, 1]] shape = [shape[i] for i in [2, 0, 1]]
self.weights[node.layer_name] = numpy.transpose( self.weights[node.layer_name] = numpy.transpose(node.value,
node.value, (2, 0, 1)) (2, 0, 1))
elif node.tf_data_format == "NCHW": elif node.tf_data_format == "NCHW":
if len(shape) == 4: if len(shape) == 4:
self.graph.data_format_propagation(node) self.graph.data_format_propagation(node)
...@@ -320,10 +307,8 @@ class TFOpMapper(OpMapper): ...@@ -320,10 +307,8 @@ class TFOpMapper(OpMapper):
'name': string(node.layer_name), 'name': string(node.layer_name),
'default_initializer': initializer 'default_initializer': initializer
} }
node.fluid_code.add_layer("create_parameter", node.fluid_code.add_layer(
inputs=None, "create_parameter", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Transpose(self, node): def Transpose(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -362,16 +347,12 @@ class TFOpMapper(OpMapper): ...@@ -362,16 +347,12 @@ class TFOpMapper(OpMapper):
node.tf_data_format = [tf_data_format[i] for i in perm] node.tf_data_format = [tf_data_format[i] for i in perm]
node.pd_data_format = [pd_data_format[i] for i in perm] node.pd_data_format = [pd_data_format[i] for i in perm]
attr = {'perm': new_perm} attr = {'perm': new_perm}
node.fluid_code.add_layer("transpose", node.fluid_code.add_layer(
inputs=input, "transpose", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
elif len(node.out_shapes[0]) != 4: elif len(node.out_shapes[0]) != 4:
attr = {'perm': perm} attr = {'perm': perm}
node.fluid_code.add_layer("transpose", node.fluid_code.add_layer(
inputs=input, "transpose", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
else: else:
raise Exception("Unexpected situation happend in Transpose OP") raise Exception("Unexpected situation happend in Transpose OP")
...@@ -401,10 +382,8 @@ class TFOpMapper(OpMapper): ...@@ -401,10 +382,8 @@ class TFOpMapper(OpMapper):
"pool_padding": string(pad_mode), "pool_padding": string(pad_mode),
"pool_stride": strides[2:4] "pool_stride": strides[2:4]
} }
node.fluid_code.add_layer("pool2d", node.fluid_code.add_layer(
inputs=input, "pool2d", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Conv2D(self, node): def Conv2D(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -444,10 +423,8 @@ class TFOpMapper(OpMapper): ...@@ -444,10 +423,8 @@ class TFOpMapper(OpMapper):
"dilation": dilations[2:4], "dilation": dilations[2:4],
"padding": string(pad_mode) "padding": string(pad_mode)
} }
node.fluid_code.add_layer("conv2d", node.fluid_code.add_layer(
inputs=input, "conv2d", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def BiasAdd(self, node): def BiasAdd(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -457,10 +434,8 @@ class TFOpMapper(OpMapper): ...@@ -457,10 +434,8 @@ class TFOpMapper(OpMapper):
axis = 1 axis = 1
inputs = {"x": input, "y": bias} inputs = {"x": input, "y": bias}
attr = {"axis": axis} attr = {"axis": axis}
node.fluid_code.add_layer("elementwise_add", node.fluid_code.add_layer(
inputs=inputs, "elementwise_add", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def FusedBatchNorm(self, node): def FusedBatchNorm(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -491,10 +466,8 @@ class TFOpMapper(OpMapper): ...@@ -491,10 +466,8 @@ class TFOpMapper(OpMapper):
"is_test": True "is_test": True
} }
node.fluid_code.add_layer("batch_norm", node.fluid_code.add_layer(
inputs=input, "batch_norm", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def FusedBatchNormV3(self, node): def FusedBatchNormV3(self, node):
return self.FusedBatchNorm(node) return self.FusedBatchNorm(node)
...@@ -539,10 +512,8 @@ class TFOpMapper(OpMapper): ...@@ -539,10 +512,8 @@ class TFOpMapper(OpMapper):
"use_cudnn": False, "use_cudnn": False,
"padding": string(pad_mode) "padding": string(pad_mode)
} }
node.fluid_code.add_layer("conv2d", node.fluid_code.add_layer(
inputs=input, "conv2d", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Reshape(self, node): def Reshape(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -562,18 +533,17 @@ class TFOpMapper(OpMapper): ...@@ -562,18 +533,17 @@ class TFOpMapper(OpMapper):
attr = {"shape": shape} attr = {"shape": shape}
self.add_omit_nodes(param.layer_name, node.layer_name) self.add_omit_nodes(param.layer_name, node.layer_name)
else: else:
assert len(param.out_shapes[0] assert len(param.out_shapes[
) == 1, "Unexpected situation of shape parameter" 0]) == 1, "Unexpected situation of shape parameter"
attr = {"shape": [-1]} attr = {"shape": [-1]}
node.fluid_code.add_layer("reshape", node.fluid_code.add_layer(
"reshape",
inputs=param, inputs=param,
output="shape_param", output="shape_param",
param_attr=attr) param_attr=attr)
attr = {"num_or_sections": param.out_shapes[0][0], "dim": 0} attr = {"num_or_sections": param.out_shapes[0][0], "dim": 0}
node.fluid_code.add_layer("split", node.fluid_code.add_layer(
inputs="shape_param", "split", inputs="shape_param", output=node, param_attr=attr)
output=node,
param_attr=attr)
new_param = "[" new_param = "["
for i in range(param.out_shapes[0][0]): for i in range(param.out_shapes[0][0]):
new_param += (node.layer_name + "[{}]".format(i) + ", ") new_param += (node.layer_name + "[{}]".format(i) + ", ")
...@@ -601,14 +571,10 @@ class TFOpMapper(OpMapper): ...@@ -601,14 +571,10 @@ class TFOpMapper(OpMapper):
if len(input.out_shapes[0]) == 4 and node.tf_data_format == "NHWC": if len(input.out_shapes[0]) == 4 and node.tf_data_format == "NHWC":
if len(attr["shape"]) < 3: if len(attr["shape"]) < 3:
perm = {"perm": [0, 2, 3, 1]} perm = {"perm": [0, 2, 3, 1]}
node.fluid_code.add_layer("transpose", node.fluid_code.add_layer(
inputs=input, "transpose", inputs=input, output=node, param_attr=perm)
output=node, node.fluid_code.add_layer(
param_attr=perm) "reshape", inputs=node, output=node, param_attr=attr)
node.fluid_code.add_layer("reshape",
inputs=node,
output=node,
param_attr=attr)
return return
if len(attr["shape"]) == 4 and node.tf_data_format == "NHWC": if len(attr["shape"]) == 4 and node.tf_data_format == "NHWC":
...@@ -617,27 +583,19 @@ class TFOpMapper(OpMapper): ...@@ -617,27 +583,19 @@ class TFOpMapper(OpMapper):
attr["shape"] = [attr["shape"][i] for i in [0, 3, 1, 2]] attr["shape"] = [attr["shape"][i] for i in [0, 3, 1, 2]]
else: else:
perm = {"perm": [0, 2, 3, 1]} perm = {"perm": [0, 2, 3, 1]}
node.fluid_code.add_layer("transpose", node.fluid_code.add_layer(
inputs=input, "transpose", inputs=input, output=node, param_attr=perm)
output=node, node.fluid_code.add_layer(
param_attr=perm) "reshape", inputs=node, output=node, param_attr=attr)
node.fluid_code.add_layer("reshape",
inputs=node,
output=node,
param_attr=attr)
perm = {"perm": [0, 3, 1, 2]} perm = {"perm": [0, 3, 1, 2]}
node.fluid_code.add_layer("transpose", node.fluid_code.add_layer(
inputs=node, "transpose", inputs=node, output=node, param_attr=perm)
output=node,
param_attr=perm)
return return
if len(attr["shape"]) == 5: if len(attr["shape"]) == 5:
attr["shape"] = [attr["shape"][i] for i in [0, 1, 4, 2, 3]] attr["shape"] = [attr["shape"][i] for i in [0, 1, 4, 2, 3]]
node.fluid_code.add_layer("reshape", node.fluid_code.add_layer(
inputs=input, "reshape", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def AvgPool(self, node): def AvgPool(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -665,10 +623,8 @@ class TFOpMapper(OpMapper): ...@@ -665,10 +623,8 @@ class TFOpMapper(OpMapper):
"pool_stride": strides[2:4], "pool_stride": strides[2:4],
"pool_padding": string(pad_mode) "pool_padding": string(pad_mode)
} }
node.fluid_code.add_layer("pool2d", node.fluid_code.add_layer(
inputs=input, "pool2d", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def SplitV(self, node): def SplitV(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -685,28 +641,24 @@ class TFOpMapper(OpMapper): ...@@ -685,28 +641,24 @@ class TFOpMapper(OpMapper):
"num_or_sections": num_sections.value.tolist(), "num_or_sections": num_sections.value.tolist(),
"dim": dim.value "dim": dim.value
} }
node.fluid_code.add_layer("split", node.fluid_code.add_layer(
inputs=input, "split", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def ConcatV2(self, node): def ConcatV2(self, node):
inputs = [ inputs = [
self.graph.get_node(name, copy=True) self.graph.get_node(
for name in node.layer.input[:-1] name, copy=True) for name in node.layer.input[:-1]
] ]
axis = self.graph.get_node(node.layer.input[-1], copy=True) axis = self.graph.get_node(node.layer.input[-1], copy=True)
assert axis.layer_type == "Const" assert axis.layer_type == "Const"
self.add_omit_nodes(axis.layer_name, node.layer_name) self.add_omit_nodes(axis.layer_name, node.layer_name)
axis = axis.value axis = axis.value
if inputs[0].tf_data_format == "NHWC" and len( if inputs[0].tf_data_format == "NHWC" and len(inputs[0].out_shapes[
inputs[0].out_shapes[0]) == 4: 0]) == 4:
axis = nhwc_dim_to_nchw(inputs[0], axis) axis = nhwc_dim_to_nchw(inputs[0], axis)
attr = {"axis": axis} attr = {"axis": axis}
node.fluid_code.add_layer("concat", node.fluid_code.add_layer(
inputs=inputs, "concat", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Tile(self, node): def Tile(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -726,18 +678,17 @@ class TFOpMapper(OpMapper): ...@@ -726,18 +678,17 @@ class TFOpMapper(OpMapper):
expand_times[i] = 1 expand_times[i] = 1
attr = {"expand_times": expand_times} attr = {"expand_times": expand_times}
node.fluid_code.add_layer("expand", node.fluid_code.add_layer(
inputs=input, "expand", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Pack(self, node): def Pack(self, node):
inputs = [ inputs = [
self.graph.get_node(name, copy=True) for name in node.layer.input self.graph.get_node(
name, copy=True) for name in node.layer.input
] ]
axis = node.get_attr("axis") axis = node.get_attr("axis")
if inputs[0].tf_data_format == "NHWC" and len( if inputs[0].tf_data_format == "NHWC" and len(inputs[0].out_shapes[
inputs[0].out_shapes[0]) == 4: 0]) == 4:
tf_data_format = list(inputs[0].tf_data_format) tf_data_format = list(inputs[0].tf_data_format)
tf_data_format.insert(axis, str(len(tf_data_format))) tf_data_format.insert(axis, str(len(tf_data_format)))
axis = nhwc_dim_to_nchw(inputs[0], axis) axis = nhwc_dim_to_nchw(inputs[0], axis)
...@@ -747,10 +698,8 @@ class TFOpMapper(OpMapper): ...@@ -747,10 +698,8 @@ class TFOpMapper(OpMapper):
node.pd_data_format = "".join(pd_data_format) node.pd_data_format = "".join(pd_data_format)
attr = {"axis": axis} attr = {"axis": axis}
node.fluid_code.add_layer("stack", node.fluid_code.add_layer(
inputs=inputs, "stack", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Pad(self, node): def Pad(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -767,10 +716,8 @@ class TFOpMapper(OpMapper): ...@@ -767,10 +716,8 @@ class TFOpMapper(OpMapper):
paddings = paddings[4:] paddings = paddings[4:]
pad_op = "pad2d" pad_op = "pad2d"
attr = {"paddings": paddings} attr = {"paddings": paddings}
node.fluid_code.add_layer(pad_op, node.fluid_code.add_layer(
inputs=input, pad_op, inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def MirrorPad(self, node): def MirrorPad(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -789,10 +736,8 @@ class TFOpMapper(OpMapper): ...@@ -789,10 +736,8 @@ class TFOpMapper(OpMapper):
paddings = paddings[4:] paddings = paddings[4:]
pad_op = "pad2d" pad_op = "pad2d"
attr = {"paddings": paddings, "mode": string("reflect")} attr = {"paddings": paddings, "mode": string("reflect")}
node.fluid_code.add_layer(pad_op, node.fluid_code.add_layer(
inputs=input, pad_op, inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Range(self, node): def Range(self, node):
start = self.graph.get_node(node.layer.input[0], copy=True) start = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -816,10 +761,8 @@ class TFOpMapper(OpMapper): ...@@ -816,10 +761,8 @@ class TFOpMapper(OpMapper):
inputs = {"start": start, "end": limit, "step": delta} inputs = {"start": start, "end": limit, "step": delta}
attr = {"dtype": string(node.dtype)} attr = {"dtype": string(node.dtype)}
node.fluid_code.add_layer("range", node.fluid_code.add_layer(
inputs=inputs, "range", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Mean(self, node): def Mean(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -833,10 +776,8 @@ class TFOpMapper(OpMapper): ...@@ -833,10 +776,8 @@ class TFOpMapper(OpMapper):
dims[i] = nhwc_dim_to_nchw(input, dims[i]) dims[i] = nhwc_dim_to_nchw(input, dims[i])
attr = {"dim": dims, "keep_dim": keep_dims} attr = {"dim": dims, "keep_dim": keep_dims}
node.fluid_code.add_layer("reduce_mean", node.fluid_code.add_layer(
inputs=input, "reduce_mean", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def MatMul(self, node): def MatMul(self, node):
x = self.graph.get_node(node.layer.input[0], copy=True) x = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -850,15 +791,11 @@ class TFOpMapper(OpMapper): ...@@ -850,15 +791,11 @@ class TFOpMapper(OpMapper):
shape = x.out_shapes[0] shape = x.out_shapes[0]
shape[-1] = y.out_shapes[0][0] shape[-1] = y.out_shapes[0][0]
attr = {"shape": shape} attr = {"shape": shape}
node.fluid_code.add_layer("reshape", node.fluid_code.add_layer(
inputs=x, "reshape", inputs=x, output=x, param_attr=attr)
output=x,
param_attr=attr)
attr = {"transpose_x": transpose_a, "transpose_y": transpose_b} attr = {"transpose_x": transpose_a, "transpose_y": transpose_b}
node.fluid_code.add_layer("matmul", node.fluid_code.add_layer(
inputs=inputs, "matmul", inputs=inputs, output=node, param_attr=attr)
output=node,
param_attr=attr)
def ArgMax(self, node): def ArgMax(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -869,10 +806,8 @@ class TFOpMapper(OpMapper): ...@@ -869,10 +806,8 @@ class TFOpMapper(OpMapper):
if input.tf_data_format == "NHWC" and len(input.out_shapes[0]) == 4: if input.tf_data_format == "NHWC" and len(input.out_shapes[0]) == 4:
axis = nhwc_dim_to_nchw(input, axis) axis = nhwc_dim_to_nchw(input, axis)
attr = {"axis": axis} attr = {"axis": axis}
node.fluid_code.add_layer("argmax", node.fluid_code.add_layer(
inputs=input, "argmax", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def StridedSlice(self, node): def StridedSlice(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -910,16 +845,12 @@ class TFOpMapper(OpMapper): ...@@ -910,16 +845,12 @@ class TFOpMapper(OpMapper):
x = shrink_axis_mask >> i & 1 x = shrink_axis_mask >> i & 1
if x == 1: if x == 1:
squeeze_dims.append(i) squeeze_dims.append(i)
node.fluid_code.add_layer("slice", node.fluid_code.add_layer(
inputs=input, "slice", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
if shrink_axis_mask > 0 and len(input.out_shapes[0]) == 5: if shrink_axis_mask > 0 and len(input.out_shapes[0]) == 5:
attr = {"axes": squeeze_dims} attr = {"axes": squeeze_dims}
node.fluid_code.add_layer("squeeze", node.fluid_code.add_layer(
inputs=node, "squeeze", inputs=node, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Slice(self, node): def Slice(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -951,10 +882,8 @@ class TFOpMapper(OpMapper): ...@@ -951,10 +882,8 @@ class TFOpMapper(OpMapper):
"starts": begin, "starts": begin,
"ends": size "ends": size
} }
node.fluid_code.add_layer("slice", node.fluid_code.add_layer(
inputs=input, "slice", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Conv2DBackpropInput(self, node): def Conv2DBackpropInput(self, node):
out_shape = self.graph.get_node(node.layer.input[0], copy=True) out_shape = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1004,10 +933,8 @@ class TFOpMapper(OpMapper): ...@@ -1004,10 +933,8 @@ class TFOpMapper(OpMapper):
"padding": string(pad_mode), "padding": string(pad_mode),
"output_size": out_shape[1:3] "output_size": out_shape[1:3]
} }
node.fluid_code.add_layer("conv2d_transpose", node.fluid_code.add_layer(
inputs=input, "conv2d_transpose", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Max(self, node): def Max(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1019,10 +946,8 @@ class TFOpMapper(OpMapper): ...@@ -1019,10 +946,8 @@ class TFOpMapper(OpMapper):
dim = nhwc_dim_to_nchw(input, dim) dim = nhwc_dim_to_nchw(input, dim)
attr = {"dim": dim, "keep_dim": keep_dims} attr = {"dim": dim, "keep_dim": keep_dims}
node.fluid_code.add_layer("reduce_max", node.fluid_code.add_layer(
inputs=input, "reduce_max", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Sum(self, node): def Sum(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1034,19 +959,15 @@ class TFOpMapper(OpMapper): ...@@ -1034,19 +959,15 @@ class TFOpMapper(OpMapper):
dim = nhwc_dim_to_nchw(input, dim) dim = nhwc_dim_to_nchw(input, dim)
attr = {"dim": dim, "keep_dim": keep_dims} attr = {"dim": dim, "keep_dim": keep_dims}
node.fluid_code.add_layer("reduce_sum", node.fluid_code.add_layer(
inputs=input, "reduce_sum", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Cast(self, node): def Cast(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
dtype = node.dtype_map[node.get_attr('DstT')] dtype = node.dtype_map[node.get_attr('DstT')]
attr = {"dtype": string(dtype)} attr = {"dtype": string(dtype)}
node.fluid_code.add_layer("cast", node.fluid_code.add_layer(
inputs=input, "cast", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Split(self, node): def Split(self, node):
dim = self.graph.get_node(node.layer.input[0], copy=True) dim = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1058,10 +979,8 @@ class TFOpMapper(OpMapper): ...@@ -1058,10 +979,8 @@ class TFOpMapper(OpMapper):
dim = nhwc_dim_to_nchw(input, dim) dim = nhwc_dim_to_nchw(input, dim)
attr = {"num_or_sections": num_split, "dim": dim} attr = {"num_or_sections": num_split, "dim": dim}
node.fluid_code.add_layer("split", node.fluid_code.add_layer(
inputs=input, "split", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Squeeze(self, node): def Squeeze(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1070,10 +989,8 @@ class TFOpMapper(OpMapper): ...@@ -1070,10 +989,8 @@ class TFOpMapper(OpMapper):
for i in range(len(squeeze_dims)): for i in range(len(squeeze_dims)):
squeeze_dims[i] = nhwc_dim_to_nchw(input, squeeze_dims[i]) squeeze_dims[i] = nhwc_dim_to_nchw(input, squeeze_dims[i])
attr = {"axes": squeeze_dims} attr = {"axes": squeeze_dims}
node.fluid_code.add_layer("squeeze", node.fluid_code.add_layer(
inputs=input, "squeeze", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def Softmax(self, node): def Softmax(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1083,10 +1000,8 @@ class TFOpMapper(OpMapper): ...@@ -1083,10 +1000,8 @@ class TFOpMapper(OpMapper):
if input.tf_data_format == "NHWC" and len(input.out_shapes[0]) == 4: if input.tf_data_format == "NHWC" and len(input.out_shapes[0]) == 4:
axis = nhwc_dim_to_nchw(input, axis) axis = nhwc_dim_to_nchw(input, axis)
attr = {"axis": axis} attr = {"axis": axis}
node.fluid_code.add_layer("softmax", node.fluid_code.add_layer(
inputs=input, "softmax", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def ResizeNearestNeighbor(self, node): def ResizeNearestNeighbor(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1095,14 +1010,12 @@ class TFOpMapper(OpMapper): ...@@ -1095,14 +1010,12 @@ class TFOpMapper(OpMapper):
if resize_shape.layer_type == "Const": if resize_shape.layer_type == "Const":
resize_shape = resize_shape.value.tolist() resize_shape = resize_shape.value.tolist()
else: else:
resize_shape = self.decoder.infer_shape_tensor( resize_shape = self.decoder.infer_shape_tensor(resize_shape,
resize_shape, node.out_shapes[0]) node.out_shapes[0])
align_corners = node.get_attr("align_corners") align_corners = node.get_attr("align_corners")
attr = {"align_corners": align_corners, "out_shape": resize_shape} attr = {"align_corners": align_corners, "out_shape": resize_shape}
node.fluid_code.add_layer("resize_nearest", node.fluid_code.add_layer(
inputs=input, "resize_nearest", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def ResizeBilinear(self, node): def ResizeBilinear(self, node):
input = self.graph.get_node(node.layer.input[0], copy=True) input = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1111,27 +1024,23 @@ class TFOpMapper(OpMapper): ...@@ -1111,27 +1024,23 @@ class TFOpMapper(OpMapper):
if resize_shape.layer_type == "Const": if resize_shape.layer_type == "Const":
resize_shape = resize_shape.value.tolist() resize_shape = resize_shape.value.tolist()
else: else:
resize_shape = self.decoder.infer_shape_tensor( resize_shape = self.decoder.infer_shape_tensor(resize_shape,
resize_shape, node.out_shapes[0]) node.out_shapes[0])
align_corners = node.get_attr("align_corners") align_corners = node.get_attr("align_corners")
attr = { attr = {
"align_corners": align_corners, "align_corners": align_corners,
"out_shape": resize_shape, "out_shape": resize_shape,
"align_mode": 1 "align_mode": 1
} }
node.fluid_code.add_layer("resize_bilinear", node.fluid_code.add_layer(
inputs=input, "resize_bilinear", inputs=input, output=node, param_attr=attr)
output=node,
param_attr=attr)
def GreaterEqual(self, node): def GreaterEqual(self, node):
x = self.graph.get_node(node.layer.input[0], copy=True) x = self.graph.get_node(node.layer.input[0], copy=True)
y = self.graph.get_node(node.layer.input[1], copy=True) y = self.graph.get_node(node.layer.input[1], copy=True)
inputs = {"x": x, "y": y} inputs = {"x": x, "y": y}
node.fluid_code.add_layer("greater_equal", node.fluid_code.add_layer(
inputs=inputs, "greater_equal", inputs=inputs, output=node, param_attr=None)
output=node,
param_attr=None)
def RandomUniform(self, node): def RandomUniform(self, node):
shape = self.graph.get_node(node.layer.input[0], copy=True) shape = self.graph.get_node(node.layer.input[0], copy=True)
...@@ -1145,26 +1054,21 @@ class TFOpMapper(OpMapper): ...@@ -1145,26 +1054,21 @@ class TFOpMapper(OpMapper):
attr = {"shape": shape, "min": 0.0, "max": 0.9999} attr = {"shape": shape, "min": 0.0, "max": 0.9999}
if shape[0] < 0: if shape[0] < 0:
input = self.batch_node input = self.batch_node
node.fluid_code.add_layer("uniform_random_batch_size_like", node.fluid_code.add_layer(
"uniform_random_batch_size_like",
inputs=input, inputs=input,
output=node, output=node,
param_attr=attr) param_attr=attr)
else: else:
node.fluid_code.add_layer("uniform_random", node.fluid_code.add_layer(
inputs=None, "uniform_random", inputs=None, output=node, param_attr=attr)
output=node,
param_attr=attr)
def SquaredDifference(self, node): def SquaredDifference(self, node):
x = self.graph.get_node(node.layer.input[0], copy=True) x = self.graph.get_node(node.layer.input[0], copy=True)
y = self.graph.get_node(node.layer.input[1], copy=True) y = self.graph.get_node(node.layer.input[1], copy=True)
inputs = {"x": x, "y": y} inputs = {"x": x, "y": y}
node.fluid_code.add_layer("elementwise_sub", node.fluid_code.add_layer(
inputs=inputs, "elementwise_sub", inputs=inputs, output=node, param_attr=None)
output=node,
param_attr=None)
inputs = {"x": node, "y": node} inputs = {"x": node, "y": node}
node.fluid_code.add_layer("elementwise_mul", node.fluid_code.add_layer(
inputs=inputs, "elementwise_mul", inputs=inputs, output=node, param_attr=None)
output=node,
param_attr=None)
...@@ -486,8 +486,8 @@ class TFOpMapperNHWC(OpMapper): ...@@ -486,8 +486,8 @@ class TFOpMapperNHWC(OpMapper):
attr = {"shape": shape} attr = {"shape": shape}
self.add_omit_nodes(param.layer_name, node.layer_name) self.add_omit_nodes(param.layer_name, node.layer_name)
else: else:
assert len(param.out_shapes[0] assert len(param.out_shapes[
) == 1, "Unexpected situation of shape parameter" 0]) == 1, "Unexpected situation of shape parameter"
attr = {"shape": [-1]} attr = {"shape": [-1]}
node.fluid_code.add_layer( node.fluid_code.add_layer(
"reshape", "reshape",
...@@ -577,8 +577,8 @@ class TFOpMapperNHWC(OpMapper): ...@@ -577,8 +577,8 @@ class TFOpMapperNHWC(OpMapper):
def ConcatV2(self, node): def ConcatV2(self, node):
inputs = [ inputs = [
self.graph.get_node(name, copy=True) self.graph.get_node(
for name in node.layer.input[:-1] name, copy=True) for name in node.layer.input[:-1]
] ]
axis = self.graph.get_node(node.layer.input[-1], copy=True) axis = self.graph.get_node(node.layer.input[-1], copy=True)
assert axis.layer_type == "Const" assert axis.layer_type == "Const"
...@@ -608,7 +608,8 @@ class TFOpMapperNHWC(OpMapper): ...@@ -608,7 +608,8 @@ class TFOpMapperNHWC(OpMapper):
def Pack(self, node): def Pack(self, node):
inputs = [ inputs = [
self.graph.get_node(name, copy=True) for name in node.layer.input self.graph.get_node(
name, copy=True) for name in node.layer.input
] ]
axis = node.get_attr("axis") axis = node.get_attr("axis")
attr = {"axis": axis} attr = {"axis": axis}
...@@ -949,8 +950,8 @@ class TFOpMapperNHWC(OpMapper): ...@@ -949,8 +950,8 @@ class TFOpMapperNHWC(OpMapper):
if resize_shape.layer_type == "Const": if resize_shape.layer_type == "Const":
resize_shape = resize_shape.value.tolist() resize_shape = resize_shape.value.tolist()
else: else:
resize_shape = self.decoder.infer_shape_tensor( resize_shape = self.decoder.infer_shape_tensor(resize_shape,
resize_shape, node.out_shapes[0]) node.out_shapes[0])
align_corners = node.get_attr("align_corners") align_corners = node.get_attr("align_corners")
attr = {"perm": [0, 3, 1, 2]} attr = {"perm": [0, 3, 1, 2]}
node.fluid_code.add_layer( node.fluid_code.add_layer(
...@@ -969,8 +970,8 @@ class TFOpMapperNHWC(OpMapper): ...@@ -969,8 +970,8 @@ class TFOpMapperNHWC(OpMapper):
if resize_shape.layer_type == "Const": if resize_shape.layer_type == "Const":
resize_shape = resize_shape.value.tolist() resize_shape = resize_shape.value.tolist()
else: else:
resize_shape = self.decoder.infer_shape_tensor( resize_shape = self.decoder.infer_shape_tensor(resize_shape,
resize_shape, node.out_shapes[0]) node.out_shapes[0])
align_corners = node.get_attr("align_corners") align_corners = node.get_attr("align_corners")
attr = {"perm": [0, 3, 1, 2]} attr = {"perm": [0, 3, 1, 2]}
node.fluid_code.add_layer( node.fluid_code.add_layer(
......
...@@ -41,7 +41,8 @@ class CaffeOptimizer(object): ...@@ -41,7 +41,8 @@ class CaffeOptimizer(object):
if is_delete_node: if is_delete_node:
parent_node.fluid_code.clear() parent_node.fluid_code.clear()
node.fluid_code.clear() node.fluid_code.clear()
node.fluid_code.add_layer("batch_norm", node.fluid_code.add_layer(
"batch_norm",
inputs=input, inputs=input,
output=node, output=node,
param_attr=parent_param_attr) param_attr=parent_param_attr)
...@@ -62,7 +63,8 @@ class CaffeOptimizer(object): ...@@ -62,7 +63,8 @@ class CaffeOptimizer(object):
if is_delete_node: if is_delete_node:
parent_node.fluid_code.clear() parent_node.fluid_code.clear()
node.fluid_code.clear() node.fluid_code.clear()
node.fluid_code.add_layer(op, node.fluid_code.add_layer(
op,
inputs=input, inputs=input,
output=node, output=node,
param_attr=parent_param_attr) param_attr=parent_param_attr)
...@@ -554,7 +554,8 @@ class TFOptimizer(object): ...@@ -554,7 +554,8 @@ class TFOptimizer(object):
node.fluid_code.layers[0].param_attr["shape"] = shape node.fluid_code.layers[0].param_attr["shape"] = shape
node.fluid_code.layers[0].output = "nhwc_" + name node.fluid_code.layers[0].output = "nhwc_" + name
attr = {"perm": [0, 2, 3, 1]} attr = {"perm": [0, 2, 3, 1]}
node.fluid_code.add_layer("transpose", node.fluid_code.add_layer(
"transpose",
inputs="nhwc_" + name, inputs="nhwc_" + name,
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -767,8 +768,8 @@ class TFOptimizer(object): ...@@ -767,8 +768,8 @@ class TFOptimizer(object):
is_prelu = False is_prelu = False
continue continue
if len(in_nodes0[0].outputs) != 1 or len( if len(in_nodes0[0].outputs) != 1 or len(in_nodes0[1]
in_nodes0[1].outputs) != 1: .outputs) != 1:
is_prelu = False is_prelu = False
continue continue
...@@ -777,8 +778,8 @@ class TFOptimizer(object): ...@@ -777,8 +778,8 @@ class TFOptimizer(object):
self.graph.get_node(in_name) self.graph.get_node(in_name)
for in_name in in_nodes0[1].inputs for in_name in in_nodes0[1].inputs
] ]
if in_nodes2[1].layer_type != "Const" or numpy.fabs( if in_nodes2[1].layer_type != "Const" or numpy.fabs(in_nodes2[
in_nodes2[1].value - 0.5) > 1e-06: 1].value - 0.5) > 1e-06:
is_prelu = False is_prelu = False
continue continue
if in_nodes2[0].layer_type != "Mul": if in_nodes2[0].layer_type != "Mul":
...@@ -787,8 +788,8 @@ class TFOptimizer(object): ...@@ -787,8 +788,8 @@ class TFOptimizer(object):
if exist_act(in_nodes2[0]): if exist_act(in_nodes2[0]):
is_prelu = False is_prelu = False
continue continue
if len(in_nodes2[1].outputs) != 1 or len( if len(in_nodes2[1].outputs) != 1 or len(in_nodes2[0]
in_nodes2[0].outputs) != 1: .outputs) != 1:
is_prelu = False is_prelu = False
continue continue
...@@ -803,8 +804,8 @@ class TFOptimizer(object): ...@@ -803,8 +804,8 @@ class TFOptimizer(object):
if exist_act(in_nodes3[1]): if exist_act(in_nodes3[1]):
is_prelu = False is_prelu = False
continue continue
if len(in_nodes3[0].outputs) != 1 or len( if len(in_nodes3[0].outputs) != 1 or len(in_nodes3[1]
in_nodes3[1].outputs) != 1: .outputs) != 1:
is_prelu = False is_prelu = False
continue continue
...@@ -856,12 +857,12 @@ class TFOptimizer(object): ...@@ -856,12 +857,12 @@ class TFOptimizer(object):
mode = "element" mode = "element"
elif len(in_nodes3[0].value.shape) == 0: elif len(in_nodes3[0].value.shape) == 0:
mode = "all" mode = "all"
elif len(in_nodes3[0].value.shape elif len(in_nodes3[0].value.shape) == 1 and in_nodes3[
) == 1 and in_nodes3[0].value.shape[0] == 1: 0].value.shape[0] == 1:
mode = "all" mode = "all"
elif len(in_shape) == 4 and len( elif len(in_shape) == 4 and len(in_nodes3[
in_nodes3[0].value.shape 0].value.shape) == 1 and in_nodes3[0].value.shape[
) == 1 and in_nodes3[0].value.shape[0] == in_shape[-1]: 0] == in_shape[-1]:
mode = "channel" mode = "channel"
weight = self.op_mapper.weights[in_nodes3[0].layer_name] weight = self.op_mapper.weights[in_nodes3[0].layer_name]
weight = numpy.expand_dims(weight, 0) weight = numpy.expand_dims(weight, 0)
...@@ -916,14 +917,15 @@ class TFOptimizer(object): ...@@ -916,14 +917,15 @@ class TFOptimizer(object):
self.graph.get_node(in_name) for in_name in node.inputs self.graph.get_node(in_name) for in_name in node.inputs
] ]
if in_nodes0[0].layer_type != "Mul" or in_nodes0[ if in_nodes0[0].layer_type != "Mul" or in_nodes0[
1].layer_type != "Const" or in_nodes0[1].value.size != 1: 1].layer_type != "Const" or in_nodes0[
1].value.size != 1:
is_scale = False is_scale = False
continue continue
if exist_act(in_nodes0[0]): if exist_act(in_nodes0[0]):
is_scale = False is_scale = False
continue continue
if len(in_nodes0[0].outputs) != 1 or len( if len(in_nodes0[0].outputs) != 1 or len(in_nodes0[1]
in_nodes0[1].outputs) != 1: .outputs) != 1:
is_scale = False is_scale = False
continue continue
...@@ -939,8 +941,8 @@ class TFOptimizer(object): ...@@ -939,8 +941,8 @@ class TFOptimizer(object):
if exist_act(in_nodes1[1]): if exist_act(in_nodes1[1]):
is_scale = False is_scale = False
continue continue
if len(in_nodes1[0].outputs) != 1 or len( if len(in_nodes1[0].outputs) != 1 or len(in_nodes1[1]
in_nodes1[1].outputs) != 1: .outputs) != 1:
is_scale = False is_scale = False
continue continue
...@@ -962,8 +964,8 @@ class TFOptimizer(object): ...@@ -962,8 +964,8 @@ class TFOptimizer(object):
scale = 1.0 / in_nodes2[1].value * in_nodes1[0].value scale = 1.0 / in_nodes2[1].value * in_nodes1[0].value
act = None act = None
if node.fluid_code.layers[0].param_attr is not None: if node.fluid_code.layers[0].param_attr is not None:
act = node.fluid_code.layers[0].param_attr.get( act = node.fluid_code.layers[0].param_attr.get("act",
"act", None) None)
node.fluid_code.clear() node.fluid_code.clear()
attr = { attr = {
...@@ -972,10 +974,8 @@ class TFOptimizer(object): ...@@ -972,10 +974,8 @@ class TFOptimizer(object):
"bias_after_scale": True, "bias_after_scale": True,
"act": act "act": act
} }
node.fluid_code.add_layer("scale", node.fluid_code.add_layer(
inputs=in_node, "scale", inputs=in_node, output=node, param_attr=attr)
output=node,
param_attr=attr)
del self.graph.node_map[in_nodes0[0].layer_name] del self.graph.node_map[in_nodes0[0].layer_name]
del self.graph.node_map[in_nodes0[1].layer_name] del self.graph.node_map[in_nodes0[1].layer_name]
...@@ -1004,17 +1004,17 @@ class TFOptimizer(object): ...@@ -1004,17 +1004,17 @@ class TFOptimizer(object):
if exist_act(in_nodes0[0]): if exist_act(in_nodes0[0]):
is_affine_channel = False is_affine_channel = False
continue continue
if len(in_nodes0[0].outputs) != 1 or len( if len(in_nodes0[0].outputs) != 1 or len(in_nodes0[1]
in_nodes0[1].outputs) != 1: .outputs) != 1:
is_affine_channel = False is_affine_channel = False
continue continue
in_nodes1 = [ in_nodes1 = [
self.graph.get_node(in_name) self.graph.get_node(in_name)
for in_name in in_nodes0[0].inputs for in_name in in_nodes0[0].inputs
] ]
if len(in_nodes1[0].out_shapes[0] if len(in_nodes1[0].out_shapes[0]) != 4 or in_nodes1[
) != 4 or in_nodes1[1].layer_type != "Const" or len( 1].layer_type != "Const" or len(in_nodes1[1]
in_nodes1[1].value.shape) != 3: .value.shape) != 3:
is_affine_channel = False is_affine_channel = False
continue continue
if len(in_nodes1[1].outputs) != 1: if len(in_nodes1[1].outputs) != 1:
...@@ -1037,8 +1037,8 @@ class TFOptimizer(object): ...@@ -1037,8 +1037,8 @@ class TFOptimizer(object):
node.layer_type = "AffineChannel" node.layer_type = "AffineChannel"
node.inputs = [in_node.layer_name] node.inputs = [in_node.layer_name]
scale = 1.0 / in_nodes0[1].value.flatten() scale = 1.0 / in_nodes0[1].value.flatten()
bias = in_nodes1[1].value.flatten( bias = in_nodes1[1].value.flatten() / in_nodes0[
) / in_nodes0[1].value.flatten() 1].value.flatten()
if not bias_add: if not bias_add:
bias *= -1.0 bias *= -1.0
self.op_mapper.weights[node.layer_name + "_scale"] = scale self.op_mapper.weights[node.layer_name + "_scale"] = scale
...@@ -1046,8 +1046,8 @@ class TFOptimizer(object): ...@@ -1046,8 +1046,8 @@ class TFOptimizer(object):
act = None act = None
if node.fluid_code.layers[0].param_attr is not None: if node.fluid_code.layers[0].param_attr is not None:
act = node.fluid_code.layers[0].param_attr.get( act = node.fluid_code.layers[0].param_attr.get("act",
"act", None) None)
node.fluid_code.clear() node.fluid_code.clear()
attr = { attr = {
...@@ -1055,7 +1055,8 @@ class TFOptimizer(object): ...@@ -1055,7 +1055,8 @@ class TFOptimizer(object):
"shape": [channel], "shape": [channel],
"name": string(node.layer_name + "_scale") "name": string(node.layer_name + "_scale")
} }
node.fluid_code.add_layer("create_parameter", node.fluid_code.add_layer(
"create_parameter",
inputs=None, inputs=None,
output=node.layer_name + "_scale", output=node.layer_name + "_scale",
param_attr=attr) param_attr=attr)
...@@ -1064,7 +1065,8 @@ class TFOptimizer(object): ...@@ -1064,7 +1065,8 @@ class TFOptimizer(object):
"shape": [channel], "shape": [channel],
"name": string(node.layer_name + "_bias") "name": string(node.layer_name + "_bias")
} }
node.fluid_code.add_layer("create_parameter", node.fluid_code.add_layer(
"create_parameter",
inputs=None, inputs=None,
output=node.layer_name + "_bias", output=node.layer_name + "_bias",
param_attr=attr) param_attr=attr)
...@@ -1074,7 +1076,8 @@ class TFOptimizer(object): ...@@ -1074,7 +1076,8 @@ class TFOptimizer(object):
"bias": node.layer_name + "_bias" "bias": node.layer_name + "_bias"
} }
attr = {"act": act} attr = {"act": act}
node.fluid_code.add_layer("affine_channel", node.fluid_code.add_layer(
"affine_channel",
inputs=inputs, inputs=inputs,
output=node, output=node,
param_attr=attr) param_attr=attr)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册