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

Merge pull request #163 from PaddlePaddle/develop

pull
...@@ -129,9 +129,12 @@ def caffe2paddle(proto, weight, save_dir, caffe_proto): ...@@ -129,9 +129,12 @@ def caffe2paddle(proto, weight, save_dir, caffe_proto):
from x2paddle.op_mapper.caffe_op_mapper import CaffeOpMapper from x2paddle.op_mapper.caffe_op_mapper import CaffeOpMapper
from x2paddle.optimizer.caffe_optimizer import CaffeOptimizer from x2paddle.optimizer.caffe_optimizer import CaffeOptimizer
import google.protobuf as gpb import google.protobuf as gpb
ver_str = gpb.__version__.replace('.', '') ver_part = gpb.__version__.split('.')
ver_int = int(ver_str[0:2]) version_satisfy = False
assert ver_int >= 36, 'The version of protobuf must be larger than 3.6.0!' if (int(ver_part[0]) == 3 and int(ver_part[1]) >= 6) \
or (int(ver_part[0]) > 3):
version_satisfy = True
assert version_satisfy, 'google.protobuf >= 3.6.0 is required'
print("Now translating model from caffe to paddle.") print("Now translating model from caffe to paddle.")
model = CaffeDecoder(proto, weight, caffe_proto) model = CaffeDecoder(proto, weight, caffe_proto)
mapper = CaffeOpMapper(model) mapper = CaffeOpMapper(model)
......
...@@ -8,13 +8,7 @@ def shufflechannel_shape(input_shape): ...@@ -8,13 +8,7 @@ def shufflechannel_shape(input_shape):
def shufflechannel_layer(inputs, group=None, input_shape=None, name=None): def shufflechannel_layer(inputs, group=None, input_shape=None, name=None):
input = inputs[0] input = inputs[0]
c_fm = fluid.layers.split(input, num_or_sections=input_shape[0][1], dim=1) out = fluid.layers.shuffle_channel(x=input, group=group)
size = int(input_shape[0][1] / group)
new_c_fm = []
for i in range(size):
for j in range(group):
new_c_fm.append(c_fm[j * size + i])
out = fluid.layers.concat(new_c_fm, axis=1)
return out return out
......
...@@ -363,7 +363,7 @@ class CaffeOpMapper(OpMapper): ...@@ -363,7 +363,7 @@ 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 = {
'n': params.local_size, 'n': params.local_size,
'k': 1.0, 'k': params.k,
'alpha': alpha, 'alpha': alpha,
'beta': params.beta, 'beta': params.beta,
'name': string(node.layer_name) 'name': string(node.layer_name)
...@@ -450,35 +450,19 @@ class CaffeOpMapper(OpMapper): ...@@ -450,35 +450,19 @@ class CaffeOpMapper(OpMapper):
slice_dim = params.slice_dim slice_dim = params.slice_dim
if slice_dim != 1 and axis == 1: if slice_dim != 1 and axis == 1:
axis = slice_dim axis = slice_dim
points = list(params.slice_point) output_shape = node.output_shape
sections_list = []
if len(points) == 0: for s in output_shape:
dims = node.input_shape[0][axis] sections_list.append(s[axis])
assert dims % top_len == 0, "the parameter of Slice is wrong"
part = dims / top_len
t = part
while t < dims:
points.append(int(t))
t += part
maxint32 = 2147483647
points = [0] + points
points.append(maxint32)
i = 0
node.fluid_code.add_note('{} = []'.format(node.layer_name))
for i in range(len(points)):
attr = { attr = {
'axes': [axis], 'num_or_sections': sections_list,
'starts': [points[i]], 'dim': axis,
'ends': [points[i + 1]] 'name': string(node.layer_name)
} }
node.fluid_code.add_layer("slice", node.fluid_code.add_layer("split",
inputs=input, inputs=input,
output=node.layer_name + '_' + str(i), output=node.layer_name,
param_attr=attr) param_attr=attr)
node.fluid_code.add_note('{}.append({})'.format(
node.layer_name, node.layer_name + '_' + str(i)))
if i == len(points) - 2:
break
def Concat(self, node): def Concat(self, node):
assert len( assert len(
...@@ -649,7 +633,8 @@ class CaffeOpMapper(OpMapper): ...@@ -649,7 +633,8 @@ class CaffeOpMapper(OpMapper):
]).astype('float32') ]).astype('float32')
scale = 0 scale = 0
else: else:
node.data = [np.squeeze(i) for i in node.data]
node.data = [np.squeeze(i).astype('float32') for i in node.data]
mean, variance, scale = node.data mean, variance, scale = node.data
# Prescale the stats # Prescale the stats
scaling_factor = 1.0 / scale if scale != 0 else 0 scaling_factor = 1.0 / scale if scale != 0 else 0
...@@ -684,8 +669,10 @@ class CaffeOpMapper(OpMapper): ...@@ -684,8 +669,10 @@ class CaffeOpMapper(OpMapper):
input_c, input_c,
]).astype('float32') ]).astype('float32')
else: else:
self.weights[node.layer_name + '_scale'] = np.squeeze(node.data[0]) self.weights[node.layer_name + '_scale'] = np.squeeze(
self.weights[node.layer_name + '_offset'] = np.squeeze(node.data[1]) node.data[0]).astype('float32')
self.weights[node.layer_name + '_offset'] = np.squeeze(
node.data[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
......
...@@ -168,7 +168,11 @@ class TFOpMapper(OpMapper): ...@@ -168,7 +168,11 @@ class TFOpMapper(OpMapper):
x_input = y x_input = y
y_input = x y_input = x
x_shape = y.out_shapes[0] x_shape = y.out_shapes[0]
if len(x_shape) == 0:
x_shape = [1]
y_shape = x.out_shapes[0] y_shape = x.out_shapes[0]
if len(y_shape) == 0:
y_shape = [1]
else: else:
if len(x_shape) == 1 and len(y_shape) == 4 and x_shape[ if len(x_shape) == 1 and len(y_shape) == 4 and x_shape[
0] == y_shape[-1] and y_shape.count(-1) < 1: 0] == y_shape[-1] and y_shape.count(-1) < 1:
......
...@@ -121,6 +121,25 @@ class TFOpMapperNHWC(OpMapper): ...@@ -121,6 +121,25 @@ class TFOpMapperNHWC(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
if len(input.out_shapes[0]) == 4 and op_info[0] != 'shape':
attr1 = {"perm": [0, 3, 1, 2]}
node.fluid_code.add_layer('transpose',
inputs=input,
output=node,
param_attr=attr1)
input = node
node.fluid_code.add_layer(op_info[0],
inputs=input,
output=node,
param_attr=attr)
input = node
attr2 = {"perm": [0, 2, 3, 1]}
node.fluid_code.add_layer('transpose',
inputs=input,
output=node,
param_attr=attr2)
else:
node.fluid_code.add_layer(op_info[0], node.fluid_code.add_layer(op_info[0],
inputs=input, inputs=input,
output=node, output=node,
...@@ -149,7 +168,11 @@ class TFOpMapperNHWC(OpMapper): ...@@ -149,7 +168,11 @@ class TFOpMapperNHWC(OpMapper):
x_input = y x_input = y
y_input = x y_input = x
x_shape = y.out_shapes[0] x_shape = y.out_shapes[0]
if len(x_shape) == 0:
x_shape = [1]
y_shape = x.out_shapes[0] y_shape = x.out_shapes[0]
if len(y_shape) == 0:
y_shape = [1]
else: else:
raise Exception("Unexpected situation happend") raise Exception("Unexpected situation happend")
...@@ -193,6 +216,25 @@ class TFOpMapperNHWC(OpMapper): ...@@ -193,6 +216,25 @@ class TFOpMapperNHWC(OpMapper):
output="y_tmp", output="y_tmp",
param_attr=attr) param_attr=attr)
y_input = "y_tmp" y_input = "y_tmp"
if len(x_shape) == 4 and len(y_shape) == 4:
node.fluid_code.add_layer("transpose",
inputs=x_input,
output=x_input,
param_attr={'perm': [0, 3, 1, 2]})
node.fluid_code.add_layer("transpose",
inputs=y_input,
output=y_input,
param_attr={'perm': [0, 3, 1, 2]})
inputs = {"x": x_input, "y": y_input}
node.fluid_code.add_layer(op_type,
inputs=inputs,
output=node,
param_attr=None)
node.fluid_code.add_layer("transpose",
inputs=node,
output=node,
param_attr={'perm': [0, 2, 3, 1]})
else:
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(op_type,
inputs=inputs, inputs=inputs,
...@@ -978,9 +1020,7 @@ class TFOpMapperNHWC(OpMapper): ...@@ -978,9 +1020,7 @@ class TFOpMapperNHWC(OpMapper):
if pad_mode == "SAME": if pad_mode == "SAME":
if node.tf_data_format == "NHWC": if node.tf_data_format == "NHWC":
print(out_shape)
out_shape = [out_shape[i] for i in [0, 3, 1, 2]] out_shape = [out_shape[i] for i in [0, 3, 1, 2]]
print(out_shape)
for i in range(4): for i in range(4):
if out_shape[i] < 0: if out_shape[i] < 0:
out_shape[i] = 999999 out_shape[i] = 999999
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册