Skip to content

Commit cf7ce8a

Browse files
ryVijay Vasudevan
authored and
Vijay Vasudevan
committedApr 19, 2016
Fix SAME padding calc when stride>ksize (#1991)
Adds a failing test case for conv2d ksize=1, stride=2, padding=same, input=4x4 Adds tests convering that case for pooling ops too. There's one test case I found that fails in a different way that I left commented out with R=C=8, K=2, S=4. The error is: F tensorflow/stream_executor/cuda/cuda_dnn.cc:483] could not set cudnn convolution descriptor: CUDNN_STATUS_BAD_PARAM
1 parent 0a37c9f commit cf7ce8a

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed
 

‎tensorflow/core/kernels/ops_util.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,14 @@ Status Get2dOutputSizeVerbose(const int in_height, const int in_width,
5454
*new_width = ceil(in_width / static_cast<float>(col_stride));
5555
// Calculate padding for top/bottom/left/right, spilling any excess
5656
// padding to bottom and right.
57-
const int pad_needed_height =
58-
(*new_height - 1) * row_stride + filter_height - in_height;
57+
const int pad_needed_height = std::max(0,
58+
(*new_height - 1) * row_stride + filter_height - in_height);
5959
*pad_top = pad_needed_height / 2;
60-
CHECK_GE(pad_needed_height, 0);
6160
*pad_bottom = pad_needed_height - *pad_top;
6261

63-
const int pad_needed_width =
64-
(*new_width - 1) * col_stride + filter_width - in_width;
62+
const int pad_needed_width = std::max(0,
63+
(*new_width - 1) * col_stride + filter_width - in_width);
6564
*pad_left = pad_needed_width / 2;
66-
CHECK_GE(pad_needed_width, 0);
6765
*pad_right = pad_needed_width - *pad_left;
6866
break;
6967
}

‎tensorflow/python/kernel_tests/conv_ops_test.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,26 @@ def testConv2DKernelSmallerThanStrideValid(self):
327327
expected=expected_output)
328328

329329
def testConv2DKernelSmallerThanStrideSame(self):
330-
expected_output = [1, 3, 7, 9]
331330
self._VerifyValues(tensor_in_sizes=[1, 3, 3, 1],
332331
filter_in_sizes=[1, 1, 1, 1],
333332
strides=[2, 2], padding="SAME",
334-
expected=expected_output)
333+
expected=[1, 3, 7, 9])
334+
335+
self._VerifyValues(tensor_in_sizes=[1, 4, 4, 1],
336+
filter_in_sizes=[1, 1, 1, 1],
337+
strides=[2, 2], padding="SAME",
338+
expected=[1, 3, 9, 11])
339+
340+
self._VerifyValues(tensor_in_sizes=[1, 4, 4, 1],
341+
filter_in_sizes=[2, 2, 1, 1],
342+
strides=[3, 3], padding="SAME",
343+
expected=[44, 28, 41, 16])
344+
345+
# TODO this currently fails.
346+
#self._VerifyValues(tensor_in_sizes=[1, 8, 8, 1],
347+
# filter_in_sizes=[2, 2, 1, 1],
348+
# strides=[4, 4], padding="SAME",
349+
# expected=[72, 112, 392, 432])
335350

336351
# Testing for backprops
337352
def _RunAndVerifyBackpropInput(self, input_sizes, filter_sizes, output_sizes,

‎tensorflow/python/kernel_tests/pooling_ops_test.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -370,32 +370,35 @@ def testDepthwiseMaxPool2x2DepthWindow3(self):
370370
expected=[3.0, 6.0, 9.0, 12.0, 15.0, 18.0, 21.0, 24.0],
371371
use_gpu=False)
372372

373-
def testKernelSmallerThanStride(self):
373+
def testKernelSmallerThanStrideValid(self):
374374
for use_gpu in [True, False]:
375-
self._VerifyValues(tf.nn.max_pool, input_sizes=[1, 3, 3, 1],
376-
ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1],
377-
padding="SAME",
378-
expected=[1, 3, 7, 9],
379-
use_gpu=use_gpu)
380-
381375
self._VerifyValues(tf.nn.max_pool, input_sizes=[1, 7, 7, 1],
382376
ksize=[1, 2, 2, 1], strides=[1, 3, 3, 1],
383377
padding="VALID",
384378
expected=[9, 12, 30, 33],
385379
use_gpu=use_gpu)
386380

387-
self._VerifyValues(tf.nn.avg_pool, input_sizes=[1, 3, 3, 1],
388-
ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1],
389-
padding="SAME",
390-
expected=[1, 3, 7, 9],
391-
use_gpu=use_gpu)
392-
393381
self._VerifyValues(tf.nn.avg_pool, input_sizes=[1, 7, 7, 1],
394382
ksize=[1, 2, 2, 1], strides=[1, 3, 3, 1],
395383
padding="VALID",
396384
expected=[5, 8, 26, 29],
397385
use_gpu=use_gpu)
398386

387+
def testKernelSmallerThanStrideSame(self):
388+
for use_gpu in [True, False]:
389+
for pool_func in [tf.nn.max_pool, tf.nn.avg_pool]:
390+
self._VerifyValues(pool_func, input_sizes=[1, 3, 3, 1],
391+
ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1],
392+
padding="SAME",
393+
expected=[1, 3, 7, 9],
394+
use_gpu=use_gpu)
395+
396+
self._VerifyValues(pool_func, input_sizes=[1, 4, 4, 1],
397+
ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1],
398+
padding="SAME",
399+
expected=[1, 3, 9, 11],
400+
use_gpu=use_gpu)
401+
399402

400403
def _testDepthwiseMaxPoolInvalidConfig(self, in_size, ksize, strides,
401404
error_msg, use_gpu=False):

0 commit comments

Comments
 (0)
Please sign in to comment.