-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
104 lines (85 loc) · 3.33 KB
/
Copy pathmodel.py
File metadata and controls
104 lines (85 loc) · 3.33 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
import torch
from torch import nn
import torch.nn.functional as F
from torch import cat, rand
def conv_block(in_channels, out_channels):
return nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
def pixel_shuffle_upsample(in_channels, out_channels):
mid_channels = 4 * out_channels
return nn.Sequential(
nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
nn.BatchNorm2d(mid_channels),
nn.ReLU(inplace=True),
nn.PixelShuffle(upscale_factor=2),
nn.BatchNorm2d(out_channels),
nn.ReLU(inplace=True)
)
class ResBlock(nn.Module):
def __init__(self, in_channel, out_channel, stride=1):
super(ResBlock, self).__init__()
self.left = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=3, stride=stride, padding=1, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU(inplace=True),
nn.Conv2d(out_channel, out_channel, kernel_size=3, stride=1, padding=1, bias=False),
nn.BatchNorm2d(out_channel)
)
self.shortcut = nn.Sequential()
if stride != 1 or in_channel != out_channel:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channel, out_channel, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_channel)
)
def forward(self, x):
out = self.left(x)
out = out + self.shortcut(x)
out = F.relu(out)
return out
class RUNet(nn.Module):
def __init__(self, input_channels=1, output_channels=1):
super(RUNet, self).__init__()
self.in_channel = 64
self.enc1 = conv_block(input_channels, 64)
self.res_enc2 = self.make_layer(ResBlock, 128, 1, stride=2)
self.res_enc3 = self.make_layer(ResBlock, 256, 2, stride=2)
self.res_enc4 = self.make_layer(ResBlock, 512, 5, stride=2)
self.up3 = pixel_shuffle_upsample(512, 512)
self.up2 = pixel_shuffle_upsample(512 + 256, 256)
self.up1 = pixel_shuffle_upsample(256 + 128, 256)
self.out_conv = nn.Conv2d(256 + 64, output_channels, kernel_size=3, padding=1)
def __str__(self):
return 'RUNet'
def make_layer(self, block, channels, num_blocks, stride):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_channel, channels, stride))
self.in_channel = channels
return nn.Sequential(*layers)
def forward(self, x):
enc1 = self.enc1(x)
enc2 = self.res_enc2(enc1)
enc3 = self.res_enc3(enc2)
enc_mid = self.res_enc4(enc3)
dec3 = self.up3(enc_mid)
dec3 = cat((dec3, enc3), dim=1)
dec2 = self.up2(dec3)
dec2 = cat((dec2, enc2), dim=1)
dec1 = self.up1(dec2)
dec1 = cat((dec1, enc1), dim=1)
out = self.out_conv(dec1)
return out, enc_mid
if __name__ == '__main__':
test_sample = rand(2, 1, 36, 68)
model = RUNet()
out, mid = model(test_sample)
print("Output shape:", out.shape)
print("Mid feature shape:", mid.shape)