-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUNet.py
More file actions
74 lines (60 loc) · 2.48 KB
/
UNet.py
File metadata and controls
74 lines (60 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import torch.nn as nn
import torch.nn.functional as F
import torch
import torchvision
class Block(nn.Module):
def __init__(self, in_ch, out_ch):
super().__init__()
self.conv1 = nn.Conv2d(in_ch, out_ch, 3)
self.relu = nn.ReLU()
self.conv2 = nn.Conv2d(out_ch, out_ch, 3)
self.t_encoder = nn.Linear(1, out_ch)
def forward(self, x, t):
t_emb = self.t_encoder(t)[:, :, None, None]
return self.relu(self.conv2(self.relu(self.conv1(x)) + t_emb))
class Encoder(nn.Module):
def __init__(self, chs=(3,64,128,256,512,1024)):
super().__init__()
self.enc_blocks = nn.ModuleList([Block(chs[i], chs[i+1]) for i in range(len(chs)-1)])
self.pool = nn.MaxPool2d(2)
def forward(self, x, t):
ftrs = []
for block in self.enc_blocks:
x = block(x, t)
ftrs.append(x)
x = self.pool(x)
return ftrs
class Decoder(nn.Module):
def __init__(self, chs=(1024, 512, 256, 128, 64)):
super().__init__()
self.chs = chs
self.upconvs = nn.ModuleList([nn.ConvTranspose2d(chs[i], chs[i+1], 2, 2) for i in range(len(chs)-1)])
self.dec_blocks = nn.ModuleList([Block(chs[i], chs[i+1]) for i in range(len(chs)-1)])
def forward(self, x, encoder_features, t):
for i in range(len(self.chs)-1):
x = self.upconvs[i](x)
enc_ftrs = self.crop(encoder_features[i], x)
x = torch.cat([x, enc_ftrs], dim=1)
x = self.dec_blocks[i](x, t)
return x
def crop(self, enc_ftrs, x):
_, _, H, W = x.shape
enc_ftrs = torchvision.transforms.CenterCrop([H, W])(enc_ftrs)
return enc_ftrs
class UNet(nn.Module):
def __init__(self, enc_chs=(3,64,128,256,512,1024), dec_chs=(1024, 512, 256, 128, 64), num_class=1, retain_dim=False, out_sz=(572,572)):
super().__init__()
self.encoder = Encoder(enc_chs)
self.decoder = Decoder(dec_chs)
self.head = nn.Conv2d(dec_chs[-1], num_class, 1)
self.retain_dim = retain_dim
self.out_sz = out_sz
self.sigmoid = nn.Sigmoid()
def forward(self, x, t):
enc_ftrs = self.encoder(x, t)
out = self.decoder(enc_ftrs[::-1][0], enc_ftrs[::-1][1:], t)
out = self.head(out)
out = self.sigmoid(out)
if self.retain_dim:
out = F.interpolate(out, self.out_sz)
return out