This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSweatyNet1.py
141 lines (104 loc) · 5.42 KB
/
SweatyNet1.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import torch
import torch.nn as nn
from torch.nn.functional import interpolate
class SweatyNet1(nn.Module):
def __init__(self):
print("Model initialized")
super(SweatyNet1, self).__init__()
# -- Encoder --
# ConvBlock1
self.conv_block1 = nn.Sequential(
nn.Conv2d(3, 8, 3, stride=1, padding=1), # N x 3 x H x W --> N x 8 x H x W
nn.BatchNorm2d(8),
nn.ReLU()
)
# ConvBlock2
self.conv_block2 = nn.Sequential(
nn.Conv2d(8, 16, 3, stride=1, padding=1), # N x 8 x (H/2) x (W/2) --> N x 16 x (H/2) x (H/2)
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16, 16, 3, stride=1, padding=1), # N x 16 x (H/2) x (W/2) --> N x 16 x (H/2) x (H/2)
nn.BatchNorm2d(16),
nn.ReLU(),
)
# ConvBlock3
self.conv_block3 = nn.Sequential(
nn.Conv2d(24, 32, 3, stride=1, padding=1), # N x 24 x (H/4) x (W/4) --> N x 32 x (H/4) x (W/4)
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 32, 3, stride=1, padding=1), # N x 32 x (H/4) x (W/4) --> N x 32 x (H/4) x (W/4)
nn.BatchNorm2d(32),
nn.ReLU(),
)
# ConvBlock4
self.conv_block4 = nn.Sequential(
nn.Conv2d(56, 64, 3, stride=1, padding=1), # N x 56 x (H/8) x (W/8) --> N x 64 x (H/8) x (W/8)
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, stride=1, padding=1), # N x 64 x (H/8) x (W/8) --> N x 64 x (H/8) x (W/8)
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, 3, stride=1, padding=1), # N x 64 x (H/8) x (W/8) --> N x 64 x (H/8) x (W/8)
nn.BatchNorm2d(64),
nn.ReLU(),
)
# ConvBlock5
self.conv_block5 = nn.Sequential(
nn.Conv2d(120, 128, 3, stride=1, padding=1), # N x 120 x (H/16) x (W/16) --> N x 128 x (H/16) x (W/16)
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, 3, stride=1, padding=1), # N x 128 x (H/16) x (W/16) --> N x 128 x (H/16) x (W/16)
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, 3, stride=1, padding=1), # N x 128 x (H/16) x (W/16) --> N x 128 x (H/16) x (W/16)
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 64, 3, stride=1, padding=1), # N x 128 x (H/16) x (W/16) --> N x 64 x (H/16) x (W/16)
nn.BatchNorm2d(64),
nn.ReLU()
)
# -- Decoder --
self.conv_block6 = nn.Sequential(
nn.Conv2d(184, 64, 1, stride=1, padding=0), # N x 184 x (H/8) x (W/8) --> N x 64 x (H/8) x (W/8)
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 32, 3, stride=1, padding=1), # N x 64 x (H/8) x (W/8) --> N x 32 x (H/8) x (W/8)
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 32, 3, stride=1, padding=1), # N x 32 x (H/8) x (W/8) --> N x 32 x (H/8) x (W/8)
nn.BatchNorm2d(32),
nn.ReLU(),
)
self.conv_block7 = nn.Sequential(
nn.Conv2d(88, 16, 1, stride=1, padding=0), # N x 88 x (H/4) x (W/4) --> N x 16 x (H/4) x (W/4)
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16, 16, 3, stride=1, padding=1), # N x 16 x (H/4) x (W/4) --> N x 16 x (H/4) x (W/4)
nn.BatchNorm2d(16),
nn.ReLU(),
nn.Conv2d(16, 1, 3, stride=1, padding=1), # N x 16 x (H/4) x (W/4) --> N x 1 x (H/4) x (W/4)
nn.BatchNorm2d(1),
nn.ReLU()
)
def forward(self, x):
# -- Encode --
block1_out = self.conv_block1(x) # N x 3 x H x W --> N x 8 x H x W
mp1_out = nn.MaxPool2d(2, 2)(block1_out) # N x 8 x H x W --> N x 8 x (H/2) x (W/2)
block2_out = self.conv_block2(mp1_out) # N x 8 x (H/2) x (W/2) --> N x 16 x (H/2) x (W/2)
concat1 = torch.cat([mp1_out, block2_out], 1) # N x 24 x (H/2) x (W/2)
mp2_out = nn.MaxPool2d(2, 2)(concat1) # N x 24 x (H/2) x (W/2) --> N x (8 + 16) x (H/4) x (W/4)
block3_out = self.conv_block3(mp2_out) # N x 24 x (H/4) x (H/4) --> N x 32 x (H/4) x (H/4)
concat2 = torch.cat([mp2_out, block3_out], 1) # N x (24 + 32) x (H/4) x (W/4)
mp3_out = nn.MaxPool2d(2, 2)(concat2) # N x 56 x (H/4) x (W/4) --> N x 56 x (H/8) x (W/8)
block4_out = self.conv_block4(mp3_out) # N x 56 x (H/8) x (W/8) --> N x 64 x (H/8) x (W/8)
concat3 = torch.cat([mp3_out, block4_out], 1) # N x (56 + 64) x (H/8) x (W/8)
mp4_out = nn.MaxPool2d(2, 2)(concat3) # N x 120 x (H/8) x (W/8) --> N x 120 x (H/16) x (W/16)
block5_out = self.conv_block5(mp4_out) # N x 120 x (H/16) x (W/16) --> N x 64 x (H/16) x (W/16)
# -- Decode --
up1_out = interpolate(block5_out, scale_factor=2, mode='bilinear', align_corners=True) # N x 64 x (H/8) x (W/8)
concat4 = torch.cat([concat3, up1_out], 1) # N x (120 + 64) x (H/8) x (W/8)
block6_out = self.conv_block6(concat4) # N x 184 x (H/8) x (W/8) --> N x 32 x (H/8) x (W/8)
up2_out = interpolate(block6_out, scale_factor=2, mode='bilinear', align_corners=True) # N x 32 x (H/4) x (W/4)
concat5 = torch.cat([concat2, up2_out], 1) # N x (56 + 32) x (H/4) x (W/4)
block7_out = self.conv_block7(concat5) # N x 88 x (H/4) x (W/4) --> N x 1 x (H/4) x (W/4)
return block7_out