Document and test explicit padding for separable_conv2d.

Explicit padding already worked for separable conv2d once support was added to depthwise conv2d, but it was not documented or tested.

PiperOrigin-RevId: 308897692
Change-Id: Ic8a58eb26c814f4d9fefdaec4471cfff20a8c971
This commit is contained in:
Reed Wanderman-Milne 2020-04-28 14:27:58 -07:00 committed by TensorFlower Gardener
parent f7f727388c
commit b9f3811fbb
2 changed files with 57 additions and 4 deletions
tensorflow/python

View File

@ -2662,6 +2662,8 @@ class SeparableConv2DTest(test.TestCase):
if data_format == "NCHW":
real_t1 = array_ops.transpose(t1, [0, 3, 1, 2])
strides = [1, 1, stride, stride]
if isinstance(padding, list):
padding = [padding[0], padding[3], padding[1], padding[2]]
conv = nn_impl.separable_conv2d(
real_t1,
@ -2756,6 +2758,45 @@ class SeparableConv2DTest(test.TestCase):
return
self._testSeparableConv2DEqualInputOutputDepth("NCHW")
def _testSeparableConv2dExplicitPadding(self, data_format):
tensor_in_sizes = [1, 4, 4, 2]
depthwise_filter_in_sizes = [2, 2, 2, 3]
pointwise_filter_in_sizes = [1, 1, 6, 7]
padding = [[0, 0], [1, 2], [3, 4], [0, 0]]
with self.cached_session(use_gpu=True):
# Compute the 'expected' values by manually padding before calling
# separable_conv2d
t1 = self._InitValues(tensor_in_sizes)
t1 = array_ops.pad(t1, padding)
f1 = self._InitValues(depthwise_filter_in_sizes)
f1.set_shape(depthwise_filter_in_sizes)
f2 = self._InitValues(pointwise_filter_in_sizes)
conv = nn_impl.separable_conv2d(
t1,
f1,
f2,
strides=[1, 1, 1, 1],
padding="VALID",
data_format="NHWC")
expected = self.evaluate(conv)
expected = np.ravel(expected)
self._VerifyValues(
tensor_in_sizes=tensor_in_sizes,
depthwise_filter_in_sizes=depthwise_filter_in_sizes,
pointwise_filter_in_sizes=pointwise_filter_in_sizes,
stride=1,
padding=padding,
expected=expected,
data_format=data_format)
def testSeparableConv2dExplicitPadding(self):
self._testSeparableConv2dExplicitPadding("NHWC")
def testSeparableConv2dExplicitPaddingNCHW(self):
if not test.is_gpu_available():
return
self._testSeparableConv2dExplicitPadding("NCHW")
class DeepConv2DTest(test.TestCase):

View File

@ -974,8 +974,14 @@ def separable_conv2d(input,
filter to mix channels after `depthwise_filter` has convolved spatially.
strides: 1-D of size 4. The strides for the depthwise convolution for
each dimension of `input`.
padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm.
See the "returns" section of `tf.nn.convolution` for details.
padding: Controls how to pad the image before applying the depthwise
convolution. Can be the string `"SAME"` or `"VALID"` indicating the type
of padding algorithm to use, or a Python list indicating the explicit
paddings at the start and end of each dimension. When explicit padding is
used and data_format is `"NHWC"`, this should be in the form `[[0, 0],
[pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]`. When explicit
padding used and data_format is `"NCHW"`, this should be in the form
`[[0, 0], [0, 0], [pad_top, pad_bottom], [pad_left, pad_right]]`.
rate: 1-D of size 2. The dilation rate in which we sample input values
across the `height` and `width` dimensions in atrous convolution. If it is
greater than 1, then all values of strides must be 1.
@ -1076,8 +1082,14 @@ def separable_conv2d_v2(
`depthwise_filter` has convolved spatially.
strides: 1-D of size 4. The strides for the depthwise convolution for each
dimension of `input`.
padding: A string, either `'VALID'` or `'SAME'`. The padding algorithm. See
the "returns" section of `tf.nn.convolution` for details.
padding: Controls how to pad the image before applying the depthwise
convolution. Can be the string `"SAME"` or `"VALID"` indicating the type
of padding algorithm to use, or a Python list indicating the explicit
paddings at the start and end of each dimension. When explicit padding is
used and data_format is `"NHWC"`, this should be in the form `[[0, 0],
[pad_top, pad_bottom], [pad_left, pad_right], [0, 0]]`. When explicit
padding used and data_format is `"NCHW"`, this should be in the form
`[[0, 0], [0, 0], [pad_top, pad_bottom], [pad_left, pad_right]]`.
data_format: The data format for input. Either "NHWC" (default) or "NCHW".
dilations: 1-D of size 2. The dilation rate in which we sample input values
across the `height` and `width` dimensions in atrous convolution. If it is