gpt4 book ai didi

python - padding ='same' 转换为 PyTorch padding=#

转载 作者:行者123 更新时间:2023-12-04 12:20:43 26 4
gpt4 key购买 nike

我正在尝试将以下 Keras 模型代码转换为 pytorch,但在处理 padding='same' 时遇到问题。

    model = Sequential()
model.add(Conv2D(64, (3, 3), input_shape=img_size))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu'))
model.add(Dropout(0.3))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(BatchNormalization(axis=1))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same'))

这产生以下摘要:
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D) (None, 30, 30, 64) 1792
_________________________________________________________________
batch_normalization_1 (Batch (None, 30, 30, 64) 120
_________________________________________________________________
activation_1 (Activation) (None, 30, 30, 64) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 30, 30, 64) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 30, 30, 64) 36928
_________________________________________________________________
batch_normalization_2 (Batch (None, 30, 30, 64) 120
_________________________________________________________________
activation_2 (Activation) (None, 30, 30, 64) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 15, 15, 64) 0
=================================================================
Total params: 38,960
Trainable params: 38,840
Non-trainable params: 120

现在,我会写:
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3,
bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Conv2d(64, 64, kernel_size=3, padding = ?
bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding = ?),
)

填充应该有数值的地方。我想知道是否有更简单的方法来计算这个,因为我们使用的是 padding='same'。

此外,Keras 模型的下一行看起来像:
model.add(Conv2D(128, (3, 3), padding='same'))

所以我真的需要复习如何计算填充,尤其是在 stride 之后。 仅从粗略的角度来看,填充是 2 吗?

最佳答案

W:输入音量大小

F:内核大小

S:步幅

P:填充量

输出音量大小 = (W-F+2P)/S+1

例如

输入:7x7,内核:3x3,步幅:1,pad:0

输出尺寸 = (7-3+2*0)/1+1 = 5 =>5x5

关于python - padding ='same' 转换为 PyTorch padding=#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62166719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com