keras icon indicating copy to clipboard operation
keras copied to clipboard

Keras-DepthwiseConv2D can still accept strides != 1 && dilation_rate != 1

Open mazeltovlee opened this issue 2 years ago • 2 comments

System information.

  • Have I written custom code (as opposed to using a stock example script provided in Keras): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): 2.8.0
  • Python version: 3.7.13
  • Bazel version (if compiling from source): N/A
  • GPU model and memory: N/A
  • Exact command to reproduce:
import keras
input_shape = (10, 14, 14, 516)
x = keras.layers.Input((14,14,516))
y = keras.layers.DepthwiseConv2D(kernel_size=3, strides=(2,2), padding="same", dilation_rate=(3,1))(x)
print(y.shape)
(None, 8, 7, 516)

Describe the problem. Following the documentation of DepthwiseConv2D: https://keras.io/api/layers/convolution_layers/depthwise_convolution2d/ strides value != 1 and dilation_rate value != 1 is an incorrect hyperparameter setting. However, DepthwiseConv2D can still output some results, besides the output tensor shape (8, 7, 516) does not satisfy padding="same" (i.e., output shape should be the same as input shape).

For comparison, Conv2D will directly raise an Exception for this hyperparameter setting.

import keras
input_shape = (10, 14, 14, 516)
x = keras.layers.Input((14,14,516))
y = keras.layers.Conv2D(32, kernel_size=3, strides=(2,2), padding="same", dilation_rate=(3,1))(x)
print(y.shape)

ValueError: Exception encountered when calling layer "conv2d_3" (type Conv2D).

`strides > 1` not supported in conjunction with `dilation_rate > 1`. Received: strides=[2 2] and dilation_rate=[3 1]

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 14, 14, 516), dtype=float32)

Describe the current behavior. DepthwiseConv2D can accept strides != 1 && dilation_rate != 1

Describe the expected behavior. This API should raise an exception like Conv2D

  • Do you want to contribute a PR? (yes/no):
  • If yes, please read this page for instructions
  • Briefly describe your candidate solution(if contributing):

Standalone code to reproduce the issue.

import keras
input_shape = (10, 14, 14, 516)
x = keras.layers.Input((14,14,516))
y = keras.layers.DepthwiseConv2D(kernel_size=3, strides=(2,2), padding="same", dilation_rate=(3,1))(x)
print(y.shape)

mazeltovlee avatar Mar 28 '22 13:03 mazeltovlee

Hi, I also observe the same issue on SeparableConv2D.

import keras
input_shape = (10, 14, 14, 516)
x = keras.layers.Input((14,14,516))
y = keras.layers.SeparableConv2D(10, kernel_size=3, strides=(2,2), padding="same", dilation_rate=(3,1))(x)
print(y.shape)

That is, I think it would be better if these APIs could raise an exception to alarm users when setting strides != 1 && dilation_rate != 1.

mazeltovlee avatar Apr 01 '22 05:04 mazeltovlee

The issue was able to reproduce on colab using TF-nightly 2.11.0-dev20220921, Please find the gist here for reference. Thank you

chunduriv avatar Sep 24 '22 06:09 chunduriv