zoukankan      html  css  js  c++  java
  • Pytorch学习笔记15----nn.Conv2d与Conv3d参数理解、单通道与多通道卷积理解

    1.Conv3d

    class torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

    Parameters:

    • in_channels(int) – 输入信号的通道
    • out_channels(int) – 卷积产生的通道
    • kernel_size(int or tuple) - 卷积核的尺寸
    • stride(int or tupleoptional) - 卷积步长
    • padding(int or tupleoptional) - 输入的每一条边补充0的层数
    • dilation(int or tupleoptional) – 卷积核元素之间的间距
    • groups(intoptional) – 从输入通道到输出通道的阻塞连接数
    • bias(booloptional) - 如果bias=True,添加偏置

    三维卷积层, 输入的尺度是(N, C_in,D,H,W),输出尺度(N,C_out,D_out,H_out,W_out)

    shape:
    input: (N,C_in,D_in,H_in,W_in)
    output: (N,C_out,D_out,H_out,W_out)

    注意:3D卷积的输入是5维的tensor

    官网案例:

    >>> # With square kernels and equal stride
    >>> m = nn.Conv3d(16, 33, 3, stride=2)
    >>> # non-square kernels and unequal stride and with padding
    >>> m = nn.Conv3d(16, 33, (3, 5, 2), stride=(2, 1, 1), padding=(4, 2, 0))
    >>> input = autograd.Variable(torch.randn(20, 16, 10, 50, 100))
    >>> output = m(input)

    2.nn.Conv2d

    nn.Conv2d(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True))

    参数:
      in_channel: 输入数据的通道数,例RGB图片通道数为3;
      out_channel: 输出数据的通道数,这个根据模型调整;
      kennel_size: 卷积核大小,可以是int,或tuple;kennel_size=2,意味着卷积大小(2,2), kennel_size=(2,3),意味着卷积大小(2,3)即非正方形卷积
      stride:步长,默认为1,与kennel_size类似,stride=2,意味着步长上下左右扫描皆为2, stride=(2,3),左右扫描步长为2,上下为3;
      padding: 零填充

    案例:

    import torch
    import torch.nn as nn
    
    x = torch.randn(10, 16, 30, 32) # batch, channel , height , width
    print(x.shape)
    m = nn.Conv2d(16, 33, (3, 2), (2,1))  # in_channel, out_channel ,kennel_size,stride
    print(m)
    y = m(x)
    print(y.shape)

    控制台输出:

    卷积计算过程:
    h/w = (h/w - kennel_size + 2padding) / stride + 1
    x = ([10,16,30,32]),其中h=30,w=32,对于卷积核长分别是 h:3,w:2 ;对于步长分别是h:2,w:1;padding默认0;
    h = (30 - 3 + 2*
    0)/ 2 +1 = 27/2 +1 = 13+1 =14

    w =(32 - 2 + 2*0)/ 1 +1 = 30/1 +1 = 30+1 =31
    batch = 10, out_channel = 33
    故: y= ([10, 33, 14, 31])

    3.单通道与多通道卷积

    (1)单通道卷积核卷积过程:

    32个卷积核,可以学习32种特征。在有多个卷积核时,输出就为32个feature map

    conv2d( in_channels = 1 , out_channels = N)

    有N个filter对输入进行滤波。同时输出N个结果即feature map,每个filter滤波输出一个结果.

    (2)多通道卷积

    conv2d( in_channels = X(x>1) , out_channels = N)

    有N乘X个filter(N组filters,每组X 个)对输入进行滤波。即每次有一组里X个filter对原X个channels分别进行滤波最后相加输出一个结果,最后输出N个结果即feature map。

    参考文献:

    https://blog.csdn.net/qq_26369907/article/details/88366147

    https://www.pytorchtutorial.com/docs

    https://zhuanlan.zhihu.com/p/32190799

  • 相关阅读:
    asp.net mvc中ViewData、ViewBag和TempData的详解
    在asp.net WebForms中使用路由Route
    Sql Server批量删除指定表
    MongoDB安装并设置为windows服务以使其开机自启
    NPOI操作excel之写入数据到excel表
    NPOI操作excel之读取excel数据
    SQL Server 定时自动备份数据库
    如何用按钮的click事件去触发a标签的click事件
    C# Asp.net Quartz.NET作业调度之创建、安装、卸载、调试windows服务的简单事例
    c#中浅拷贝和深拷贝的理解
  • 原文地址:https://www.cnblogs.com/luckyplj/p/13467823.html
Copyright © 2011-2022 走看看