-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
153 lines (127 loc) · 7.27 KB
/
Copy pathtrain.py
File metadata and controls
153 lines (127 loc) · 7.27 KB
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
import os
import time
import json
import torch
import torch.multiprocessing as mp
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from flow_n_corr_utils import min_max_norm
from model import CVAE
from dataset import cvae_dataset
torch.manual_seed = 21
def train_cvae(l1o_idx, top_out_path, device, config):
torch.cuda.set_device(device)
DEVICE = torch.device(device)
out_path = f"{top_out_path}/{l1o_idx}"
print(out_path)
os.makedirs(out_path)
os.makedirs(out_path+"/train")
os.makedirs(out_path+"/eval")
os.makedirs(out_path+"/z_generation")
train_writer = SummaryWriter(out_path+"/train")
eval_writer = SummaryWriter(out_path+"/eval")
generation_writer = SummaryWriter(out_path+"/z_generation")
dataset_path = "/home/shahar/projects/CVAE_proj/CVAE/data/data_for_cvae" #its the same for train and validation because of leavw-one-out (l1o) training strategy. l1o_idx param sets the validation sampe num.
train_set = cvae_dataset(dataset_path, device=DEVICE, l1o_idx=l1o_idx, train=True, augmentations=True, augmentation_params=config["augmentation_params"])
train_loader = DataLoader(train_set, batch_size=config["batch_size"], shuffle=True)
val_set = cvae_dataset(dataset_path, device=DEVICE, l1o_idx=l1o_idx, train=False)
val_loader = DataLoader(val_set, batch_size=1, shuffle=False)
num_chs = [3, 16, 32, 64, 96, 128, 128]
cvae_model = CVAE(num_chs=num_chs, max_flow_hat_abs_val=50).to(DEVICE)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(cvae_model.parameters(), lr=1e-3)
lambda_reconstraction = config["lambda_reconstraction"]
lambda_kl = config["lambda_kl"]
num_epochs = config["num_epochs"]
for num_epoch in range(num_epochs):
cvae_model.train()
epoch_train_acc_loss = 0
epoch_train_reconstraction_loss = 0
epoch_train_kl = 0
for batch_num, (flow, conditions_pyramid) in enumerate(train_loader):
optimizer.zero_grad()
flow_hat, train_kl = cvae_model(flow, conditions_pyramid)
train_reconstraction_loss = criterion(flow_hat, flow)
train_loss = lambda_reconstraction * train_reconstraction_loss + lambda_kl * train_kl
train_loss.backward()
torch.nn.utils.clip_grad_norm_(cvae_model.parameters(), max_norm=config["grad_max_norm"])
optimizer.step()
epoch_train_acc_loss += train_loss.item()
epoch_train_kl += train_kl.item()
epoch_train_reconstraction_loss += train_reconstraction_loss.item()
epoch_train_acc_loss /= (batch_num+1)
epoch_train_reconstraction_loss /= (batch_num+1)
epoch_train_kl /= (batch_num+1)
train_writer.add_scalar("reconstraction_loss", epoch_train_reconstraction_loss, num_epoch)
train_writer.add_scalar("kl", epoch_train_kl, num_epoch)
train_writer.add_scalar("total_loss", epoch_train_acc_loss, num_epoch)
train_writer.add_image("f_hat",min_max_norm(flow_hat[0, :, :, 60, :]), num_epoch) # 3 h w
train_writer.add_image("f",min_max_norm(flow[0, :, :, 60, :]), num_epoch) # 3 h w
train_writer.add_image("img",min_max_norm(conditions_pyramid[-1][0, :, :, 60, :]), num_epoch) # 3 h w
cvae_model.eval()
epoch_eval_acc_loss = 0
epoch_eval_reconstraction_loss = 0
epoch_eval_generation_loss = 0
epoch_eval_kl = 0
for batch_num, (flow, conditions_pyramid) in enumerate(val_loader):
flow_hat, eval_kl = cvae_model(flow, conditions_pyramid)
eval_reconstraction_loss = criterion(flow_hat, flow)
eval_loss = lambda_reconstraction * eval_reconstraction_loss + lambda_kl * eval_kl
epoch_eval_acc_loss += eval_loss.item()
epoch_eval_kl += eval_kl.item()
epoch_eval_reconstraction_loss += eval_reconstraction_loss.item()
flow_hat_generated = cvae_model.generate(conditions_pyramid, device=DEVICE)
eval_generation_loss = criterion(flow_hat_generated, flow)
epoch_eval_generation_loss += eval_generation_loss.item()
epoch_eval_acc_loss /= (batch_num+1)
epoch_eval_reconstraction_loss /= (batch_num+1)
epoch_eval_kl /= (batch_num+1)
epoch_eval_generation_loss /= (batch_num+1)
eval_writer.add_scalar("reconstraction_loss", epoch_eval_reconstraction_loss, num_epoch)
eval_writer.add_scalar("kl", epoch_eval_kl, num_epoch)
eval_writer.add_scalar("total_loss", epoch_eval_acc_loss, num_epoch)
generation_writer.add_scalar("reconstraction_loss", epoch_eval_generation_loss, num_epoch)
eval_writer.add_image("f_hat",min_max_norm(flow_hat[0, :, :, 60, :]), num_epoch) # 3 h w
generation_writer.add_image("f_hat",min_max_norm(flow_hat_generated[0, :, :, 60, :]), num_epoch) # 3 h w
eval_writer.add_image("f",min_max_norm(flow[0, :, :, 60, :]), num_epoch) # 3 h w
eval_writer.add_image("img",min_max_norm(conditions_pyramid[-1][0, :, :, 60, :]), num_epoch) # 3 h w
if num_epoch % config["save_weights_every"] == 0:
torch.save(cvae_model.state_dict(), f"{out_path}/model_e{num_epoch}.pt")
for batch_num, (flow, conditions_pyramid) in enumerate(val_loader):
flow_hat_generated = cvae_model.generate(conditions_pyramid, device=DEVICE).detach().cpu().numpy()
np.save(f"{out_path}/flow_generated_e{num_epoch}.npy", flow_hat_generated)
np.save(f"{out_path}/condition.npy", conditions_pyramid[-1].detach().cpu().numpy())
torch.save(cvae_model.state_dict(), f"{out_path}/model_final.pt")
cvae_model.eval()
for batch_num, (flow, conditions_pyramid) in enumerate(val_loader):
flow_hat_generated = cvae_model.generate(conditions_pyramid, device=DEVICE).detach().cpu().numpy()
np.save(f"{out_path}/flow_generated_final.npy", flow_hat_generated)
np.save(f"{out_path}/condition.npy", conditions_pyramid[-1].detach().cpu().numpy())
if __name__ == '__main__':
mp.set_start_method('spawn')
DEVICE0 = "cuda:0"
DEVICE1 = "cuda:1"
multiprocess=False
top_out_path = f"/home/shahar/projects/CVAE_proj/CVAE/outputs/len_44_dataset/{time.strftime('%Y%m%d_%H%M%S')}"
os.makedirs(top_out_path)
config_file_path = "config.json"
with open(config_file_path, 'r') as config_file:
config = json.load(config_file)
output_config_path = os.path.join(top_out_path, "config.json")
with open(output_config_path, 'w') as output_config_file:
json.dump(config, output_config_file, indent=4)
if not multiprocess:
l1o_idx, device = 2, DEVICE0 #TODO set sample number to l1o_idx
train_cvae(l1o_idx, top_out_path, device, config)
else:
for batch in range(11): # each "batch" of sample nums maning training *seperately* for 4 samples simutaniously
processes = []
for l1o_idx, device in enumerate([DEVICE0, DEVICE0, DEVICE1, DEVICE1]):
print("l1o_idx", (batch*4)+l1o_idx)
p = mp.Process(target=train_cvae, args=((batch*4)+l1o_idx, top_out_path, device, config))
p.start()
processes.append(p)
for p in processes:
p.join()
print("Finished")