DWT+pixelshuffle+注意力机制+1x1卷积

本文详细介绍了离散小波变换(DWT)和逆离散小波变换(IDWT)在图像处理中的应用,通过实验展示DWT+IDWT的可逆性。接着探讨了Pixshuffle在下采样和上采样中的作用。同时,文章还讨论了注意力机制如何利用通道和空间关系增强特征表示。最后,阐述了1x1卷积在图像处理中的优势,如便捷的维度调整和在移动设备上的高效应用。
摘要由CSDN通过智能技术生成

一、离散小波变换(DWT+IDWT)

1、实验过程

为了更好的学习下采样DWT和上采样IDWT的过程,我们构建一个简单的网络模型,将一张图片先进行下采样,然后再进行上采样操作,最后我们比较输入和输出图片的是否完全一样?PSNR值!

在这里插入图片描述
所以这个操作是可逆的!一张图片先经过下采样然后再进行上采样,是可以完全恢复的!
在这里插入图片描述
在这里插入图片描述

理解这个F.conv_transpose2d函数

import torch

inputs = torch.randint(1,10, (1, 4, 1, 1))
print(inputs)
print(inputs.shape)
weights = torch.randint(1,10,(4, 1, 2, 2))
print(weights)
print(weights.shape)
y = F.conv_transpose2d(inputs, weights, groups=1, stride=2)
print(y)
print(y.shape)


inputs = torch.randint(1,10, (1, 4, 1, 1))
print(inputs)
print(inputs.shape)
weights = torch.randint(1,10,(1, 4, 2, 2))
print(weights)
print(weights.shape)
y1=F.conv2d(inputs, weights, padding=1,stride=1)
print(y1)
print(y1.shape)

2、代码

# -*- coding: utf-8 -*-

import numpy as np
import torch.nn.init as init
import torch.nn.functional as F
import torch
from torch import nn
import cv2

#离散小波变换  下采样操作
class DWTForward(nn.Module):
    def __init__(self):
        super(DWTForward, self).__init__()

        #ll lh hl hh shape(2,2)
        ll = np.array([[0.5, 0.5], [0.5, 0.5]])
        lh = np.array([[-0.5, -0.5], [0.5, 0.5]])
        hl = np.array([[-0.5, 0.5], [-0.5, 0.5]])
        hh = np.array([[0.5, -0.5], [-0.5, 0.5]])

        #filts shape(4,1,2,2)
        filts = np.stack([ll[None,::-1,::-1], lh[None,::-1,::-1],
                          hl[None,::-1,::-1], hh[None,::-1,::-1]],
                         axis=0)

        self.weight = nn.Parameter(
            torch.tensor(filts).to(torch.get_default_dtype()),
            requires_grad=False)

    def forward(self, x):
        print("输入灰度图的形状:", x.shape)  #torch.Size([1, 1, 512, 512])
        C = x.shape[1]  ##通道数

        print("生成下采样参数的形状:",self.weight.shape)  # torch.Size([4, 1, 2, 2])
        #如果通道数是C,那们filters:torch.Size([4C, 1, 2, 2])

        filters = torch.cat([self.weight,] * C, dim=0)
        print("生成下采样卷积核的形状:", filters.shape)  # torch.Size([4, 1, 2, 2])
        ##其实这里进行分组卷积操作  由定义的卷积进行下采样操作
        #输入X:(1,1,512,512)  filters:(4,1,2,2) group=1,stride=2  padding = 1
        #输出Y:(1,4,256,256)
        y = F.conv2d(x, filters, groups=
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值