-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner_DLEPT.py
260 lines (249 loc) · 9.54 KB
/
runner_DLEPT.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
import torch
from models.models_module import NetworkModule
import pytorch_lightning as pl
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers import TensorBoardLogger
from pytorch_lightning.callbacks import RichProgressBar
from pytorch_lightning.callbacks.progress.rich_progress import RichProgressBarTheme
from pathlib import Path
import os
import math
import glob
# General Inputs
root_dir = 'your/dir'
mode = 'train' # Switch to: train, test, test_OOD, fine_tune_train, fine_tune_test, fine_tune_test_OOD
# The "fine_tune_train" option fine-tunes a model that was originally trained using "train",
# When fine-tuning an additional cascade is included in the original architecture.
# The OOD options are used to test the network for a different test dataset, outside-of-distribution of your standard test dataset.
# Architecture Inputs
architecture = 'TransUNet' # UNet, TransUNet
normalization = 'FiLM' # IN, FiLM
cascades = 3 # Any number you want
# Unet Inputs
in_chans = 3 # Three inputs for the B1+, Transceive Phase, and Edge mask.
out_chans = 2 # Two outputs for relative permittivity and conductivity.
chans = 64 # Channels of the UNets
num_pool_layers = 2 # Number of pooling layers per UNet
drop_prob = 0.1 # Dropout probability
# Vision transformer Inputs, Control the size of the transformer and should be adapted depending on the size of your network and the size of your data.
num_heads = 16
num_layers = 6
num_patches = 1089
# Training Inputs
epochs = 100 # Number of Training epochs
epochs_fine_tune = 100 # Number of epochs for fine-tuning
div_factor = 100 # Controls the learning rate, according to the scheduler
final_div_factor = 1000 # Controls the learning rate, according to the scheduler
lr = 0.01 # Initial Learning Rate
lr_fine_tune = 0.003 # Initial Learning Rate during fine-tuning
weight_decay = 0 # Weight decay
gradient_clip_val = 1.0 # Gradient Clip
# Normalization of the outputs. These are used so the network is trained with values between 0 and 1 and can be adjusted if needed.
# Remember to multiply the outputs with these values to get the true electrical properties
norm_er = 135 # For relative permittivity
norm_se = 2.8 # For conductivity
# If fine tuning
fine_tune_str = ''
if mode == 'fine_tune_train' or mode == 'fine_tune_test' or mode == 'fine_tune_test_OOD':
fine_tune_str = '_fine_tune'
# If testing OOD
OOD_str = ''
if mode == 'test_OOD' or mode == 'fine_tune_test_OOD':
OOD_str = '_OOD'
# Progress bar
progress_bar = RichProgressBar(
theme=RichProgressBarTheme(
description="green_yellow",
progress_bar="green1",
progress_bar_finished="green1",
progress_bar_pulse="#6206E0",
batch_progress="green_yellow",
time="grey82",
processing_speed="grey82",
metrics="grey82"
)
)
# Paths
logging_dir = Path(root_dir + "/logs/" + architecture + '_' + normalization + '_' + str(cascades) + "/")
mREDm_dir = logging_dir / 'mREDm'
mREDm_fine_tune_dir = logging_dir / 'mREDm_fine_tune'
checkpoint_dir = logging_dir / 'checkpoints'
checkpoints_fine_tune_dir = logging_dir / 'checkpoints_fine_tune'
if not os.path.exists(logging_dir):
os.makedirs(logging_dir)
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
if not os.path.exists(checkpoints_fine_tune_dir):
os.makedirs(checkpoints_fine_tune_dir)
if not os.path.exists(mREDm_dir):
os.makedirs(mREDm_dir)
if not os.path.exists(mREDm_fine_tune_dir):
os.makedirs(mREDm_fine_tune_dir)
tb_logger = TensorBoardLogger(logging_dir, name='mREDm')
tb_fine_tune_logger = TensorBoardLogger(logging_dir, name='mREDm_fine_tune')
ckpt_list = sorted(checkpoint_dir.glob("*.ckpt"), key=os.path.getmtime)
ckpt_fine_tune_list = sorted(checkpoints_fine_tune_dir.glob("*.ckpt"), key=os.path.getmtime)
# Model
if mode == 'train':
checkpoint_callback = ModelCheckpoint(
dirpath = root_dir+'/logs/'+architecture+'_'+normalization+'_'+str(cascades)+'/checkpoints',
save_top_k=True, verbose=True,
monitor="val/MSE_loss",
mode="min"
)
model = NetworkModule(
in_chans=in_chans,
out_chans=out_chans,
chans=chans,
num_pool_layers=num_pool_layers,
drop_prob=drop_prob,
lr=lr,
epochs = epochs,
div_factor = div_factor,
final_div_factor = final_div_factor,
weight_decay=weight_decay,
data_path = root_dir,
OOD_str=OOD_str,
fine_tune_str = fine_tune_str,
num_heads=num_heads,
num_layers=num_layers,
num_patches=num_patches,
norm_er = norm_er,
norm_se = norm_se,
architecture = architecture,
normalization = normalization,
cascades = cascades
)
if ckpt_list:
trainer = pl.Trainer(
accelerator = 'gpu',
devices = 1,
max_epochs=epochs,
default_root_dir=root_dir,
resume_from_checkpoint=str(ckpt_list[-1]),
callbacks=[checkpoint_callback, progress_bar],
logger=tb_logger,
log_every_n_steps=10,
gradient_clip_val=gradient_clip_val
)
else:
trainer = pl.Trainer(
accelerator = 'gpu',
devices = 1,
max_epochs=epochs,
default_root_dir=root_dir,
callbacks=[checkpoint_callback, progress_bar],
logger=tb_logger,
log_every_n_steps=10,
gradient_clip_val=gradient_clip_val
)
trainer.fit(model)
elif mode == 'fine_tune_train':
checkpoint_callback = ModelCheckpoint(
dirpath = root_dir+'/logs/'+architecture+'_'+normalization+'_'+str(cascades)+'/checkpoints_fine_tune',
save_top_k=True,
verbose=True,
monitor="val/MSE_loss",
mode="min"
)
model = NetworkModule(
in_chans=in_chans,
out_chans=out_chans,
chans=chans,
num_pool_layers=num_pool_layers,
drop_prob=drop_prob,
lr=lr_fine_tune,
epochs=epochs_fine_tune,
div_factor=div_factor,
final_div_factor=final_div_factor,
weight_decay=weight_decay,
data_path=root_dir,
OOD_str=OOD_str,
fine_tune_str=fine_tune_str,
num_heads=num_heads,
num_layers=num_layers,
num_patches=num_patches,
norm_er = norm_er,
norm_se = norm_se,
architecture = architecture,
normalization = normalization,
cascades = cascades
)
ckpt_path = str(ckpt_list[-1])
checkpoint = torch.load(ckpt_path, map_location="cuda:0")
state_dict = checkpoint['state_dict']
model_dict = model.state_dict()
checkpoint = {k: v for k, v in state_dict.items() if k in model_dict}
model_dict.update(checkpoint)
model.load_state_dict(model_dict)
model.requires_grad_(True)
fine_tune_checkpoints = sorted(glob.glob(root_dir + '/logs/'+architecture+'_'+normalization+'_'+str(cascades)+'/checkpoints_fine_tune/*.ckpt'))
latest_fine_tune_checkpoint = fine_tune_checkpoints[-1] if fine_tune_checkpoints else None
trainer = pl.Trainer(
accelerator = 'gpu',
devices = 1,
max_epochs=epochs_fine_tune,
default_root_dir=root_dir,
callbacks=[checkpoint_callback, progress_bar],
logger=tb_fine_tune_logger,
log_every_n_steps=10,
gradient_clip_val=gradient_clip_val,
resume_from_checkpoint=latest_fine_tune_checkpoint
)
trainer.fit(model)
elif mode == 'test' or mode == 'test_OOD':
ckpt_path = str(ckpt_list[-1])
model = NetworkModule.load_from_checkpoint(
ckpt_path,
in_chans=in_chans,
out_chans=out_chans,
chans=chans,
num_pool_layers=num_pool_layers,
drop_prob=drop_prob,
lr=lr,
epochs = epochs,
div_factor = div_factor,
final_div_factor = final_div_factor,
weight_decay=weight_decay,
data_path = root_dir,
OOD_str=OOD_str,
fine_tune_str=fine_tune_str,
num_heads=num_heads,
num_layers=num_layers,
num_patches=num_patches,
norm_er = norm_er,
norm_se = norm_se,
architecture = architecture,
normalization = normalization,
cascades = cascades,
)
trainer = pl.Trainer(accelerator='gpu', devices=1, callbacks=progress_bar)
trainer.test(model)
elif mode == 'fine_tune_test' or mode == 'fine_tune_test_OOD':
ckpt_path = str(ckpt_fine_tune_list[-1])
model = NetworkModule.load_from_checkpoint(
ckpt_path,
in_chans=in_chans,
out_chans=out_chans,
chans=chans,
num_pool_layers=num_pool_layers,
drop_prob=drop_prob,
lr=lr_fine_tune,
epochs = epochs_fine_tune,
div_factor = div_factor,
final_div_factor = final_div_factor,
weight_decay=weight_decay,
data_path = root_dir,
OOD_str=OOD_str,
fine_tune_str=fine_tune_str,
num_heads=num_heads,
num_layers=num_layers,
num_patches=num_patches,
norm_er = norm_er,
norm_se = norm_se,
architecture = architecture,
normalization = normalization,
cascades = cascades
)
trainer = pl.Trainer(accelerator='gpu', devices=1, callbacks=progress_bar)
trainer.test(model)