-
Notifications
You must be signed in to change notification settings - Fork 0
/
epiframework.py
343 lines (316 loc) · 16.5 KB
/
epiframework.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import scipy.interpolate
import itertools
import datetime
import numpy as np
import pandas as pd
import build_dataset, training_datasets
import nn_blocks, idplots, ddpm, myutils, inpaint, ground_truth
import sys
sys.path.append('CoPaint4influpaint')
from guided_diffusion import O_DDIMSampler
from guided_diffusion import unet
from utils import config
def copaint_config_library(timesteps):
config_lib = {
"celebahq_try1":config.Config(default_config_dict={
"respace_interpolate": False,
"ddim": {
"ddim_sigma": 0.0,
"schedule_params": {
"ddpm_num_steps": timesteps,
"jump_length": 20, #10,
"jump_n_sample": 4, #2,
"num_inference_steps": timesteps,
"schedule_type": "linear",
"time_travel_filter_type": "none",
"use_timetravel": True
}
},
"optimize_xt": {
"coef_xt_reg": 0.00001,#0.0001,
"coef_xt_reg_decay": 1.05,#1.01,
"filter_xT": False,
"lr_xt": 0.02,
"lr_xt_decay": 1.012,
"mid_interval_num": 1,
"num_iteration_optimize_xt": 5,
"optimize_before_time_travel": True,
"optimize_xt": True,
"use_adaptive_lr_xt": True,
"use_smart_lr_xt_decay": True
},
"debug":False
}, use_argparse=False),
"celebahq_noTT":config.Config(default_config_dict={
"respace_interpolate": False,
"ddim": {
"ddim_sigma": 0.0,
"schedule_params": {
"ddpm_num_steps": timesteps,
"jump_length": 10,
"jump_n_sample": 2,
"num_inference_steps": timesteps,
"schedule_type": "linear",
"time_travel_filter_type": "none",
"use_timetravel": False,
}
},
"optimize_xt": {
"coef_xt_reg": 0.0001,
"coef_xt_reg_decay": 1.01,
"filter_xT": False,
"lr_xt": 0.02,
"lr_xt_decay": 1.012,
"mid_interval_num": 1,
"num_iteration_optimize_xt": 2,
"optimize_before_time_travel": True,
"optimize_xt": True,
"use_adaptive_lr_xt": True,
"use_smart_lr_xt_decay": True
},
"debug":False
}, use_argparse=False),
"celebahq_noTT2":config.Config(default_config_dict={
"respace_interpolate": False,
"ddim": {
"ddim_sigma": 0.0,
"schedule_params": {
"ddpm_num_steps": timesteps,
"jump_length": 10,
"jump_n_sample": 2,
"num_inference_steps": timesteps,
"schedule_type": "linear",
"time_travel_filter_type": "none",
"use_timetravel": False,
}
},
"optimize_xt": {
"coef_xt_reg": 0.0001,
"coef_xt_reg_decay": 1.01,
"filter_xT": False,
"lr_xt": 0.02,
"lr_xt_decay": 1.012,
"mid_interval_num": 1,
"num_iteration_optimize_xt": 5,
"optimize_before_time_travel": True,
"optimize_xt": True,
"use_adaptive_lr_xt": True,
"use_smart_lr_xt_decay": True
},
"debug":False
}, use_argparse=False),
"celebahq_try3":config.Config(default_config_dict={
"respace_interpolate": False,
"ddim": {
"ddim_sigma": 0.0,
"schedule_params": {
"ddpm_num_steps": timesteps,
"jump_length": 5, #10
"jump_n_sample": 2,
"num_inference_steps": timesteps,
"schedule_type": "linear",
"time_travel_filter_type": "none",
"use_timetravel": True
}
},
"optimize_xt": {
"coef_xt_reg": 0.0001,
"coef_xt_reg_decay": 1.01,
"filter_xT": False,
"lr_xt": 0.02,
"lr_xt_decay": 1.012,
"mid_interval_num": 1,
"num_iteration_optimize_xt": 5,#2,
"optimize_before_time_travel": True,
"optimize_xt": True,
"use_adaptive_lr_xt": True,
"use_smart_lr_xt_decay": True
},
"debug":False
}, use_argparse=False),
"celebahq":config.Config(default_config_dict={
"respace_interpolate": False,
"ddim": {
"ddim_sigma": 0.0,
"schedule_params": {
"ddpm_num_steps": timesteps,
"jump_length": 10,
"jump_n_sample": 2,
"num_inference_steps": timesteps,
"schedule_type": "linear",
"time_travel_filter_type": "none",
"use_timetravel": True
}
},
"optimize_xt": {
"coef_xt_reg": 0.0001,
"coef_xt_reg_decay": 1.01,
"filter_xT": False,
"lr_xt": 0.02,
"lr_xt_decay": 1.012,
"mid_interval_num": 1,
"num_iteration_optimize_xt": 2,
"optimize_before_time_travel": True,
"optimize_xt": True,
"use_adaptive_lr_xt": True,
"use_smart_lr_xt_decay": True
},
"debug":False
}, use_argparse=False),
#"imagenet":config.Config(default_config_dict={
# "respace_interpolate": False,
# "ddim": {
# "ddim_sigma": 0.0,
# "schedule_params": {
# "ddpm_num_steps": timesteps,
# "jump_length": 10,
# "jump_n_sample": 2,
# "num_inference_steps": 200,
# "schedule_type": "linear",
# "time_travel_filter_type": "none",
# "use_timetravel": True
# }
# },
# "optimize_xt": {
# "coef_xt_reg": 0.01,
# "coef_xt_reg_decay": 1.0,
# "filter_xT": False,
# "lr_xt": 0.02,
# "lr_xt_decay": 1.012,
# "mid_interval_num": 1,
# "num_iteration_optimize_xt": 2,
# "optimize_before_time_travel": True,
# "optimize_xt": True,
# "use_adaptive_lr_xt": True,
# "use_smart_lr_xt_decay": True
#
# },
# "debug":False
# }, use_argparse=False),
}
return config_lib
def model_libary(image_size, channels, epoch, device, batch_size):
unet_spec = {
"MyUnet200": ddpm.DDPM(model=nn_blocks.Unet(
dim=image_size,
channels=channels,
dim_mults=(1, 2, 4,),
use_convnext=False
),
image_size=image_size,
channels=channels,
batch_size=batch_size,
epochs=epoch,
timesteps=200,
device=device),
"MyUnet500": ddpm.DDPM(model=nn_blocks.Unet(
dim=image_size,
channels=channels,
dim_mults=(1, 2, 4,),
use_convnext=False
),
image_size=image_size,
channels=channels,
batch_size=batch_size,
epochs=epoch,
timesteps=500,
device=device)
}
return unet_spec
def dataset_library(gt1, channels):
dataset_spec = {
#"Fv":training_datasets.FluDataset.from_fluview(season_setup=gt1.season_setup, download=False),
"R1Fv": training_datasets.FluDataset.from_SMHR1_fluview(season_setup=gt1.season_setup, download=False),
"R1": training_datasets.FluDataset.from_csp_SMHR1('Flusight/flu-datasets/synthetic/CSP_FluSMHR1_weekly_padded_4scn.nc', channels=channels)
}
return dataset_spec
def get_git_revision_short_hash() -> str:
import subprocess
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()
def create_folders(path):
from pathlib import Path
Path(path).mkdir(parents=True, exist_ok=True)
def transform_library(scaling_per_channel):
from torchvision import transforms
import epitransforms
print(scaling_per_channel)
transform_enrich = {
"No":transforms.Compose([]),
"PoisPadScale":transforms.Compose([
transforms.Lambda(lambda t: epitransforms.transform_poisson(t)),
transforms.Lambda(lambda t: epitransforms.transform_random_padintime(t, min_shift = -15, max_shift = 15)),
transforms.Lambda(lambda t: epitransforms.transform_randomscale(t, min=.1, max=1.9)),
]),
"PoisPadScaleSmall":transforms.Compose([
transforms.Lambda(lambda t: epitransforms.transform_poisson(t)),
transforms.Lambda(lambda t: epitransforms.transform_random_padintime(t, min_shift = -4, max_shift = 4)),
transforms.Lambda(lambda t: epitransforms.transform_randomscale(t, min=.7, max=1.3)),
]),
"Pois":transforms.Compose([
transforms.Lambda(lambda t: epitransforms.transform_poisson(t)),
])
}
# transforms.Lambda(lambda t: epitransforms.transform_skewednoise(t, scale=.4, a=-1.8))
transforms_spec = {
# No scaling (linear scale)
"Lins":{
"reg":transforms.Compose([
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale(t, scale = 1/scaling_per_channel)),
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale(t, scale = 2)) ,
]),
"inv":transforms.Compose([
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale_inv(t, scale = 1/scaling_per_channel)),
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale_inv(t, scale = 2)),
][::-1])
},
# sqrt scale
"Sqrt":{
"reg":transforms.Compose([
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale(t, scale = 1/scaling_per_channel)),
epitransforms.transform_sqrt,
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale(t, scale = 2)) ,
]),
"inv":transforms.Compose([
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale_inv(t, scale = 1/scaling_per_channel)),
epitransforms.transform_sqrt_inv,
transforms.Lambda(lambda t: epitransforms.transform_channelwisescale_inv(t, scale = 2)),
][::-1])
},
}
return transforms_spec, transform_enrich
def create_run_config(run_id, specifications):
if setup.scale == 'Regions':
scenarios_specs = {
'dataset': [3, 15, 150], # ax.set_ylim(0.05, 0.4)
# 'vacctotalM': [2, 5, 10, 15, 20],
'newdoseperweek': [125000, 250000, 479700, 1e6, 1.5e6, 2e6],
'epicourse': ['U', 'L'] # 'U'
}
elif setup.scale == 'Provinces':
if setup.nnodes == 107:
scenarios_specs = {
'vaccpermonthM': [3, 15, 150], # ax.set_ylim(0.05, 0.4)
# 'vacctotalM': [2, 5, 10, 15, 20],
'newdoseperweek': [125000, 250000, 479700, 1e6, 1.5e6, 2e6],
'epicourse': ['U', 'L'] # 'U'
}
elif setup.nnodes == 10:
scenarios_specs = {
'newdoseperweek': [125000*10, 250000*10],
'vaccpermonthM': [14*10, 15*10],
'epicourse': ['U'] # 'U', 'L'
}
# Compute all permutatios
keys, values = zip(*scenarios_specs.items())
permuted_specs = [dict(zip(keys, v)) for v in itertools.product(*values)]
specs_df = pd.DataFrame.from_dict(permuted_specs)
if setup.nnodes == 107:
specs_df = specs_df[((specs_df['vaccpermonthM'] == 15.0) | (specs_df['newdoseperweek'] == 479700.0))].reset_index(drop=True) # Filter out useless scenarios
# scn_spec = permuted_specs[scn_id]
scn_spec = specs_df.loc[scn_id]
tot_pop = setup.pop_node.sum()
scenario = {'name': f"{scn_spec['epicourse']}-r{int(scn_spec['vaccpermonthM'])}-t{int(scn_spec['newdoseperweek'])}-id{scn_id}",
'newdoseperweek': scn_spec['newdoseperweek'],
'rate_fomula': f"({scn_spec['vaccpermonthM'] * 1e6 / tot_pop / 30}*pop_nd)"
}
return scenario