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

Merge pull request #49 from SunAhong1993/develop

fix some bug for BN
...@@ -120,24 +120,42 @@ class CaffeGraph(Graph): ...@@ -120,24 +120,42 @@ class CaffeGraph(Graph):
inputs_num = len(self.model.input) inputs_num = len(self.model.input)
if inputs_num != 0: if inputs_num != 0:
input_dims_num = len(self.model.input_dim) input_dims_num = len(self.model.input_dim)
if input_dims_num > 0 and input_dims_num != inputs_num * 4: if input_dims_num != 0:
raise Error('invalid input_dim[%d] param in prototxt' % if input_dims_num > 0 and input_dims_num != inputs_num * 4:
(input_dims_num)) raise Error('invalid input_dim[%d] param in prototxt' %
for i in range(inputs_num): (input_dims_num))
dims = self.model.input_dim[i * 4:(i + 1) * 4] for i in range(inputs_num):
data = self.model.layer.add() dims = self.model.input_dim[i * 4:(i + 1) * 4]
try: data = self.model.layer.add()
from caffe import layers as L try:
data.CopyFrom( from caffe import layers as L
L.Input(input_param=dict(shape=dict( data.CopyFrom(
dim=[dims[0], dims[1], dims[2], dims[3] L.Input(input_param=dict(shape=dict(
]))).to_proto().layer[0]) dim=[dims[0], dims[1], dims[2], dims[3]
except: ]))).to_proto().layer[0])
raise Error( except:
'You must install the caffe first when you use old style prototxt.' raise ImportError(
) 'You must install the caffe first when you use old style prototxt.'
data.name = self.model.input[0] )
data.top[0] = self.model.input[0] data.name = self.model.input[i]
data.top[0] = self.model.input[i]
else:
for i in range(inputs_num):
dims = self.model.input_shape[i].dim[0:4]
data = self.model.layer.add()
try:
from caffe import layers as L
data.CopyFrom(
L.Input(input_param=dict(shape=dict(
dim=[dims[0], dims[1], dims[2], dims[3]
]))).to_proto().layer[0])
except:
raise ImportError(
'You must install the caffe first when you use old style prototxt.'
)
data.name = self.model.input[i]
data.top[0] = self.model.input[i]
layers = [data] + layers
top_layer = {} top_layer = {}
for layer in layers: for layer in layers:
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# limitations under the License. # limitations under the License.
import numbers import numbers
import numpy as np
from x2paddle.decoder.caffe_decoder import CaffeGraph from x2paddle.decoder.caffe_decoder import CaffeGraph
from x2paddle.core.op_mapper import OpMapper from x2paddle.core.op_mapper import OpMapper
from x2paddle.core.util import * from x2paddle.core.util import *
...@@ -156,6 +157,12 @@ class CaffeOpMapper(OpMapper): ...@@ -156,6 +157,12 @@ class CaffeOpMapper(OpMapper):
else: else:
return node.layer_name return node.layer_name
def is_BN(self, node):
return True if node.layer_type == 'BatchNorm' else False
def is_Scale(self, node):
return True if node.layer_type == 'Scale' else False
def Input(self, node): def Input(self, node):
shape = list(node.layer.input_param.shape[0].dim)[1:] shape = list(node.layer.input_param.shape[0].dim)[1:]
dtype = 'float32' dtype = 'float32'
...@@ -183,16 +190,30 @@ class CaffeOpMapper(OpMapper): ...@@ -183,16 +190,30 @@ class CaffeOpMapper(OpMapper):
assert len(node.inputs assert len(node.inputs
) == 1, 'The count of Convolution node\'s input is not 1.' ) == 1, 'The count of Convolution 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = { attr = {
'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': string(node.layer_name + '_bias'), 'dilation':
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", node.fluid_code.add_layer("conv2d",
inputs=input, inputs=input,
...@@ -213,17 +234,31 @@ class CaffeOpMapper(OpMapper): ...@@ -213,17 +234,31 @@ class CaffeOpMapper(OpMapper):
assert len(node.inputs assert len(node.inputs
) == 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = { attr = {
'output_size': None, 'output_size':
'filter_size': kernel, None,
'num_filters': channel, 'filter_size':
'stride': stride, kernel,
'padding': pad, 'num_filters':
'dilation': dilation, channel,
'groups': group, 'stride':
'name': string(node.layer_name), stride,
'param_attr': string(node.layer_name + '_weights'), 'padding':
'bias_attr': string(node.layer_name + '_bias') pad,
'dilation':
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("conv2d_transpose",
inputs=input, inputs=input,
...@@ -243,6 +278,10 @@ class CaffeOpMapper(OpMapper): ...@@ -243,6 +278,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of Pooling node\'s input is not 1.' node.inputs) == 1, 'The count of Pooling 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = { attr = {
'pool_size': kernel, 'pool_size': kernel,
'pool_stride': stride, 'pool_stride': stride,
...@@ -262,6 +301,10 @@ class CaffeOpMapper(OpMapper): ...@@ -262,6 +301,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of ReLU node\'s input is not 1.' node.inputs) == 1, 'The count of ReLU 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = {'name': string(node.layer_name)} attr = {'name': string(node.layer_name)}
node.fluid_code.add_layer("relu", node.fluid_code.add_layer("relu",
inputs=input, inputs=input,
...@@ -279,6 +322,10 @@ class CaffeOpMapper(OpMapper): ...@@ -279,6 +322,10 @@ class CaffeOpMapper(OpMapper):
# We'll account for that here. # We'll account for that here.
alpha = params.alpha / float(params.local_size) alpha = params.alpha / float(params.local_size)
input = self.graph.get_bottom_node(node, idx=0, copy=True) input = self.graph.get_bottom_node(node, idx=0, copy=True)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = { attr = {
'n': params.local_size, 'n': params.local_size,
'k': 1.0, 'k': 1.0,
...@@ -314,12 +361,21 @@ class CaffeOpMapper(OpMapper): ...@@ -314,12 +361,21 @@ class CaffeOpMapper(OpMapper):
assert params.axis == 1 assert params.axis == 1
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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = { attr = {
'size': params.num_output, 'size':
'name': string(node.layer_name), params.num_output,
'act': None, 'name':
'param_attr': string(node.layer_name + '_weights'), string(node.layer_name),
'bias_attr': string(node.layer_name + '_bias') 'act':
None,
'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("fc",
inputs=input, inputs=input,
...@@ -330,6 +386,10 @@ class CaffeOpMapper(OpMapper): ...@@ -330,6 +386,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of Softmax node\'s input is not 1.' node.inputs) == 1, 'The count of Softmax 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
params = node.layer.softmax_param params = node.layer.softmax_param
axis = params.axis axis = params.axis
shape = node.input_shape[0] shape = node.input_shape[0]
...@@ -374,6 +434,10 @@ class CaffeOpMapper(OpMapper): ...@@ -374,6 +434,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of Slice node\'s input is not 1.' node.inputs) == 1, 'The count of Slice 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
params = node.layer.slice_param params = node.layer.slice_param
axis = params.axis axis = params.axis
points = list(params.slice_point) points = list(params.slice_point)
...@@ -406,6 +470,10 @@ class CaffeOpMapper(OpMapper): ...@@ -406,6 +470,10 @@ class CaffeOpMapper(OpMapper):
inputs = [] inputs = []
for i in range(len(node.inputs)): for i in range(len(node.inputs)):
input = self.graph.get_bottom_node(node, idx=i, copy=True) input = self.graph.get_bottom_node(node, idx=i, copy=True)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
inputs.append(input) inputs.append(input)
params = node.layer.concat_param params = node.layer.concat_param
axis = params.axis axis = params.axis
...@@ -419,6 +487,10 @@ class CaffeOpMapper(OpMapper): ...@@ -419,6 +487,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of PReLU node\'s input is not 1.' node.inputs) == 1, 'The count of PReLU 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
params = node.layer.prelu_param params = node.layer.prelu_param
mode_bool = params.channel_shared mode_bool = params.channel_shared
if mode_bool: if mode_bool:
...@@ -443,6 +515,10 @@ class CaffeOpMapper(OpMapper): ...@@ -443,6 +515,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of PReLU node\'s input is not 1.' node.inputs) == 1, 'The count of PReLU 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = {'name': string(node.layer_name)} attr = {'name': string(node.layer_name)}
node.fluid_code.add_layer("sigmoid", node.fluid_code.add_layer("sigmoid",
inputs=input, inputs=input,
...@@ -453,6 +529,10 @@ class CaffeOpMapper(OpMapper): ...@@ -453,6 +529,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of PReLU node\'s input is not 1.' node.inputs) == 1, 'The count of PReLU 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = {'name': string(node.layer_name)} attr = {'name': string(node.layer_name)}
node.fluid_code.add_layer("absval", node.fluid_code.add_layer("absval",
inputs=input, inputs=input,
...@@ -468,9 +548,19 @@ class CaffeOpMapper(OpMapper): ...@@ -468,9 +548,19 @@ class CaffeOpMapper(OpMapper):
i = 0 i = 0
for shape in node.input_shape: for shape in node.input_shape:
if shape[1] == 1: if shape[1] == 1:
inputs[1] = self.graph.get_bottom_node(node, idx=i, copy=True) input = self.graph.get_bottom_node(node, idx=i, copy=True)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
inputs[1] = input
else: else:
inputs[0] = self.graph.get_bottom_node(node, idx=i, copy=True) input = self.graph.get_bottom_node(node, idx=i, copy=True)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
inputs[0] = input
i += 1 i += 1
params = node.layer.accuracy_param params = node.layer.accuracy_param
top_k = params.top_k top_k = params.top_k
...@@ -489,6 +579,10 @@ class CaffeOpMapper(OpMapper): ...@@ -489,6 +579,10 @@ class CaffeOpMapper(OpMapper):
assert len( assert len(
node.inputs) == 1, 'The count of TanH node\'s input is not 1.' node.inputs) == 1, 'The count of TanH 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
attr = {'name': string(node.layer_name)} attr = {'name': string(node.layer_name)}
node.fluid_code.add_layer("tanh", node.fluid_code.add_layer("tanh",
inputs=input, inputs=input,
...@@ -501,12 +595,25 @@ class CaffeOpMapper(OpMapper): ...@@ -501,12 +595,25 @@ class CaffeOpMapper(OpMapper):
params = node.layer.eltwise_param params = node.layer.eltwise_param
mode = params.operation mode = params.operation
inputs = [] inputs = []
inputs.append(self.graph.get_bottom_node(node, idx=0, copy=True)) input0 = self.graph.get_bottom_node(node, idx=0, copy=True)
inputs.append(self.graph.get_bottom_node(node, idx=1, copy=True)) if self.is_Scale(input0):
tmp = self.graph.get_bottom_node(input0, idx=0, copy=True)
if self.is_BN(tmp):
input0 = tmp
inputs.append(input0)
input1 = self.graph.get_bottom_node(node, idx=1, copy=True)
if self.is_Scale(input1):
tmp = self.graph.get_bottom_node(input1, idx=0, copy=True)
if self.is_BN(tmp):
input1 = tmp
inputs.append(input1)
if mode == 0: if mode == 0:
inputs_dict = {}
inputs_dict['x'] = inputs[0]
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, inputs=inputs_dict,
output=node, output=node,
param_attr=attr) param_attr=attr)
elif mode == 1: elif mode == 1:
...@@ -552,15 +659,21 @@ class CaffeOpMapper(OpMapper): ...@@ -552,15 +659,21 @@ class CaffeOpMapper(OpMapper):
output=node, output=node,
param_attr=attr) param_attr=attr)
else: else:
inputs_dict = {}
inputs_dict['x'] = inputs[0]
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, inputs=inputs_dict,
output=node, output=node,
param_attr=attr) param_attr=attr)
else: else:
inputs_dict = {}
inputs_dict['x'] = inputs[0]
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, inputs=inputs_dict,
output=node, output=node,
param_attr=attr) param_attr=attr)
...@@ -569,8 +682,12 @@ class CaffeOpMapper(OpMapper): ...@@ -569,8 +682,12 @@ class CaffeOpMapper(OpMapper):
node.outputs node.outputs
) == 1, 'The count of BatchNorm node\'s input and output is not 1.' ) == 1, 'The count of BatchNorm node\'s input and output 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)
if self.is_Scale(input):
tmp = self.graph.get_bottom_node(input, idx=0, copy=True)
if self.is_BN(tmp):
input = tmp
params = node.layer.batch_norm_param params = node.layer.batch_norm_param
if hasattr(params, eps): if hasattr(params, 'eps'):
eps = params.eps eps = params.eps
else: else:
eps = 1e-5 eps = 1e-5
...@@ -631,7 +748,15 @@ class CaffeOpMapper(OpMapper): ...@@ -631,7 +748,15 @@ class CaffeOpMapper(OpMapper):
axis = 1 axis = 1
bias_shape = node.input_shape[0][axis:axis + num_axes] bias_shape = node.input_shape[0][axis:axis + num_axes]
input0 = self.graph.get_bottom_node(node, idx=0, copy=True) input0 = self.graph.get_bottom_node(node, idx=0, copy=True)
if self.is_Scale(input0):
tmp = self.graph.get_bottom_node(input0, idx=0, copy=True)
if self.is_BN(tmp):
input0 = tmp
input1 = self.graph.get_bottom_node(node, idx=1, copy=True) input1 = self.graph.get_bottom_node(node, idx=1, copy=True)
if self.is_Scale(input1):
tmp = self.graph.get_bottom_node(input1, idx=0, copy=True)
if self.is_BN(tmp):
input1 = tmp
inputs.append(input0) inputs.append(input0)
inputs.append(input1) inputs.append(input1)
attr = {'axis': axis, 'name': string(node.layer_name + '_mul')} attr = {'axis': axis, 'name': string(node.layer_name + '_mul')}
...@@ -642,6 +767,10 @@ class CaffeOpMapper(OpMapper): ...@@ -642,6 +767,10 @@ class CaffeOpMapper(OpMapper):
else: else:
bias_shape = node.input_shape[0][axis:axis + num_axes] bias_shape = node.input_shape[0][axis:axis + num_axes]
input0 = self.graph.get_bottom_node(node, idx=0, copy=True) input0 = self.graph.get_bottom_node(node, idx=0, copy=True)
if self.is_Scale(input0):
tmp = self.graph.get_bottom_node(input0, idx=0, copy=True)
if self.is_BN(tmp):
input0 = tmp
input0_name = self.get_input_name(input0) input0_name = self.get_input_name(input0)
attr = { attr = {
'dtype': '{}.dtype'.formatr(input0_name), 'dtype': '{}.dtype'.formatr(input0_name),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册