Add model architecture
Browse files
model.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
|
| 5 |
+
class DoubleConv(nn.Module):
|
| 6 |
+
def __init__(self, in_channels, out_channels, dropout=0.1):
|
| 7 |
+
super().__init__()
|
| 8 |
+
self.conv = nn.Sequential(
|
| 9 |
+
nn.Conv2d(in_channels, out_channels, 3, 1, 1, bias=True),
|
| 10 |
+
nn.BatchNorm2d(out_channels),
|
| 11 |
+
nn.ReLU(inplace=True),
|
| 12 |
+
nn.Dropout2d(dropout),
|
| 13 |
+
nn.Conv2d(out_channels, out_channels, 3, 1, 1, bias=True),
|
| 14 |
+
nn.BatchNorm2d(out_channels),
|
| 15 |
+
nn.ReLU(inplace=True),
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def forward(self, x):
|
| 19 |
+
return self.conv(x)
|
| 20 |
+
|
| 21 |
+
class AttentionGate(nn.Module):
|
| 22 |
+
def __init__(self, F_g, F_l, F_int):
|
| 23 |
+
super().__init__()
|
| 24 |
+
self.W_g = nn.Conv2d(F_g, F_int, kernel_size=1, stride=1, padding=0, bias=True)
|
| 25 |
+
self.W_x = nn.Conv2d(F_l, F_int, kernel_size=1, stride=1, padding=0, bias=True)
|
| 26 |
+
self.psi = nn.Sequential(
|
| 27 |
+
nn.Conv2d(F_int, 1, kernel_size=1, stride=1, padding=0, bias=True),
|
| 28 |
+
nn.Sigmoid()
|
| 29 |
+
)
|
| 30 |
+
self.relu = nn.ReLU(inplace=True)
|
| 31 |
+
|
| 32 |
+
def forward(self, g, x):
|
| 33 |
+
g1 = self.W_g(g)
|
| 34 |
+
x1 = self.W_x(x)
|
| 35 |
+
psi = self.relu(g1 + x1)
|
| 36 |
+
psi = self.psi(psi)
|
| 37 |
+
return x * psi
|
| 38 |
+
|
| 39 |
+
class AttentionUNet(nn.Module):
|
| 40 |
+
def __init__(self, img_ch=1, output_ch=4):
|
| 41 |
+
super().__init__()
|
| 42 |
+
self.Maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
|
| 43 |
+
|
| 44 |
+
self.downs = nn.ModuleList([
|
| 45 |
+
DoubleConv(img_ch, 64),
|
| 46 |
+
DoubleConv(64, 128),
|
| 47 |
+
DoubleConv(128, 256),
|
| 48 |
+
DoubleConv(256, 512)
|
| 49 |
+
])
|
| 50 |
+
|
| 51 |
+
self.bottleneck = DoubleConv(512, 1024)
|
| 52 |
+
|
| 53 |
+
self.ups = nn.ModuleList([
|
| 54 |
+
nn.ConvTranspose2d(1024, 512, kernel_size=2, stride=2),
|
| 55 |
+
nn.ConvTranspose2d(512, 256, kernel_size=2, stride=2),
|
| 56 |
+
nn.ConvTranspose2d(256, 128, kernel_size=2, stride=2),
|
| 57 |
+
nn.ConvTranspose2d(128, 64, kernel_size=2, stride=2)
|
| 58 |
+
])
|
| 59 |
+
|
| 60 |
+
self.attention_gates = nn.ModuleList([
|
| 61 |
+
AttentionGate(F_g=512, F_l=512, F_int=256),
|
| 62 |
+
AttentionGate(F_g=256, F_l=256, F_int=128),
|
| 63 |
+
AttentionGate(F_g=128, F_l=128, F_int=64),
|
| 64 |
+
AttentionGate(F_g=64, F_l=64, F_int=32)
|
| 65 |
+
])
|
| 66 |
+
|
| 67 |
+
self.up_convs = nn.ModuleList([
|
| 68 |
+
DoubleConv(1024, 512),
|
| 69 |
+
DoubleConv(512, 256),
|
| 70 |
+
DoubleConv(256, 128),
|
| 71 |
+
DoubleConv(128, 64)
|
| 72 |
+
])
|
| 73 |
+
|
| 74 |
+
self.final_conv = nn.Conv2d(64, output_ch, kernel_size=1, stride=1, padding=0)
|
| 75 |
+
|
| 76 |
+
def forward(self, x):
|
| 77 |
+
e1 = self.downs[0](x)
|
| 78 |
+
e2 = self.downs[1](self.Maxpool(e1))
|
| 79 |
+
e3 = self.downs[2](self.Maxpool(e2))
|
| 80 |
+
e4 = self.downs[3](self.Maxpool(e3))
|
| 81 |
+
|
| 82 |
+
b = self.bottleneck(self.Maxpool(e4))
|
| 83 |
+
|
| 84 |
+
d4 = self.ups[0](b)
|
| 85 |
+
x4 = self.attention_gates[0](g=d4, x=e4)
|
| 86 |
+
d4 = self.up_convs[0](torch.cat((x4, d4), dim=1))
|
| 87 |
+
|
| 88 |
+
d3 = self.ups[1](d4)
|
| 89 |
+
x3 = self.attention_gates[1](g=d3, x=e3)
|
| 90 |
+
d3 = self.up_convs[1](torch.cat((x3, d3), dim=1))
|
| 91 |
+
|
| 92 |
+
d2 = self.ups[2](d3)
|
| 93 |
+
x2 = self.attention_gates[2](g=d2, x=e2)
|
| 94 |
+
d2 = self.up_convs[2](torch.cat((x2, d2), dim=1))
|
| 95 |
+
|
| 96 |
+
d1 = self.ups[3](d2)
|
| 97 |
+
x1 = self.attention_gates[3](g=d1, x=e1)
|
| 98 |
+
d1 = self.up_convs[3](torch.cat((x1, d1), dim=1))
|
| 99 |
+
|
| 100 |
+
return self.final_conv(d1)
|