Skip to content

Commit 65565c4

Browse files
committed
fix padding and reshape method
1 parent a6d546e commit 65565c4

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

x2paddle/op_mapper/tf_op_mapper.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
def get_same_padding(in_size, kernel_size, stride):
2525
new_size = int(math.ceil(in_size * 1.0 / stride))
2626
pad_size = (new_size - 1) * stride + kernel_size - in_size
27+
if pad_size < 0:
28+
pad_size = 0
2729
pad0 = int(pad_size / 2)
2830
pad1 = pad_size - pad0
2931
return [pad0, pad1]
@@ -369,12 +371,13 @@ def MaxPool(self, node):
369371
pad_w = get_same_padding(in_shape[3], k_size[3], strides[3])
370372
pad_h = pad_h[0] + pad_h[1]
371373
pad_w = pad_w[0] + pad_w[1]
372-
attr = {"paddings": [0, pad_h, 0, pad_w], "pad_value": -10000.0}
373-
node.fluid_code.add_layer("pad2d",
374-
inputs=input,
375-
output=node,
376-
param_attr=attr)
377-
input = node
374+
if pad_h != 0 or pad_w != 0:
375+
attr = {"paddings": [0, pad_h, 0, pad_w], "pad_value": -10000.0}
376+
node.fluid_code.add_layer("pad2d",
377+
inputs=input,
378+
output=node,
379+
param_attr=attr)
380+
input = node
378381
attr = {
379382
"pool_size": k_size[2:4],
380383
"pool_type": string("max"),
@@ -551,6 +554,7 @@ def DepthwiseConv2dNative(self, node):
551554
def Reshape(self, node):
552555
input = self.graph.get_node(node.layer.input[0], copy=True)
553556
param = self.graph.get_node(node.layer.input[1], copy=True)
557+
is_variable = False
554558
if param.layer_type == "Const":
555559
attr = {"shape": param.value.tolist()}
556560
self.add_omit_nodes(param.layer_name, node.layer_name)
@@ -582,6 +586,24 @@ def Reshape(self, node):
582586
new_param += (node.layer_name + "[{}]".format(i) + ", ")
583587
new_param = new_param.strip(", ") + "]"
584588
attr = {"shape": new_param}
589+
is_variable = True
590+
591+
# to change [192, -1]->[-1, 192], allways put -1 in the first dimension
592+
# optimization for Paddle-Lite
593+
in_shape = input.out_shapes[0]
594+
if is_variable and in_shape.count(-1) < 1:
595+
total_size = 1
596+
for i in range(len(in_shape)):
597+
total_size *= in_shape[i]
598+
for i in range(len(attr["shape"])):
599+
if attr["shape"][i] == 0:
600+
attr["shape"][i] = in_shape[i]
601+
if attr["shape"][i] != -1:
602+
total_size /= attr["shape"][i]
603+
if attr["shape"].count(-1) > 0:
604+
index = attr["shape"].index(-1)
605+
attr["shape"][index] = int(total_size)
606+
attr["shape"][0] = -1
585607

586608
if len(input.out_shapes[0]) == 4 and node.tf_data_format == "NHWC":
587609
if len(attr["shape"]) < 3:

x2paddle/op_mapper/tf_op_mapper_nhwc.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
def get_same_padding(in_size, kernel_size, stride):
2525
new_size = int(math.ceil(in_size * 1.0 / stride))
2626
pad_size = (new_size - 1) * stride + kernel_size - in_size
27+
if pad_size < 0:
28+
pad_size = 0
2729
pad0 = int(pad_size / 2)
2830
pad1 = pad_size - pad0
2931
return [pad0, pad1]
@@ -500,6 +502,7 @@ def DepthwiseConv2dNative(self, node):
500502
def Reshape(self, node):
501503
input = self.graph.get_node(node.layer.input[0], copy=True)
502504
param = self.graph.get_node(node.layer.input[1], copy=True)
505+
is_variable = False
503506
if param.layer_type == "Const":
504507
attr = {"shape": param.value.tolist()}
505508
self.add_omit_nodes(param.layer_name, node.layer_name)
@@ -527,6 +530,24 @@ def Reshape(self, node):
527530
new_param += (node.layer_name + "[{}]".format(i) + ", ")
528531
new_param = new_param.strip(", ") + "]"
529532
attr = {"shape": new_param}
533+
is_variable = True
534+
# to change [192, -1]->[-1, 192], allways put -1 in the first dimension
535+
# optimization for Paddle-Lite
536+
in_shape = input.out_shapes[0]
537+
if not is_variable and in_shape.count(-1) < 1:
538+
total_size = 1
539+
for i in range(len(in_shape)):
540+
total_size *= in_shape[i]
541+
for i in range(len(attr["shape"])):
542+
if attr["shape"][i] == 0:
543+
attr["shape"][i] = in_shape[i]
544+
if attr["shape"][i] != -1:
545+
total_size /= attr["shape"][i]
546+
if attr["shape"].count(-1) > 0:
547+
index = attr["shape"].index(-1)
548+
attr["shape"][index] = int(total_size)
549+
attr["shape"][0] = -1
550+
530551
node.fluid_code.add_layer("reshape",
531552
inputs=input,
532553
output=node,

0 commit comments

Comments
 (0)