2025年conv_m函数(convn函数)

conv_m函数(convn函数)pre import torch import torch nn as nn import torch nn functional as F import math import copy from einops import rearrange class SinusoidalPo nn Module def init self dim pre

大家好,我是讯享网,很高兴认识大家。



 <pre>import torch 

讯享网

import torch.nn as nn import torch.nn.functional as F import math import copy from einops import rearrange

class SinusoidalPosEmb(nn.Module):

讯享网def __init__(self, dim): super().__init__() self.dim &#61; dim def forward(self, x): device &#61; x.device half_dim &#61; self.dim // 2 emb &#61; math.log(10000) / (half_dim - 1) emb &#61; torch.exp(torch.arange(half_dim, device&#61;device) * -emb) emb &#61; x[:, None] * emb[None, :] emb &#61; torch.cat((emb.sin(), emb.cos()), dim&#61;-1) return emb 

class single_conv(nn.Module):

def __init__(self, in_ch, out_ch): super(single_conv, self).__init__() self.conv &#61; nn.Sequential( nn.Conv2d(in_ch, out_ch, 5, padding&#61;2), nn.ReLU(inplace&#61;True) ) def forward(self, x): return self.conv(x) 

class up(nn.Module):


讯享网

讯享网def __init__(self, in_ch): super(up, self).__init__() self.up &#61; nn.ConvTranspose2d(in_ch, in_ch // 2, 2, stride&#61;2) self.conv_concat &#61; nn.Conv2d(in_ch, in_ch // 2, 3, 1, 1) self.relu &#61; nn.ReLU(inplace&#61;True) def forward(self, x1, x2): x1 &#61; self.up(x1) # input is CHW diffY &#61; x2.size()[2] - x1.size()[2] diffX &#61; x2.size()[3] - x1.size()[3] x1 &#61; F.pad(x1, (diffX // 2, diffX - diffX // 2, diffY // 2, diffY - diffY // 2)) x &#61; torch.cat((x1, x2), dim&#61;1) x &#61; self.conv_concat(x) x &#61; self.relu(x) return x 

diff

class EMA(nn.Module):

