Pytorch复习笔记--nn.Conv2d()和nn.Conv3d()的计算公式

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(),其计算公式如下:

\small H_{out}=[\frac{H_{in}+2\times padding[0]-dilation[0]\times(kernelSize[0]-1)-1 }{stride[0]}+1]

\small W_{out}=[\frac{W_{in}+2\times padding[1]-dilation[1]\times(kernelSize[1]-1)-1 }{stride[1]}+1]

        对于 Pytorch 提供的三维卷积 nn.Conv3d(),其计算公式如下:

\small T_{out}=[\frac{T_{in}+2\times padding[0]-dilation[0]\times(kernelSize[0]-1)-1 }{stride[0]}+1]

\small H_{out}=[\frac{H_{in}+2\times padding[1]-dilation[1]\times(kernelSize[1]-1)-1 }{stride[1]}+1]

\small W_{out}=[\frac{W_{in}+2\times padding[2]-dilation[2]\times(kernelSize[2]-1)-1 }{stride[2]}+1]

猜你喜欢

转载自blog.csdn.net/weixin_43863869/article/details/129784028