【深度学习】特征图的上采样(nn.Upsample)和转置卷积(nn.ConvTranspose2d) | pytorch

慈云数据 8个月前 (03-13) 技术支持 49 0

文章目录

  • 前言
  • 一、nn.Upsample 上采样
  • 二、nn.ConvTranspose2d 转置卷积

    前言

    这次就不废话了,我想赶在10点前回去洗头(现在9.17,还差一篇文章)

    一、nn.Upsample 上采样

    该函数有四个参数:

    在这里插入图片描述

    参数的介绍如下:

    在这里插入图片描述

    稍微翻译一下:

    参数:

    1)size(int或Tuple[int]或Tuple[int,int]或Tupple[int,int,int],可选):输出空间大小

    2)scale_factor(float或Tuple[floot]或Tuple[floot,float]或Tuple[floot、float、float],可选):空间大小的乘数。如果是元组,则必须匹配输入大小。

    3)mode(str,可选):上采样算法:“最近”,“线性”、“双线性”、“双三次”和“三线性”。默认值 ‘nearst’

    4)align_ccorners(bool,可选):如果“True”,则输入的角像素和输出张量对齐,从而保持这些像素。仅当:attr:mode为“线性”、“线性”或“三线性”。默认值:False

    深度学习里用的比较多的是二倍向上采样,比如经常出现在U-net网络结构里,例子如下:

    import torch
    import torch.nn as nn
    A =torch.rand(1,3,24,24)
    up=nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
    B =up(A)
    print(B.shape)
    

    在这里插入图片描述

    如果少写了一个维度,就会报错

    import torch
    import torch.nn as nn
    A =torch.rand(3,24,24)
    up=nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
    B =up(A)
    print(B.shape)
    

    在这里插入图片描述

    二、nn.ConvTranspose2d 转置卷积

    我们首先看看它的参数,依然如此得多

    在这里插入图片描述

    其实,转置卷积和普通的卷积操作参数配置都是很相似的。看几个例子把

    1)311型转置卷积:

    import torch
    import torch.nn as nn
    A =torch.rand(1,64,24,24)
    up=nn.ConvTranspose2d(64,32,3,1,1)
    B =up(A)
    print(B.shape)
    

    在这里插入图片描述

    和普通卷积的311一样,宽高不变

    2)310型转置卷积:

    import torch
    import torch.nn as nn
    A =torch.rand(1,64,24,24)
    up=nn.ConvTranspose2d(64,32,3,1,0)
    B =up(A)
    print(B.shape)
    

    在这里插入图片描述

    和普通卷积类似,这里是+2(普通卷积是宽高-2)

    
                    
                    
                    
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon