[英]Tensorflow/Keras Conv2D layers with padding='SAME' behave strangely
My question:我的问题:
A straightforward experiment that I conducted showed that using padding='SAME'
in a conv2d layer in Keras/TF is different from using padding='VALID'
with a preceding zero-padding layer.我进行的一个简单的实验表明,在 Keras/TF 的 conv2d 层中使用padding='SAME'
padding='VALID'
与在前面的零填充层中使用padding='VALID'
不同。
Explanation of the experiment - just if you're interested in reading further:实验说明 - 如果您有兴趣进一步阅读:
I used the onnx2keras
package to convert my Pytorch model into keras/TF.我使用onnx2keras
包将我的 Pytorch 模型转换为 keras/TF。
When onnx2keras
encounters a convolutional layer with padding > 0
in the ONNX model, it translates it to Keras' Conv2D
with valid
padding (ie, no padding!), preceded by Keras' ZeroPadding2D
layer.当onnx2keras
在ONNX 模型中遇到padding > 0
的卷积层时,它会将其转换为Conv2D
并带有valid
padding(即没有padding!),在ZeroPadding2D
层之前。 This works very well and returns outputs that are identical to those produced by the Pytorch network.这非常有效,并返回与 Pytorch 网络产生的输出相同的输出。
I yet thought it was strange that it didn't simply used padding='SAME'
, as most of the references say that Keras/TF use zero padding, just like Pytorch does.我还觉得奇怪的是它并没有简单地使用padding='SAME'
,因为大多数参考资料都说 Keras/TF 使用零填充,就像 Pytorch 一样。
Nevertheless, I patched onnx2keras
and made it produce me Conv2D
layers with padding='SAME'
rather than the existing solution of 'VALID'
padding with a preceding zero-padding layer.尽管如此,我修补了onnx2keras
并使其生成了带有padding='SAME'
Conv2D
层,而不是使用前面的零填充层的'VALID'
填充的现有解决方案。 This made the resulting model return different outputs than the one with the zero-padding layer, and of course different from my Pytorch model, which was identical until the patch.这使得生成的模型返回与零填充层不同的输出,当然与我的 Pytorch 模型不同,后者在补丁之前是相同的。
padding='Same'
in Keras means padding is added as required to make up for overlaps when the input size and kernel size do not perfectly fit. padding='Same'
表示当输入大小和内核大小不完全适合时,根据需要添加填充以弥补重叠。
Example of padding='Same': padding='Same' 示例:
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2) ,padding='Same'))
# Model Summary
model.summary()
Output of the code -代码的输出 -
Model: "sequential_20"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_28 (Conv2D) (None, 3, 3, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________
Pictorial Representation: Below image shows how the padding for the input (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2)) when padding='Same'.图片表示:下图显示了当 padding='Same' 时输入的填充 (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2))。
padding='Valid'
in Keras means no padding is added. padding='Valid'
表示不添加填充。
Example of padding='Valid': Have used same input for Conv2D that we used above for padding = 'Same' .ie (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2)) padding='Valid' 示例: Conv2D 使用了与我们上面用于 padding = 'Same' 相同的输入。ie (input_shape=(5,5,1), kernel_size=(2,2), strides =(2, 2))
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2) ,padding='Valid'))
# Model Summary
model.summary()
Output of the code -代码的输出 -
Model: "sequential_21"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_29 (Conv2D) (None, 2, 2, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________
Pictorial Representation: Below image shows there is no padding added for the input (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2)) when padding='Valid'.图片表示:下图显示当 padding='Valid' 时,没有为输入添加填充 (input_shape=(5,5,1), kernel_size=(2,2), strides =(2,2))。
Now lets try same code that we used for padding='Valid'
for the input (input_shape=(6,6,1), kernel_size=(2,2), strides =(2,2)).现在让我们尝试使用与padding='Valid'
相同的代码作为输入 (input_shape=(6,6,1), kernel_size=(2,2), strides =(2,2))。 Here padding='Valid'
should behave same as padding='Same'
.这里padding='Valid'
行为应该与padding='Same'
。
Code -代码 -
# Importing dependency
import keras
from keras.models import Sequential
from keras.layers import Conv2D
# Create a sequential model
model = Sequential()
# Convolutional Layer
model.add(Conv2D(filters=24, input_shape=(6,6,1), kernel_size=(2,2), strides =(2,2) ,padding='Valid'))
# Model Summary
model.summary()
Output of the code -代码的输出 -
Model: "sequential_22"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_30 (Conv2D) (None, 3, 3, 24) 120
=================================================================
Total params: 120
Trainable params: 120
Non-trainable params: 0
_________________________________________________________________
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.