def __init__(self, channels, c2&#61;None, factor&#61;32): super(EMA, self).__init__() self.groups &#61; factor assert channels // self.groups &gt; 0 self.softmax &#61; nn.Softmax(-1) self.agp &#61; nn.AdaptiveAvgPool2d((1, 1)) self.pool_h &#61; nn.AdaptiveAvgPool2d((None, 1)) self.pool_w &#61; nn.AdaptiveAvgPool2d((1, None)) self.gn &#61; nn.GroupNorm(channels // self.groups, channels // self.groups) self.conv1x1 &#61; nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size&#61;5, stride&#61;1, padding&#61;2) self.conv3x3 &#61; nn.Conv2d(channels // self.groups, channels // self.groups, kernel_size&#61;5, stride&#61;1, padding&#61;2) def forward(self, x): b, c, h, w &#61; x.size() group_x &#61; x.reshape(b * self.groups, -1, h, w) # b*g,c//g,h,w x_h &#61; self.pool_h(group_x) x_w &#61; self.pool_w(group_x).permute(0, 1, 3, 2) hw &#61; self.conv1x1(torch.cat([x_h, x_w], dim&#61;2)) x_h, x_w &#61; torch.split(hw, [h, w], dim&#61;2) x1 &#61; self.gn(group_x * x_h.sigmoid() * x_w.permute(0, 1, 3, 2).sigmoid()) x2 &#61; self.conv3x3(group_x) x11 &#61; self.softmax(self.agp(x1).reshape(b * self.groups, -1, 1).permute(0, 2, 1)) x12 &#61; x2.reshape(b * self.groups, c // self.groups, -1) # b*g, c//g, hw x21 &#61; self.softmax(self.agp(x2).reshape(b * self.groups, -1, 1).permute(0, 2, 1)) x22 &#61; x1.reshape(b * self.groups, c // self.groups, -1) # b*g, c//g, hw weights &#61; (torch.matmul(x11, x12) &#43; torch.matmul(x21, x22)).reshape(b * self.groups, 1, h, w) return (group_x * weights.sigmoid()).reshape(b, c, h, w) 

diff

class down(nn.Module):

讯享网def __init__(self, in_channels, dilation_rates&#61;(1, 2, 3)): super(down, self).__init__() self.conv &#61; nn.ModuleList([ nn.Sequential( nn.Conv2d(in_channels, in_channels, kernel_size&#61;3, stride&#61;2, padding&#61;d, dilation&#61;d, bias&#61;False), nn.ReLU(inplace&#61;True) ) for d in dilation_rates ]) self.conv_2 &#61; nn.Conv2d(in_channels * 3, in_channels, 3, 1, 1) self.relu &#61; nn.ReLU(inplace&#61;True) def forward(self, x): outputs &#61; [conv(x) for conv in self.conv] out &#61; torch.cat(outputs, dim&#61;1) out &#61; self.conv_2(out) out &#61; self.relu(out) return out 

class outconv(nn.Module):

def __init__(self, in_ch, out_ch): super(outconv, self).__init__() self.conv &#61; nn.Conv2d(in_ch, out_ch, 1) def forward(self, x): x &#61; self.conv(x) return x 

class adjust_net(nn.Module):

讯享网def __init__(self, out_channels&#61;64, middle_channels&#61;32): super(adjust_net, self).__init__() self.model &#61; nn.Sequential( nn.Conv2d(2, middle_channels, 3, padding&#61;1), nn.ReLU(inplace&#61;True), nn.AvgPool2d(2), nn.Conv2d(middle_channels, middle_channels * 2, 3, padding&#61;1), nn.ReLU(inplace&#61;True), nn.AvgPool2d(2), nn.Conv2d(middle_channels * 2, middle_channels * 4, 3, padding&#61;1), nn.ReLU(inplace&#61;True), nn.AvgPool2d(2), nn.Conv2d(middle_channels * 4, out_channels * 2, 1, padding&#61;0) ) def forward(self, x): out &#61; self.model(x) out &#61; F.adaptive_avg_pool2d(out, (1, 1)) out1 &#61; out[:, :out.shape[1] // 2] out2 &#61; out[:, out.shape[1] // 2:] return out1, out2 

The architecture of U-Net refers to “Toward Convolutional Blind Denoising of Real Photographs”,

official MATLAB implementation: https://github.com/GuoShi28/CBDNet.

unofficial PyTorch implementation: https://github.com/IDKiro/CBDNet-pytorch/tree/master.

We improved it by adding time step embedding and EMM module, while removing the noise estimation network.

class UNet(nn.Module):

def __init__(self, in_channels&#61;2, out_channels&#61;1): super(UNet, self).__init__() dim &#61; 32 self.time_mlp &#61; nn.Sequential( SinusoidalPosEmb(dim), nn.Linear(dim, dim * 4), nn.GELU(), nn.Linear(dim * 4, dim) ) self.inc &#61; nn.Sequential( single_conv(in_channels, 64), single_conv(64, 64) ) self.down1 &#61; down(64) self.mlp1 &#61; nn.Sequential( nn.GELU(), nn.Linear(dim, 64) ) self.adjust1 &#61; adjust_net(64) self.conv1 &#61; nn.Sequential( single_conv(64, 128), single_conv(128, 128), single_conv(128, 128) ) self.down2 &#61; down(128) self.mlp2 &#61; nn.Sequential( nn.GELU(), nn.Linear(dim, 128) ) self.adjust2 &#61; adjust_net(128) self.conv2 &#61; nn.Sequential( single_conv(128, 256), single_conv(256, 256), single_conv(256, 256), single_conv(256, 256), single_conv(256, 256), single_conv(256, 256) ) self.up1 &#61; up(256) self.mlp3 &#61; nn.Sequential( nn.GELU(), nn.Linear(dim, 128) ) self.adjust3 &#61; adjust_net(128) self.conv3 &#61; nn.Sequential( single_conv(128, 128), single_conv(128, 128), single_conv(128, 128) ) self.up2 &#61; up(128) self.mlp4 &#61; nn.Sequential( nn.GELU(), nn.Linear(dim, 64) ) self.adjust4 &#61; adjust_net(64) self.conv4 &#61; nn.Sequential( single_conv(64, 64), single_conv(64, 64) ) self.outc &#61; outconv(64, out_channels) self.ema1 &#61; EMA(channels&#61;128) self.ema2 &#61; EMA(channels&#61;256) self.ema3 &#61; EMA(channels&#61;128) self.ema4 &#61; EMA(channels&#61;64) def forward(self, x, t, x_adjust, adjust): inx &#61; self.inc(x) time_emb &#61; self.time_mlp(t) down1 &#61; self.down1(inx) condition1 &#61; self.mlp1(time_emb) b, c &#61; condition1.shape condition1 &#61; rearrange(condition1, &#39;b c -&gt; b c 1 1&#39;) if adjust: gamma1, beta1 &#61; self.adjust1(x_adjust) down1 &#61; down1 &#43; gamma1 * condition1 &#43; beta1 else: down1 &#61; down1 &#43; condition1 conv1 &#61; self.conv1(down1) # diff: 通过EMA模块处理conv1的输出 conv1 &#61; self.ema1(conv1) down2 &#61; self.down2(conv1) condition2 &#61; self.mlp2(time_emb) b, c &#61; condition2.shape condition2 &#61; rearrange(condition2, &#39;b c -&gt; b c 1 1&#39;) if adjust: gamma2, beta2 &#61; self.adjust2(x_adjust) down2 &#61; down2 &#43; gamma2 * condition2 &#43; beta2 else: down2 &#61; down2 &#43; condition2 conv2 &#61; self.conv2(down2) # diff: 通过EMA模块处理conv2的输出 conv2 &#61; self.ema2(conv2) up1 &#61; self.up1(conv2, conv1) condition3 &#61; self.mlp3(time_emb) b, c &#61; condition3.shape condition3 &#61; rearrange(condition3, &#39;b c -&gt; b c 1 1&#39;) if adjust: gamma3, beta3 &#61; self.adjust3(x_adjust) up1 &#61; up1 &#43; gamma3 * condition3 &#43; beta3 else: up1 &#61; up1 &#43; condition3 conv3 &#61; self.conv3(up1) conv3 &#61; self.ema3(conv3) up2 &#61; self.up2(conv3, inx) condition4 &#61; self.mlp4(time_emb) b, c &#61; condition4.shape condition4 &#61; rearrange(condition4, &#39;b c -&gt; b c 1 1&#39;) if adjust: gamma4, beta4 &#61; self.adjust4(x_adjust) up2 &#61; up2 &#43; gamma4 * condition4 &#43; beta4 else: up2 &#61; up2 &#43; condition4 conv4 &#61; self.conv4(up2) conv4 &#61; self.ema4(conv4) out &#61; self.outc(conv4) return out 

class Network(nn.Module):

讯享网def __init__(self, in_channels&#61;3, out_channels&#61;1, context&#61;True): super(Network, self).__init__() self.unet &#61; UNet(in_channels&#61;in_channels, out_channels&#61;out_channels) self.context &#61; context def forward(self, x, t, y, x_end, adjust&#61;True): if self.context: x_middle &#61; x[:, 1].unsqueeze(1) else: x_middle &#61; x x_adjust &#61; torch.cat((y, x_end), dim&#61;1) out &#61; self.unet(x, t, x_adjust, adjust&#61;adjust) &#43; x_middle return out 

WeightNet of the one-shot learning framework

class WeightNet(nn.Module):

def __init__(self, weight_num&#61;10): super(WeightNet, self).__init__() init &#61; torch.ones([1, weight_num, 1, 1]) / weight_num self.weights &#61; nn.Parameter(init) def forward(self, x): weights &#61; F.softmax(self.weights, 1) out &#61; weights * x out &#61; out.sum(dim&#61;1, keepdim&#61;True) return out, weights 

Main function to test the model

def main():

讯享网# Set random seed for reproducibility torch.manual_seed(0) # Instantiate the model model &#61; Network(in_channels&#61;2, out_channels&#61;1, context&#61;True) # Set the model to evaluation mode (since we&#39;re debugging dimensions) model.eval() # Define input dimensions batch_size &#61; 2 num_channels &#61; 2 # Input channels for x height &#61; 64 width &#61; 64 # Create sample inputs x &#61; torch.randn(batch_size, num_channels, height, width) t &#61; torch.randint(0, 1000, (batch_size,)) y &#61; torch.randn(batch_size, 1, height, width) x_end &#61; torch.randn(batch_size, 1, height, width) # Pass the inputs through the model with torch.no_grad(): output &#61; model(x, t, y, x_end, adjust&#61;True) # Print the output shape print(f&#34;Final output shape: {output.shape}&#34;) 

if name == “main”:

main()</pre> 
小讯
上一篇 2025-05-24 15:23
下一篇 2025-04-28 18:06

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/172893.html