1--基本知识
nn.Conv2d( ) 和 nn.Conv3d() 分别表示二维卷积和三维卷积;二维卷积常用于处理单帧图片来提取高维特征;三维卷积则常用于处理视频,从多帧图像中提取高维特征;
三维卷积可追溯于论文3D Convolutional Neural Networks for Human Action Recognition;
三维卷积使用三维卷积核,在 T、H 和 W 三个维度进行移动,以提取时间特征和空间特征,一个简单示意图如下:
2--基本用法
import torch
import torch.nn as nn
if __name__ == "__main__":
B = 8
C = 3
T = 10
H = 255
W = 255
input1 = torch.rand(B, C, H, W)
Conv2D = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=[3, 3], stride=1, padding=1)
output1 = Conv2D(input1)
print("input1.shape: ", input1.shape)
print("output1.shape: ", output1.shape)
input2 = torch.rand(B, C, T, H, W)
Conv3D = nn.Conv3d(in_channels=3, out_channels=64, kernel_size=[3, 3, 3], stride=1, padding=1)
output2 = Conv3D(input2)
print("input2.shape: ", input2.shape)
print("output2.shape: ", output2.shape)
3--计算公式
对于 Pytorch 提供的二维卷积 nn.Conv2d(),其计算公式如下:
对于 Pytorch 提供的三维卷积 nn.Conv3d(),其计算公式如下: