PixelShuffle 实现模型上下采样

PixelShuffle 和 UnpixelShuffle 减少尺寸的同时也会改变通道数

1 32 128 128 -- UnpixelShuffle(2)-> 1 128 64 64

【torch.nn.PixelShuffle】和 【torch.nn.UnpixelShuffle】-优快云博客

效果还行 可惜硬件不支持

        if use_PixelShuffle:
            print('use_PixelShuffle')
        ##############################  PixelShuffle  ####################################
            self.down2 = nn.Sequential(
                nn.PixelUnshuffle(2) , #1 32 128 128 -> 1 128 64 64
                nn.Conv2d(feat_top*4, feat_mid, 1, stride=1, padding=0, bias=bias)
            )
            self.down4 = nn.Sequential(
                nn.PixelUnshuffle(2) ,
                nn.Conv2d(feat_mid*4, feat_bot, 1, stride=1, padding=0, bias=bias)
            )
            self.up2 = nn.Sequential(nn.Conv2d(feat_bot, feat_mid*4, 1, stride=1, padding=0, bias=bias),
                                      nn.PixelShuffle(2))
            self.up4 = nn.Sequential(nn.Conv2d(feat_mid, feat_top*4, 1, stride=1, padding=0, bias=bias),
                                      nn.PixelShuffle(2))
        #################################################################################
### 卷积神经网络中上采样和下采样的概念及实现方法 #### 一、定义与区别 在卷积神经网络(CNN)中,**下采样**通常用于减少数据的空间尺寸,从而降低计算复杂度并提取更高级别的特征。而**上采样**则相反,它旨在恢复空间分辨率,常用于图像分割或生成模型的任务中。 - **下采样**可以通过池化操作完成,例如最大值池化或平均值池化[^3]。这种操作会显著减小输入张量的大小,同时保留重要信息。 - **上采样**则是为了增加特征图的空间维度,常见的方法包括插值法(如双线性插值)、转置卷积以及像素混洗(Pixel Shuffle)等[^2]。 值得注意的是,在某些情况下,“反卷积”这一术语被误认为是指代上采样的具体方式之一——即转置卷积加微步卷积的操作集合[^1]。然而严格意义上讲,它们并不完全相同;实际上,反卷积只是广义上的空洞卷积的一部分。 #### 二、具体的实现方法 ##### (1)下采样实现方法 最常用的两种形式分别是基于pooling的技术和其他类型的strided convolution: - Pooling Layers (e.g., MaxPooling, AvgPooling): 这些层通过对局部区域内的数值取极值或者均值得到新的较小规模表示; - Stride Convolution: 设置较大的stride参数使得每次滑动窗口移动更多位置,进而达到压缩效果的同时还能学习滤波器权重. ##### (2)上采样实现方法 对于需要放大图片的情况,则可以考虑以下几种策略: - Interpolation Methods: 如最近邻插值(Nearest Neighbor), 双三次插值(Bicubic) 或者双线性插值(Bilinear). 它们不涉及任何训练过程而是单纯依赖数学公式来进行估计; - Transposed Convolutions: 此种做法类似于常规前向传播过程中应用的标准convolutions但方向反转过来,并允许自适应调整输出形状以便匹配目标尺度需求 ; - Pixel Shuffling Technique combined with small kernels like `1×1`: After applying this technique which rearranges elements within tensor without changing its total number of entries ,a subsequent application of shallow convolutions could further enrich representational capacity while maintaining increased resolution levels.[^2] #### 示例代码展示 以下是利用PyTorch框架分别执行简单版本上下采样的Python脚本实例: ```python import torch.nn as nn # Define Down-sampling using max pooling layer downsample_layer = nn.MaxPool2d(kernel_size=2) # Example input Tensor shape [batch_size, channels, height, width] input_tensor = torch.randn(1, 3, 64, 64) output_downsampled = downsample_layer(input_tensor) # For Up-Sampling via bilinear interpolation method upsample_bilinear = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True) output_upsampledBilin = upsample_bilinear(output_downsampled) # Alternatively use transposed convolutional layers for Upscaling purposes. up_conv_transpose = nn.ConvTranspose2d(in_channels=3,out_channels=3,kernel_size=4,stride=2,padding=1,bias=False) output_upsampledTranspConv = up_conv_transpose(output_downsampled) ``` 上述代码片段展示了如何创建不同种类的采样模块及其调用流程。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值