-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate.py
200 lines (172 loc) · 9.12 KB
/
validate.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
import torch
from models.masks import ParticleMask, SpecificParticleMask, KinematicMask
import json
from utils import parse_model_name
import os
# Validation loop
def validate(val_loader, models, device, criterion, model_type, output_vars, mask, epoch, num_epochs, loss_min, save_path, model_name):
# Create a config checkpoint file
config = parse_model_name(model_name)
if model_type == 'autoencoder':
dir_name = './outputs/' + model_name
if not os.path.exists(dir_name):
os.makedirs(dir_name)
if not os.path.exists(dir_name + '/ckpt_config.json'):
with open(dir_name + '/ckpt_config.json', 'w') as f:
json.dump(config, f, indent=4)
tae = models[0]
tae.eval() # Set the tae to evaluation mode
losses = []
with torch.no_grad(): # Disable gradient calculations
for val_batch in val_loader:
# Move the data to the device
inputs, _ = val_batch
inputs = inputs.to(device)
if mask is not None:
if mask == 0:
mask_layer = ParticleMask(output_vars+(output_vars%3))
else:
mask_layer = KinematicMask(mask)
# Mask input data
masked_inputs = mask_layer(inputs)
# Forward pass
outputs = tae(masked_inputs)
outputs = torch.reshape(outputs, (outputs.size(0),
outputs.size(1) * outputs.size(2)))
# Flatten last axes
if output_vars == 3:
inputs = inputs[:,:,:-1]
inputs = torch.reshape(inputs, (inputs.size(0),
inputs.size(1) * inputs.size(2)))
loss = criterion.compute_loss(outputs, inputs, zero_padded=[4])
elif output_vars == 4:
inputs = torch.reshape(inputs, (inputs.size(0),
inputs.size(1) * inputs.size(2)))
loss = criterion.compute_loss(outputs, inputs, zero_padded=[3,6,8])
losses.append(loss.item())
loss_mean = sum(losses) / len(losses)
print(f"Epoch [{epoch+1}/{num_epochs}], Val Loss: {loss_mean:.4f}")
# Save files if better than best performance
if loss_mean < loss_min:
loss_min = loss_mean
torch.save(tae.state_dict(), save_path + '/TAE_best_' + model_name)
# Update the checkpoint file
with open('./outputs/' + model_name + '/ckpt_config.json', 'r') as f:
config = json.load(f)
config['ae_resume_epoch'] = epoch + 1
with open('./outputs/' + model_name + '/ckpt_config.json', 'w') as f:
json.dump(config, f, indent=4)
return loss_min
elif model_type == 'classifier partial':
dir_name = './outputs/' + model_name
if not os.path.exists(dir_name):
os.makedirs(dir_name)
if not os.path.exists(dir_name + '/ckpt_config.json'):
with open(dir_name + '/ckpt_config.json', 'w') as f:
json.dump(config, f, indent=4)
tae, classifier = models[0], models[1]
# Validation loop
tae.eval() # Set the tae to evaluation mode
classifier.eval()
losses = []
with torch.no_grad(): # Disable gradient calculations
for val_batch in val_loader:
# Move the data to the device
inputs, labels = val_batch
inputs = inputs.to(device)
labels = labels.to(device)
if mask is not None:
if mask == 0:
mask_layer = ParticleMask(output_vars+(output_vars%3))
else:
mask_layer = KinematicMask(mask)
# Mask input data
masked_inputs = mask_layer(inputs)
# Forward pass
outputs = tae(masked_inputs)
# Reset trivial values
mask_999 = (masked_inputs[:, :, 3] == 999).float()
outputs[:,:,3:5] = torch.nn.functional.softmax(outputs[:,:,3:5], dim=2)
outputs[:, :, 3] = (1 - mask_999) * outputs[:, :, 3] + mask_999 * 1
outputs[:, :, 4] = (1 - mask_999) * outputs[:, :, 4]
masked_inputs[:,:,3:5] = torch.nn.functional.softmax(masked_inputs[:,:,3:5], dim=2)
masked_inputs[:, :, 3] = (1 - mask_999) * masked_inputs[:, :, 3] + mask_999 * 1
masked_inputs[:, :, 4] = (1 - mask_999) * masked_inputs[:, :, 4]
outputs = torch.reshape(outputs, (outputs.size(0),
outputs.size(1) * outputs.size(2)))
masked_inputs = torch.reshape(masked_inputs, (masked_inputs.size(0),
masked_inputs.size(1) * masked_inputs.size(2)))
# Concat encodings and masked inputs
outputs_2 = classifier(torch.cat((outputs, masked_inputs), axis=1)).squeeze(1)
loss = criterion(outputs_2, labels.float())
losses.append(loss.item())
loss_mean = sum(losses) / len(losses)
print(f"Epoch [{epoch+1}/{num_epochs}], Val Loss: {loss_mean:.4f}")
# Save files if better than best performance
if loss_mean < loss_min:
loss_min = loss_mean
torch.save(classifier.state_dict(), save_path + '/Classifier_partial_best_' + model_name)
# Update the checkpoint file
with open('./outputs/' + model_name + '/ckpt_config.json', 'r') as f:
config = json.load(f)
config['pc_resume_epoch'] = epoch + 1
with open('./outputs/' + model_name + '/ckpt_config.json', 'w') as f:
json.dump(config, f, indent=4)
return loss_min
elif model_type == 'classifier full':
dir_name = './outputs/' + model_name
if not os.path.exists(dir_name):
os.makedirs(dir_name)
if not os.path.exists(dir_name + '/ckpt_config.json'):
with open(dir_name + '/ckpt_config.json', 'w') as f:
json.dump(config, f, indent=4)
tae, classifier = models[0], models[1]
# Validation loop
tae.eval() # Set the tae to evaluation mode
classifier.eval()
losses = []
with torch.no_grad(): # Disable gradient calculations
for val_batch in val_loader:
# Move the data to the device
inputs, labels = val_batch
inputs = inputs.to(device)
labels = labels.to(device)
outputs = torch.zeros(inputs.size(0), 6, output_vars+(output_vars%3)).to(device)
for i in range(6):
if mask is not None:
if mask == 0:
mask_layer = SpecificParticleMask(output_vars+(output_vars%3), i)
else:
mask_layer = KinematicMask(mask)
# Mask input data
masked_inputs = mask_layer(inputs)
# Forward pass for autoencoder
temp_outputs = tae(masked_inputs)
outputs[:,i,:] = temp_outputs[:,i,:]
# Reset trivial values
mask_999 = (masked_inputs[:, :, 3] == 999).float()
outputs[:,:,3:5] = torch.nn.functional.softmax(outputs[:,:,3:5], dim=2)
outputs[:, :, 3] = (1 - mask_999) * outputs[:, :, 3] + mask_999 * 1
outputs[:, :, 4] = (1 - mask_999) * outputs[:, :, 4]
# Flatten last axis
outputs = torch.reshape(outputs, (outputs.size(0),
outputs.size(1) * outputs.size(2)))
inputs = torch.reshape(inputs, (inputs.size(0),
inputs.size(1) * inputs.size(2)))
# Concat encodings and inputs
outputs_2 = classifier(torch.cat((outputs, inputs), axis=1)).squeeze(1)
loss = criterion(outputs_2, labels.float())
losses.append(loss.item())
loss_mean = sum(losses) / len(losses)
print(f"Epoch [{epoch+1}/{num_epochs}], Val Loss: {loss_mean:.4f}")
# Save files if better than best performance
if loss_mean < loss_min:
loss_min = loss_mean
torch.save(classifier.state_dict(), save_path + '/Classifier_full_best_' + model_name)
# Update the checkpoint file
with open('./outputs/' + model_name + '/ckpt_config.json', 'r') as f:
config = json.load(f)
config['fc_resume_epoch'] = epoch + 1
with open('./outputs/' + model_name + '/ckpt_config.json', 'w') as f:
json.dump(config, f, indent=4)
return loss_min