Conv2d() comparison between pytorch and tensorflow

pytorch

torch.nn.Conv2d()parameter

in_channels  # 输入数据的通道数(彩色图片为3,灰度图为1,卷积中可以设置更加高维的通道数)
out_channels  # 输出数据的通道数
kernel_size  # 卷积核的尺寸(可填整数和元组,3与(3,3)等同,但建议填元组)
stride = 1,  # 卷积步长,就是卷积操作时每次移动的格子数,可填整数和元组,3与(3,3)等同,但建议填元组
padding = 0,  # 原图周围需要填充的格子行(列)数,无填充的话卷积到边缘会直接忽略该边缘,默认填充0
dilation = 1, # 空洞卷积的空洞指数,一般默认为1即可
groups = 1,  # 分组卷积的组数,一般默认设置为1
bias = True, # 卷积偏置,一般设置为False
padding_mode = 'zeros’  # 设置边缘填充值为0,可设置其他的数,一般都默认设置为0,可增加边缘提取的能力

tensorflow

tensorflow.python.keras.layers.Conv2D
tf.nn.conv2d()
The following interception is tensorflow.python.keras.layers.Conv2Dthe parameters in

filters,
kernel_size,
strides=(1, 1),
padding='valid',
data_format=None,
dilation_rate=(1, 1),
activation=None,
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,

Through the above parameters, it is found that most of the parameters in pytorch are close to tensorflow, but the difference in padding is very large, one is an integer and the other is a string.

There are only two options for padding in keras, SAMEand VALID, SAME is to fill a circle of 0 around the image before convolution, and use VALID to not fill the input image. Therefore, the output obtained by the two padding methods is different.

Output Size Calculation Formula

output_size = (input_size - k + 2p)/ s +1

In keras, it is equivalent to the automatic calculation of the network. There are two options. In pytorch, you need to calculate whether the output image has changed through the above formula.

Guess you like

Origin blog.csdn.net/Zeus_daifu/article/details/128275741