Unverified Commit a3b13973 by Zhao Wu Committed by GitHub

[Frontend] Asymmetric padding of convolution support (#4803)

parent dbd01140
......@@ -77,10 +77,7 @@ def _ConvolutionLayerParams(op, inexpr, etab):
pad_b = valid.paddingAmounts.borderAmounts[0].endEdgeSize
pad_r = valid.paddingAmounts.borderAmounts[1].endEdgeSize
if not all(v == 0 for v in (pad_t, pad_l, pad_b, pad_r)):
inexpr = _op.nn.pad(data=inexpr, pad_width=((0, 0),
(0, 0),
(pad_t, pad_b),
(pad_l, pad_r)))
params['padding'] = (pad_t, pad_l, pad_b, pad_r)
elif op.WhichOneof('ConvolutionPaddingType') == 'same':
assert op.same.asymmetryMode == 0, "Only support BOTTOM_RIGHT_HEAVY mode, " \
"which is used by tf/caffe and so on"
......@@ -88,11 +85,7 @@ def _ConvolutionLayerParams(op, inexpr, etab):
strides = params['strides']
pad_t, pad_b = get_pad_value(H, kernel[0], strides[0])
pad_l, pad_r = get_pad_value(W, kernel[1], strides[1])
inexpr = _op.nn.pad(data=inexpr, pad_width=((0, 0),
(0, 0),
(pad_t, pad_b),
(pad_l, pad_r)))
params['padding'] = (pad_t, pad_l, pad_b, pad_r)
else:
raise NotImplementedError("Valid/Same convolution padding implemented")
......
......@@ -290,15 +290,7 @@ def _convert_convolution(inexpr, keras_layer, etab):
in_w = keras_layer.input_shape[2]
pad_t, pad_b = _get_pad_pair(in_h, dilated_kernel_h, stride_h)
pad_l, pad_r = _get_pad_pair(in_w, dilated_kernel_w, stride_w)
if pad_t == pad_b and pad_l == pad_r:
params['padding'] = (pad_t, pad_l)
elif etab.data_layout == 'NCHW':
inexpr = _op.nn.pad(data=inexpr, pad_width=(
(0, 0), (0, 0), (pad_t, pad_b), (pad_l, pad_r)))
else:
inexpr = _op.nn.pad(data=inexpr, pad_width=(
(0, 0), (pad_t, pad_b), (pad_l, pad_r), (0, 0)))
params['padding'] = (pad_t, pad_l, pad_b, pad_r)
else:
msg = 'Padding with {} is not supported for operator Convolution ' \
'in frontend Keras.'
......@@ -424,15 +416,7 @@ def _convert_separable_convolution(inexpr, keras_layer, etab):
in_w = keras_layer.input_shape[2]
pad_t, pad_b = _get_pad_pair(in_h, kernel_h, stride_h)
pad_l, pad_r = _get_pad_pair(in_w, kernel_w, stride_w)
if pad_t == pad_b and pad_l == pad_r:
params0['padding'] = (pad_t, pad_l)
elif etab.data_layout == 'NCHW':
inexpr = _op.nn.pad(data=inexpr, pad_width=(
(0, 0), (0, 0), (pad_t, pad_b), (pad_l, pad_r)))
else:
inexpr = _op.nn.pad(data=inexpr, pad_width=(
(0, 0), (pad_t, pad_b), (pad_l, pad_r), (0, 0)))
params0['padding'] = (pad_t, pad_l, pad_b, pad_r)
else:
msg = 'Padding with {} is not supported for operator Separable ' \
'Convolution in frontend Keras.'
......
......@@ -1308,13 +1308,7 @@ class OperatorConverter(object):
pad_left, pad_right = get_pad_value(input_w, dilated_kernel_w, stride_w)
do_pad = not (pad_top == 0 and pad_bottom == 0 and pad_left == 0 and pad_right == 0)
if do_pad:
pad_value = 0
if input_tensor.qnn_params:
pad_value = get_scalar_from_constant(input_tensor.qnn_params['zero_point'])
in_expr = _op.nn.pad(data=in_expr, pad_width=((0, 0),
(pad_top, pad_bottom),
(pad_left, pad_right),
(0, 0)), pad_value=float(pad_value))
params['padding'] = [pad_top, pad_left, pad_bottom, pad_right]
else:
raise tvm.error.OpAttributeUnImplemented(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment