-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblocks.py
89 lines (66 loc) · 2.6 KB
/
blocks.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
import torch
import torch.nn as nn
class DoubleConvBlock(nn.Module):
"""double conv layers block"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.double_conv = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.double_conv(x)
class DownBlock(nn.Module):
"""Downscale block: maxpool -> double conv block"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
DoubleConvBlock(in_channels, out_channels)
)
def forward(self, x):
return self.maxpool_conv(x)
class BridgeDown(nn.Module):
"""Downscale bottleneck block: maxpool -> conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.maxpool_conv = nn.Sequential(
nn.MaxPool2d(2),
nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1),
nn.ReLU(inplace=True)
)
def forward(self, x):
return self.maxpool_conv(x)
class BridgeUP(nn.Module):
"""Downscale bottleneck block: conv -> transpose conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv_up = nn.Sequential(
nn.Conv2d(in_channels, in_channels, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)
)
def forward(self, x):
return self.conv_up(x)
class UpBlock(nn.Module):
"""Upscale block: double conv block -> transpose conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.conv = DoubleConvBlock(in_channels * 2, in_channels)
self.up = nn.ConvTranspose2d(in_channels, out_channels, kernel_size=2, stride=2)
def forward(self, x1, x2):
x = torch.cat([x2, x1], dim=1)
x = self.conv(x)
return torch.relu(self.up(x))
class OutputBlock(nn.Module):
"""Output block: double conv block -> output conv"""
def __init__(self, in_channels, out_channels):
super().__init__()
self.out_conv = nn.Sequential(
DoubleConvBlock(in_channels * 2, in_channels),
nn.Conv2d(in_channels, out_channels, kernel_size=1))
def forward(self, x1, x2):
x = torch.cat([x2, x1], dim=1)
return self.out_conv(x)