-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
319 lines (276 loc) · 17.8 KB
/
config.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# ------------------------------------------------------------------
# Main config file
# Contact person: Mohamad Hakam, Shams Eddin <shams@iai.uni-bonn.de>
# Based on https://github.com/sj-li/DP_GAN/blob/main/config.py
# ------------------------------------------------------------------
import numpy as np
import argparse
import pickle
import os
import datetime
# ------------------------------------------------------------------
def add_all_arguments(parser):
# --- general options ---
parser.add_argument('--seed', type=int, default=43, help='random seed')
parser.add_argument('--n_workers', type=int, default=8, help='number of workers for multiprocessing')
parser.add_argument('--pin_memory', type=bool, default=True,
help='allocate the loaded samples in GPU memory. Use it with training on GPU')
parser.add_argument('--batch_size', type=int, default=1, help='batch size')
parser.add_argument('--name', type=str, default='', help='name of the experiment')
parser.add_argument('--dir_log', type=str, default=r'./log', help='log folder')
parser.add_argument('--root_data', type=str, default=r'./data/TerrSysMP_NET',
help='root of the TerrSysMP_NET dataset')
parser.add_argument('--encoder', type=str, default='FocalNet_2D', help='name of the encoder model')
parser.add_argument('--decoder', type=str, default='FocalNet_2D', help='name of the decoder model')
parser.add_argument('--gpu_id', type=int, default=0, help='gpu ids: i.e. 0 (0,1,2, use -1 for CPU)')
parser.add_argument('--n_lat', type=int, default=411, help='latitudinal extension [image height]')
parser.add_argument('--n_lon', type=int, default=423, help='longitudinal extension [image width]')
parser.add_argument('--cut_lateral_boundary', type=int, default=7,
help='number of pixels to be excluded from the lateral boundary relaxation zone')
parser.add_argument('--nan_fill', type=float, default=0.0, help='a value to fill missing data')
# --- encoder ---
parser.add_argument('--in_channels', type=int, default=65+6, help='number of input variables')
parser.add_argument('--out_channels', type=int, default=[96, 192, 384], help='hidden dimensions in the model')
parser.add_argument('--en_down_sampling', type=str, default='conv', help='type of the down-sampling (conv or max)')
parser.add_argument('--en_depths', type=int, default=[2, 2, 2], help='number transformer blocks inside each layer')
parser.add_argument('--en_patch_size', type=int, default=1, help='keep it 1 for regression tasks')
parser.add_argument('--en_channel_att', type=bool, default=False, help='if True, add channel attention')
parser.add_argument('--en_channel_att_r', type=int, default=5, help='reduction rate for channel attention')
parser.add_argument('--en_channel_att_p', type=str, default=['mean', 'std'],
help='pooling type for channel attention')
# encoder Swin v1 + Swin v2 + Focal + Wave_MLP
parser.add_argument('--en_n_heads', type=int, default=[3, 6, 12], help='number of heads for self-attention')
parser.add_argument('--en_window_size', type=int, default=8, help='window size for self-attention')
parser.add_argument('--en_mlp_ratio', type=float, default=4., help='ratio of mlp hidden dim to embedding dim')
parser.add_argument('--en_drop_rate', type=float, default=0.2, help='dropout rate')
parser.add_argument('--en_attn_drop_rate', type=float, default=0.0, help='attention dropout rate')
parser.add_argument('--en_drop_path_rate', type=float, default=0.3, help='stochastic depth rate')
parser.add_argument('--en_qkv_bias', type=bool, default=True,
help='if True, add a learnable bias to query, key, value')
parser.add_argument('--en_qk_scale', type=float, default=None,
help='override default qk scale of head_dim ** -0.5 if set')
parser.add_argument('--en_ape', type=bool, default=False,
help='if True, add absolute position embedding to the patch embedding')
parser.add_argument('--en_patch_norm', type=bool, default=False,
help='if True, add normalization after patch embedding')
parser.add_argument('--en_use_checkpoint', type=bool, default=False,
help='whether to use checkpointing to save memory')
parser.add_argument('--en_fused_window_process', type=bool, default=False,
help='if True, use one kernel to fused window shift & window partition for acceleration, similar for the reversed part')
# encoder Swin v2
parser.add_argument('--en_pretrained_window_sizes', type=int, default=[12, 12, 12],
help='the height and width of the window in pre-training')
# encoder Focal
parser.add_argument('--en_focal_levels', type=int, default=[3, 3, 3],
help='how many focal levels at all stages. Note that this excludes the finest-grain level')
parser.add_argument('--en_focal_windows', type=int, default=[3, 3, 3], help='the focal window size at all stages')
parser.add_argument('--en_use_layerscale', type=bool, default=True,
help='whether to use layerscale proposed in CaiT')
parser.add_argument('--en_layerscale_value', type=float, default=1/np.sqrt(2), help='value for layer scale')
parser.add_argument('--en_use_postln', type=bool, default=False,
help='whether to use layernorm after modulation. It helps stablize training of large models')
parser.add_argument('--en_use_postln_in_modulation', type=bool, default=False,
help='whether to use layernorm after modulation')
parser.add_argument('--en_normalize_modulator', type=bool, default=False,
help='whether to normaize the context in the modulation')
# pretrained encoder model
parser.add_argument('--en_pretrained', type=str, default=None,
help='pretrained model i.e. focalnet_tiny_lrf.pth or trained model with best loss')
# --- decoder ---
parser.add_argument('--de_up_sampling', type=str, default='bilinear', help='type of the up sampling conv or bilinear')
parser.add_argument('--de_depths', type=int, default=[2, 2, 2], help='number transformer blocks inside each layer')
# decoder Swin v1 + Swin v2 + Focal + Wave_MLP
parser.add_argument('--de_n_heads', type=int, default=[3, 6, 12], help='number of heads for self-attention')
parser.add_argument('--de_window_size', type=int, default=8, help='window size for self-attention')
parser.add_argument('--de_mlp_ratio', type=float, default=2., help='ratio of mlp hidden dim to embedding dim')
parser.add_argument('--de_drop_rate', type=float, default=0.2, help='dropout rate')
parser.add_argument('--de_attn_drop_rate', type=float, default=0.0, help='attention dropout rate')
parser.add_argument('--de_drop_path_rate', type=float, default=0.3, help='stochastic depth rate')
parser.add_argument('--de_qkv_bias', type=bool, default=True,
help='if True, add a learnable bias to query, key, value')
parser.add_argument('--de_qk_scale', type=float, default=None,
help='override default qk scale of head_dim ** -0.5 if set')
parser.add_argument('--de_use_checkpoint', type=bool, default=False,
help='whether to use checkpointing to save memory')
parser.add_argument('--de_fused_window_process', type=bool, default=False,
help='if True, use one kernel to fused window shift & window partition for acceleration, similar for the reversed part')
# decoder Swin v2
parser.add_argument('--de_pretrained_window_sizes', type=int, default=[12, 12, 12],
help='the height and width of the window in pre-training')
# decoder Focal
parser.add_argument('--de_focal_levels', type=int, default=[3, 3, 3],
help='how many focal levels at all stages. Note that this excludes the finest-grain level')
parser.add_argument('--de_focal_windows', type=int, default=[3, 3, 3], help='the focal window size at all stages')
parser.add_argument('--de_use_layerscale', type=bool, default=True,
help='whether to use layerscale proposed in CaiT')
parser.add_argument('--de_layerscale_value', type=float, default=1/np.sqrt(2), help='value for layer scale')
parser.add_argument('--de_use_postln', type=bool, default=False,
help='whether to use layernorm after modulation. It helps stabilize training of large models')
parser.add_argument('--de_use_postln_in_modulation', type=bool, default=False,
help='whether to use layernorm after modulation')
parser.add_argument('--de_normalize_modulator', type=bool, default=False,
help='whether to normalize the context in the modulation')
parser.add_argument('--en_de_pretrained', type=str, default=None,
help='pretrained model for testing i.e., a trained model called FocalNet_2D_96.pth')
parser.add_argument('--years_train', type=str,
default=[str(year) for year in range(1989, 2010)] +
[str(year) for year in range(2013, 2017)], help='years for training')
parser.add_argument('--years_val', type=str, default=['2010', '2011', '2017'], help='years for validation')
parser.add_argument('--years_test', type=str, default=['2012', '2018', '2019'], help='years for testing')
parser.add_argument('--is_aug', type=bool, default=True, help='if True, apply data augmentation')
parser.add_argument('--n_epochs', type=int, default=100, help='number of epochs for training')
parser.add_argument('--optimizer', type=str, default='AdamW', help='optimizer for training')
parser.add_argument('--en_lr', type=float, default=0.0003, help='encoder learning rate')
parser.add_argument('--de_lr', type=float, default=0.0003, help='decoder learning rate')
parser.add_argument('--en_weight_decay', type=float, default=0.05, help='encoder weight decay')
parser.add_argument('--de_weight_decay', type=float, default=0.05, help='decoder weight decay')
parser.add_argument('--beta1', type=float, default=0.9, help='beta1 momentum term for Adam/AdamW')
parser.add_argument('--beta2', type=float, default=0.999, help='beta2 momentum term for Adam/AdamW')
parser.add_argument('--lr_scheduler', type=str, default='StepLR', help='learning rate scheduler')
parser.add_argument('--lr_step_size', type=int, default=16, help='learning rate step decay')
parser.add_argument('--lr_decay', type=float, default=0.9, help='learning rate decay')
parser.add_argument('--loss_NDVI', type=str, default='L1', help='type of Loss function for NDVI (L1, L2 or Huber)')
parser.add_argument('--loss_BT', type=str, default='L1', help='type of Loss function for BT (L1, L2 or Huber)')
parser.add_argument('--loss_reduction', type=str, default='mean', help='reduction type for loss functions')
parser.add_argument('--lambda_NDVI', type=float, default=1., help='weight for NDVI Loss')
parser.add_argument('--lambda_BT', type=float, default=1., help='weight for BT Loss')
parser.add_argument('--lambda_vgg', type=float, default=0.1, help='weight for VGG loss')
# input variables
parser.add_argument('--variables', type=str,
default=['awt',
'capec',
'capeml',
'ceiling',
'cli',
'clt',
'clw',
'evspsbl',
# 'gh',
'hfls',
'hfss',
'hudiv',
'hur2',
'hur200',
'hur500',
'hur850',
'hus2',
'hus200',
'hus500',
'hus850',
'incml',
# 'lf',#
# 'pgw',
'pr',
'prc',
'prg',
'prsn',
'prso',
'prt',
'ps',
'psl',
'rlds',
# 'rs',
'sgw',
'snt',
# 'sr',
'ta200',
'ta500',
'ta850',
'tas',
'tch',
'td2',
'trspsbl',
'ua200',
'ua500',
'ua850',
'uas',
'va200',
'va500',
'va850',
'vas',
'wtd',
'zg200',
'zg500',
'zg850',
'zmla'
]
, help='input variables')
parser.add_argument('--variables_static', type=str,
default=['FR_LAND',
'HSURF',
'ZBOT',
'DIS_SEA',
'ROUGHNESS',
'SLOPE'],
help='input variables')
return parser
def read_arguments(train=True, print=True, save=True):
parser = argparse.ArgumentParser()
parser = add_all_arguments(parser)
parser.add_argument('--phase', type=str, default='train')
config = parser.parse_args()
config.phase = 'train' if train else 'test'
if print:
print_options(config, parser)
if save:
save_options(config, parser)
return config
def save_options(config, parser):
if config.name is None or len(config.name) == 0:
config.name = str(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
dir_log = os.path.join(config.dir_log, config.name)
os.makedirs(dir_log, exist_ok=True)
with open(dir_log + '/config.txt', 'wt') as config_file:
message = ''
message += '----------------- Options --------------- -------------------\n\n'
for k, v in sorted(vars(config).items()):
if k in ['variables', 'years_train', 'years_val', 'years_test', 'dir_log', 'root_data']:
continue
# comment = ''
default = parser.get_default(k)
# if v != default:
comment = '\t[default: %s]' % str(default)
message += '{:>25}: {:<20}{}\n'.format(str(k), str(v), comment)
comment = '\t[default: %s]' % str(parser.get_default('root_data'))
message += '\n{:>25}: {:<20}{}\n'.format('root_data', vars(config)['root_data'], comment)
comment = '\t[default: %s]' % str(parser.get_default('dir_log'))
message += '{:>25}: {:<20}{}\n'.format('dir_log', vars(config)['dir_log'], comment)
message += '\n----------------- Input Variables ------- -------------------'
message += '\n\n{}\n'.format(str(config.variables))
message += '\n----------------- Years ----------------- -------------------'
if config.phase == 'train':
message += '\n\nTraining: {}'.format(str(config.years_train))
message += '\nValidation: {}\n'.format(str(config.years_val))
else:
message += '\n\nTesting: {}\n'.format(str(config.years_test))
message += '\n----------------- End ------------------- -------------------'
config_file.write(message)
with open(dir_log + '/config.pkl', 'wb') as config_file:
pickle.dump(config, config_file)
def print_options(config, parser):
message = ''
message += '----------------- Options --------------- -------------------\n\n'
for k, v in sorted(vars(config).items()):
if k in ['variables', 'years_train', 'years_val', 'years_test', 'dir_log', 'root_data']:
continue
# comment = ''
default = parser.get_default(k)
# if v != default:
comment = '\t[default: %s]' % str(default)
message += '{:>25}: {:<20}{}\n'.format(str(k), str(v), comment)
comment = '\t[default: %s]' % str(parser.get_default('root_data'))
message += '\n{:>25}: {:<20}{}\n'.format('root_data', vars(config)['root_data'], comment)
comment = '\t[default: %s]' % str(parser.get_default('dir_log'))
message += '{:>25}: {:<20}{}\n'.format('dir_log', vars(config)['dir_log'], comment)
message += '\n----------------- Input Variables ------- -------------------'
message += '\n\n{}\n'.format(str(config.variables))
message += '\n----------------- Years ----------------- -------------------'
if config.phase == 'train':
message += '\n\nTraining: {}'.format(str(config.years_train))
message += '\nValidation: {}\n'.format(str(config.years_val))
else:
message += '\n\nTesting: {}\n'.format(str(config.years_test))
message += '\n----------------- End ------------------- -------------------'
print(message)
if __name__ == '__main__':
config = read_arguments(train=True, print=True, save=False)