-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_diffusion.py
134 lines (116 loc) · 4.23 KB
/
run_diffusion.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
from diffusion.diffusion import *
from diffusion.eegwave import *
from data.utils import *
from torch.utils.data import random_split
import numpy as np
import wandb
import json
import os
from eval import *
from run_sampling import sample
from eegnet.torch_eegnet import EEGNet
from pathlib import Path
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def load_checkpoint(savepath, device):
checkpoint = torch.load(savepath)
epoch = checkpoint['epoch']
config = checkpoint['config']
function_approximator = EEGWave(
checkpoint['n_class'],
checkpoint['n_subject'],
checkpoint['N'],
checkpoint['n'],
checkpoint['C'],
checkpoint['E'],
checkpoint['K']
)
model = Diffusion(function_approximator, checkpoint['T'])
model.load_state_dict(checkpoint['model_state_dict'])
model.to(device)
return epoch, config, model
print("Initialize")
with open("diffusion/diffusion_conf.json",'r') as fconf:
conf = json.load(fconf)
if "checkpoint" in conf:
savepath = Path(conf["checkpoint"])
epoch, cp_conf, model = load_checkpoint(savepath, device)
wandb.init(project="amal_diffusion",entity="amal_2223",config=cp_conf)
else:
wandb.init(project="amal_diffusion",entity="amal_2223",config=conf)
random_seed = np.random.choice(9999)
config = wandb.config
config.SEED = random_seed
torch.manual_seed(config.SEED)
np.random.seed(config.SEED)
torch.backends.cudnn.deterministic = True
print("Loading data")
if config.DATA == 'VEPESS':
train_ds = VepessDataset(config.N_SUBJECTS,True,partition='train')
val_ds = VepessDataset(config.N_SUBJECTS,True,partition='val')
test_ds = VepessDataset(config.N_SUBJECTS,True,partition='test')
model = Diffusion(EEGWave(n_class=2,n_subject=18,E=70))
SIGNAL_LENGTH = 512
else:
train_ds = BCICIV2aDataset(config.N_SUBJECTS,True,partition='train')
val_ds = BCICIV2aDataset(config.N_SUBJECTS,True,partition='val')
test_ds = BCICIV2aDataset(config.N_SUBJECTS,True,partition='test')
model = Diffusion(EEGWave(n_class=4,n_subject=9,E=25))
SIGNAL_LENGTH = 448
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), config.LEARNING_RATE)
wandb.watch(model, log="all")
train_dl = DataLoader(train_ds,batch_size=config.TRAIN_BATCH_SIZE,shuffle=True)
val_dl = DataLoader(val_ds,batch_size=config.EVAL_BATCH_SIZE,shuffle=False)
test_dl = DataLoader(test_ds,batch_size=config.EVAL_BATCH_SIZE,shuffle=False)
def load_checkpoint(savepath, device):
checkpoint = torch.load(savepath)
epoch = checkpoint['epoch']
config = checkpoint['config']
model = EEGNet(
checkpoint['sampling_rate'],
checkpoint['N'],
checkpoint['L'],
checkpoint['C'],
checkpoint['F1'],
checkpoint['D'],
checkpoint['F2'],
checkpoint['dropout_rate'],
)
model.load_state_dict(checkpoint['model_state_dict'])
model.to(device)
return epoch, config, model
_, conf_eeg, eegnet = load_checkpoint("eegnet/checkpoints/eegnet_default.pch", device)
eegnet.to(device)
is_t, _ = compute_is(eegnet, device, test_dl)
fid_t = compute_fid(eegnet, test_dl, val_dl, device, len(test_ds), len(val_ds))
sampling_conf = {
"checkpoint": None,
"nb_samples": 36,
"data": config.DATA.lower(),
"set": 1,
"signal_length": SIGNAL_LENGTH,
"gamma": 0.1
}
def run(model, device, train_dl, val_dl, optimizer, config, wandb):
for epoch in range(1,1+config.EPOCHS):
train(model, device, train_dl, optimizer, wandb, config.CLASS_CONDITIONING, config.SUBJECT_CONDITIONING)
val(model, device, val_dl, wandb, config.CLASS_CONDITIONING, config.SUBJECT_CONDITIONING)
savepath = Path(f"diffusion/checkpoints/diffusion_{config.SEED}_{epoch}.pch")
save_checkpoint(epoch, config, model, savepath)
if epoch%16==0:
sampling_conf["checkpoint"] = f"diffusion_{config.SEED}_{epoch}.pch"
with open("diffusion/sampling_conf.json",'w') as f:
json.dump(sampling_conf, f)
sample_path = sample()
gen_ds = GenDataset(config.DATA.lower(), os.path.basename(sample_path))
gen_dl = DataLoader(gen_ds,batch_size=sampling_conf["nb_samples"],shuffle=False)
is_g, _ = compute_is(eegnet, device, gen_dl)
fid_g = compute_fid(eegnet, test_dl, gen_dl, device, len(test_ds), len(gen_ds))
wandb.log({
"is_t": is_t,
"fid_t": fid_t,
"is_g": is_g,
"fid_g": fid_g
})
print("Training")
run(model, device, train_dl, val_dl, optimizer, config, wandb)