Skip to content

Commit c5a78fc

Browse files
Add support for Explicit padding (#1458)
Signed-off-by: Tom Wildenhain <[email protected]>
1 parent ac4c68d commit c5a78fc

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

tests/test_backend.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,16 @@ def test_conv2d_6(self):
438438
kernel_val = np.arange(1, 1 + np.prod(kernel_shape)).astype("float32").reshape(kernel_shape)
439439
self._conv_test(x_val, kernel_val, strides=strides, padding="VALID", rtol=1e-05)
440440

441+
@check_tf_min_version("1.14", "tf 1.14 needed for explicit padding")
442+
def test_conv2d_explicit_padding(self):
443+
x_shape = [1, 35, 35, 288]
444+
kernel_shape = [3, 3, 288, 384]
445+
pads = [[0, 0], [1, 2], [3, 4], [0, 0]]
446+
strides = [1, 1, 1, 1]
447+
x_val = np.arange(1, 1 + np.prod(x_shape)).astype("float32").reshape(x_shape)
448+
kernel_val = np.arange(1, 1 + np.prod(kernel_shape)).astype("float32").reshape(kernel_shape)
449+
self._conv_test(x_val, kernel_val, strides=strides, padding=pads, rtol=1e-05)
450+
441451
def test_conv2d_dilation_same(self):
442452
x_shape = [1, 35, 35, 288] # NHWC
443453
kernel_shape = [3, 3, 288, 384] # [filter_height, filter_width, in_channels, out_channels]

tf2onnx/onnx_opset/nn.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,15 @@ def add_padding(ctx, node, kernel_shape, strides, dilations=None, spatial=2):
249249
node.set_attr("pads", pads)
250250
elif padding == "VALID":
251251
pass
252+
elif padding == "EXPLICIT":
253+
pads = node.get_attr_value("explicit_paddings")
254+
start_pads = []
255+
end_pads = []
256+
d = 1 if is_channels_last(node) else 2
257+
for i in range(spatial):
258+
start_pads.append(pads[(d + i) * 2])
259+
end_pads.append(pads[(d + i) * 2 + 1])
260+
node.set_attr("pads", start_pads + end_pads)
252261
else:
253262
raise ValueError("invalid padding value: {}".format(padding))
254263

0 commit comments

Comments
 (0)