目录

标准卷积Standard-Convolution

目录

标准卷积(Standard Convolution)

标准卷积的基础操作图解:

https://i-blog.csdnimg.cn/direct/557b74b404704a0cbb3dcdac9af8eab2.jpeg

卷积之后尺寸公式:

  • 输入尺寸:W×H
  • 卷积核尺寸:Fw​×Fh​
  • 填充大小:P
  • 步长:S

输出尺寸 Wout×Hout可以通过以下公式计算:

https://latex.csdn.net/eq?W_%7Bout%7D%3D%5B%5Cfrac%7BW+2P-F_%7Bw%7D%7D%7BS%7D%5D+1

https://latex.csdn.net/eq?H_%7Bout%7D%3D%5B%5Cfrac%7BW+2P-F_%7Bh%7D%7D%7BS%7D%5D+1

其中[x]表示向下取整。

实例:

  • 输入图像尺寸:5x5
  • 卷积核尺寸:3x3
  • 填充:1
  • 步长:1

输出尺寸:

https://latex.csdn.net/eq?W_%7Bout%7D%3D%5B%5Cfrac%7B5+2%5Ccdot%201-3%7D%7B1%7D%5D+1%3D5

https://latex.csdn.net/eq?H_%7Bout%7D%3D%5B%5Cfrac%7B5+2%5Ccdot%201-3%7D%7B1%7D%5D+1%3D5

所以输出特征图尺寸为5×5.

代码实例:

import torch
import torch.nn as nn

# 创建随机输入张量 (batch_size, channels, height, width)
input_tensor = torch.randn(1, 1, 5, 5)  # 1个样本,1个通道,5x5大小的图像

# 定义卷积层
conv_layer = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3, padding=1, stride=1)

# 执行卷积操作
output_tensor = conv_layer(input_tensor)

# 打印输出特征图的尺寸
print("Output tensor shape:", output_tensor.shape)

输出:

Output tensor shape: torch.Size([1, 1, 5, 5])

图来源: