forked from HansBambel/SmaAt-UNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unet_precip_regression_lightning.py
234 lines (202 loc) · 9.22 KB
/
unet_precip_regression_lightning.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import argparse
from models.unet_parts import *
from models.unet_parts_depthwise_separable import DoubleConvDS, UpDS, DownDS
from models.layers import CBAM
import pytorch_lightning as pl
from models.regression_lightning import Precip_regression_base
class UNet(Precip_regression_base):
def __init__(self, hparams):
super(UNet, self).__init__(hparams=hparams)
self.n_channels = hparams.n_channels
self.n_classes = hparams.n_classes
self.bilinear = hparams.bilinear
self.inc = DoubleConv(self.n_channels, 64)
self.down1 = Down(64, 128)
self.down2 = Down(128, 256)
self.down3 = Down(256, 512)
factor = 2 if self.bilinear else 1
self.down4 = Down(512, 1024 // factor)
self.up1 = Up(1024, 512 // factor, self.bilinear)
self.up2 = Up(512, 256 // factor, self.bilinear)
self.up3 = Up(256, 128 // factor, self.bilinear)
self.up4 = Up(128, 64, self.bilinear)
self.outc = OutConv(64, self.n_classes)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
logits = self.outc(x)
return logits
class UNet_Attention(Precip_regression_base):
def __init__(self, hparams):
super(UNet_Attention, self).__init__(hparams=hparams)
self.n_channels = hparams.n_channels
self.n_classes = hparams.n_classes
self.bilinear = hparams.bilinear
reduction_ratio = hparams.reduction_ratio
self.inc = DoubleConv(self.n_channels, 64)
self.cbam1 = CBAM(64, reduction_ratio=reduction_ratio)
self.down1 = Down(64, 128)
self.cbam2 = CBAM(128, reduction_ratio=reduction_ratio)
self.down2 = Down(128, 256)
self.cbam3 = CBAM(256, reduction_ratio=reduction_ratio)
self.down3 = Down(256, 512)
self.cbam4 = CBAM(512, reduction_ratio=reduction_ratio)
factor = 2 if self.bilinear else 1
self.down4 = Down(512, 1024 // factor)
self.cbam5 = CBAM(1024 // factor, reduction_ratio=reduction_ratio)
self.up1 = Up(1024, 512 // factor, self.bilinear)
self.up2 = Up(512, 256 // factor, self.bilinear)
self.up3 = Up(256, 128 // factor, self.bilinear)
self.up4 = Up(128, 64, self.bilinear)
self.outc = OutConv(64, self.n_classes)
def forward(self, x):
x1 = self.inc(x)
x1Att = self.cbam1(x1)
x2 = self.down1(x1)
x2Att = self.cbam2(x2)
x3 = self.down2(x2)
x3Att = self.cbam3(x3)
x4 = self.down3(x3)
x4Att = self.cbam4(x4)
x5 = self.down4(x4)
x5Att = self.cbam5(x5)
x = self.up1(x5Att, x4Att)
x = self.up2(x, x3Att)
x = self.up3(x, x2Att)
x = self.up4(x, x1Att)
logits = self.outc(x)
return logits
class UNetDS(Precip_regression_base):
def __init__(self, hparams):
super(UNetDS, self).__init__(hparams=hparams)
self.n_channels = hparams.n_channels
self.n_classes = hparams.n_classes
self.bilinear = hparams.bilinear
kernels_per_layer = hparams.kernels_per_layer
self.inc = DoubleConvDS(self.n_channels, 64, kernels_per_layer=kernels_per_layer)
self.down1 = DownDS(64, 128, kernels_per_layer=kernels_per_layer)
self.down2 = DownDS(128, 256, kernels_per_layer=kernels_per_layer)
self.down3 = DownDS(256, 512, kernels_per_layer=kernels_per_layer)
factor = 2 if self.bilinear else 1
self.down4 = DownDS(512, 1024 // factor, kernels_per_layer=kernels_per_layer)
self.up1 = UpDS(1024, 512 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up2 = UpDS(512, 256 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up3 = UpDS(256, 128 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up4 = UpDS(128, 64, self.bilinear, kernels_per_layer=kernels_per_layer)
self.outc = OutConv(64, self.n_classes)
def forward(self, x):
x1 = self.inc(x)
x2 = self.down1(x1)
x3 = self.down2(x2)
x4 = self.down3(x3)
x5 = self.down4(x4)
x = self.up1(x5, x4)
x = self.up2(x, x3)
x = self.up3(x, x2)
x = self.up4(x, x1)
logits = self.outc(x)
return logits
class UNetDS_Attention(Precip_regression_base):
def __init__(self, hparams):
super(UNetDS_Attention, self).__init__(hparams=hparams)
self.n_channels = hparams.n_channels
self.n_classes = hparams.n_classes
self.bilinear = hparams.bilinear
reduction_ratio = hparams.reduction_ratio
kernels_per_layer = hparams.kernels_per_layer
self.inc = DoubleConvDS(self.n_channels, 64, kernels_per_layer=kernels_per_layer)
self.cbam1 = CBAM(64, reduction_ratio=reduction_ratio)
self.down1 = DownDS(64, 128, kernels_per_layer=kernels_per_layer)
self.cbam2 = CBAM(128, reduction_ratio=reduction_ratio)
self.down2 = DownDS(128, 256, kernels_per_layer=kernels_per_layer)
self.cbam3 = CBAM(256, reduction_ratio=reduction_ratio)
self.down3 = DownDS(256, 512, kernels_per_layer=kernels_per_layer)
self.cbam4 = CBAM(512, reduction_ratio=reduction_ratio)
factor = 2 if self.bilinear else 1
self.down4 = DownDS(512, 1024 // factor, kernels_per_layer=kernels_per_layer)
self.cbam5 = CBAM(1024 // factor, reduction_ratio=reduction_ratio)
self.up1 = UpDS(1024, 512 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up2 = UpDS(512, 256 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up3 = UpDS(256, 128 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up4 = UpDS(128, 64, self.bilinear, kernels_per_layer=kernels_per_layer)
self.outc = OutConv(64, self.n_classes)
def forward(self, x):
x1 = self.inc(x)
x1Att = self.cbam1(x1)
x2 = self.down1(x1)
x2Att = self.cbam2(x2)
x3 = self.down2(x2)
x3Att = self.cbam3(x3)
x4 = self.down3(x3)
x4Att = self.cbam4(x4)
x5 = self.down4(x4)
x5Att = self.cbam5(x5)
x = self.up1(x5Att, x4Att)
x = self.up2(x, x3Att)
x = self.up3(x, x2Att)
x = self.up4(x, x1Att)
logits = self.outc(x)
return logits
class UNetDS_Attention_4CBAMs(Precip_regression_base):
def __init__(self, hparams):
super(UNetDS_Attention_4CBAMs, self).__init__(hparams=hparams)
self.n_channels = hparams.n_channels
self.n_classes = hparams.n_classes
self.bilinear = hparams.bilinear
reduction_ratio = hparams.reduction_ratio
kernels_per_layer = hparams.kernels_per_layer
self.inc = DoubleConvDS(self.n_channels, 64, kernels_per_layer=kernels_per_layer)
self.cbam1 = CBAM(64, reduction_ratio=reduction_ratio)
self.down1 = DownDS(64, 128, kernels_per_layer=kernels_per_layer)
self.cbam2 = CBAM(128, reduction_ratio=reduction_ratio)
self.down2 = DownDS(128, 256, kernels_per_layer=kernels_per_layer)
self.cbam3 = CBAM(256, reduction_ratio=reduction_ratio)
self.down3 = DownDS(256, 512, kernels_per_layer=kernels_per_layer)
self.cbam4 = CBAM(512, reduction_ratio=reduction_ratio)
factor = 2 if self.bilinear else 1
self.down4 = DownDS(512, 1024 // factor, kernels_per_layer=kernels_per_layer)
self.up1 = UpDS(1024, 512 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up2 = UpDS(512, 256 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up3 = UpDS(256, 128 // factor, self.bilinear, kernels_per_layer=kernels_per_layer)
self.up4 = UpDS(128, 64, self.bilinear, kernels_per_layer=kernels_per_layer)
self.outc = OutConv(64, self.n_classes)
def forward(self, x):
x1 = self.inc(x)
x1Att = self.cbam1(x1)
x2 = self.down1(x1)
x2Att = self.cbam2(x2)
x3 = self.down2(x2)
x3Att = self.cbam3(x3)
x4 = self.down3(x3)
x4Att = self.cbam4(x4)
x5 = self.down4(x4)
x = self.up1(x5, x4Att)
x = self.up2(x, x3Att)
x = self.up3(x, x2Att)
x = self.up4(x, x1Att)
logits = self.outc(x)
return logits
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser = Precip_regression_base.add_model_specific_args(parser)
parser = pl.Trainer.add_argparse_args(parser)
parser.add_argument('--dataset_folder',
default='../data/precipitation/RAD_NL25_RAC_5min_train_test_2016-2019.h5', type=str)
parser.add_argument('--batch_size', type=int, default=8)
parser.add_argument('--learning_rate', type=float, default=0.001)
parser.add_argument('--epochs', type=int, default=150)
args = parser.parse_args()
net = UNetDS_Attention(hparams=args)
trainer = pl.Trainer(gpus=1,
fast_dev_run=True,
weights_summary=None,
default_save_path="../lightning/precip",
max_epochs=args.epochs)
trainer.fit(net)