写一个基于self attention 的 unet模型程序
时间: 2023-04-10 14:02:56 浏览: 135
以下是一个基于self attention的unet模型的程序:
```
import torch
import torch.nn as nn
import torch.nn.functional as F
class SelfAttentionBlock(nn.Module):
def __init__(self, in_channels):
super(SelfAttentionBlock, self).__init__()
self.query_conv = nn.Conv2d(in_channels, in_channels // 8, kernel_size=1)
self.key_conv = nn.Conv2d(in_channels, in_channels // 8, kernel_size=1)
self.value_conv = nn.Conv2d(in_channels, in_channels, kernel_size=1)
self.gamma = nn.Parameter(torch.zeros(1))
def forward(self, x):
batch_size, C, H, W = x.size()
proj_query = self.query_conv(x).view(batch_size, -1, H * W).permute(0, 2, 1)
proj_key = self.key_conv(x).view(batch_size, -1, H * W)
energy = torch.bmm(proj_query, proj_key)
attention = F.softmax(energy, dim=-1)
proj_value = self.value_conv(x).view(batch_size, -1, H * W)
out = torch.bmm(proj_value, attention.permute(0, 2, 1))
out = out.view(batch_size, C, H, W)
out = self.gamma * out + x
return out
class DoubleConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(DoubleConv, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.conv(x)
return x
class Up(nn.Module):
def __init__(self, in_channels, out_channels, bilinear=True):
super(Up, self).__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
else:
self.up = nn.ConvTranspose2d(in_channels // 2, in_channels // 2, kernel_size=2, stride=2)
self.conv = DoubleConv(in_channels, out_channels)
def forward(self, x1, x2):
x1 = self.up(x1)
diffY = x2.size()[2] - x1.size()[2]
diffX = x2.size()[3] - x1.size()[3]
x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
diffY // 2, diffY - diffY // 2])
x = torch.cat([x2, x1], dim=1)
x = self.conv(x)
return x
class UNet(nn.Module):
def __init__(self, n_channels, n_classes, bilinear=True):
super(UNet, self).__init__()
self.n_channels = n_channels
self.n_classes = n_classes
self.bilinear = bilinear
self.inc = DoubleConv(n_channels, 64)
self.down1 = DoubleConv(64, 128)
self.down2 = DoubleConv(128, 256)
self.down3 = DoubleConv(256, 512)
self.down4 = DoubleConv(512, 1024)
self.up1 = Up(1024, 512, bilinear)
self.up2 = Up(512, 256, bilinear)
self.up3 = Up(256, 128, bilinear)
self.up4 = Up(128, 64, bilinear)
self.outc = nn.Conv2d(64, n_classes, kernel_size=1)
self.sa1 = SelfAttentionBlock(64)
self.sa2 = SelfAttentionBlock(128)
self.sa3 = SelfAttentionBlock(256)
self.sa4 = SelfAttentionBlock(512)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x2 = self.sa1(x2)
x3 = self.down2(x2)
x3 = self.sa2(x3)
x4 = self.down3(x3)
x4 = self.sa3(x4)
x5 = self.down4(x4)
x5 = self.sa4(x5)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
x = self.outc(x)
return x
```
希望这个程序能够帮助你!
阅读全文