简体   繁体   English

为什么我的输入和来自 keras conv2d 的 output 形状尺寸相同?

[英]Why is my input and output shape from keras conv2d the same dimensions?

I'm trying to rebuild someone else's.network with this shape:我正在尝试用这种形状重建别人的网络:

在此处输入图像描述

My (image) data going into the.network has this shape:我的(图像)数据进入 .network 具有以下形状:

print(X_train[0].shape)
print(len(X_train))
print(len(y_train))

(150,150,3)
2160
2160

I can write and get a neural.network to run no problem:我可以写一个 neural.network 来运行没问题:

model = Sequential()
model.add(Input(shape=(150,150,3)))
model.add(Conv2D(32, kernel_size=3,strides=(1, 1),activation='relu', padding='same', dilation_rate=1))
model.add(MaxPooling2D(pool_size=(2, 2)))

But then when I view the plot, it looks like this:但是当我查看 plot 时,它看起来像这样:

在此处输入图像描述

Can someone explain to me why my output of the Conv2D layer does not decrease from 150 to 148, as expected?有人可以向我解释为什么我的 Conv2D 层的 output 没有按预期从 150 减少到 148 吗? (Presumably then, the 'wrong' numbers in the max_pooling layers are a consequence of this, so I only need to focus on understanding the discrepancy in the Conv2D layer). (据推测,max_pooling 层中的“错误”数字是这个的结果,所以我只需要专注于理解 Conv2D 层中的差异)。

A possible solution is to use padding=valid in the Conv2D layer:一种可能的解决方案是在Conv2D层中使用padding=valid

model = Sequential()
model.add(Input(shape=(150,150,3)))
model.add(Conv2D(32, kernel_size=3, strides=(1, 1), activation='relu', padding='valid', dilation_rate=1))
model.add(MaxPooling2D(pool_size=(2, 2)))

The resulting summary:结果总结:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d (Conv2D)             (None, 148, 148, 32)      896       
                                                                 
 max_pooling2d (MaxPooling2D)  (None, 74, 74, 32)       0         

=================================================================

You use padding='same so you dont "loose" any values on the side您使用padding='same这样您就不会“松动”侧面的任何值

This has a good gif on different padding strategies 对不同的填充策略有很好的 gif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM