-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
280 lines (220 loc) · 10 KB
/
eval.py
File metadata and controls
280 lines (220 loc) · 10 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
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
import os
import numpy as np
import torch
import torch.nn.functional as F
from loguru import logger
from scipy.stats import kendalltau
import utils
import wandb
from datasets import batch_chunk_image, get_test_loader
@torch.inference_mode()
def eval_image_dataset(config, ckpt_dir):
model = utils.init_model(config)
model.load_state_dict(torch.load(f"{ckpt_dir}/{config.train.run_name}.pth"))
if config.train.record_wandb and config.eval_only:
model.train()
total_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
wandb.run.summary["total_params"] = total_params
model.eval()
diffusion_utils = utils.init_diffusion_utils(config)
test_loader = get_test_loader(config)
kendall_taus = []
mean_mse = []
mean_rmse = []
mean_l1 = []
l1_loss = torch.nn.L1Loss()
perm_all_correct = 0
perm_piece_correct = 0
image_all_correct = 0
image_piece_correct = 0
total_images = 0
total_pieces = 0
for i, data in enumerate(test_loader):
logger.info(f"Mini-batch {i}")
if config.dataset == "unscramble-noisy-MNIST" or config.dataset == "sort-MNIST":
random_pieces, gt_perm_list = data
random_pieces, gt_perm_list = random_pieces.cuda(), gt_perm_list.cuda()
pieces = utils.permute_image_perm_list(gt_perm_list, random_pieces)
elif config.dataset == "unscramble-CIFAR10":
inputs, _ = data
pieces, random_pieces, gt_perm_list = batch_chunk_image(inputs, config.num_pieces)
pieces, random_pieces, gt_perm_list = (
pieces.cuda(),
random_pieces.cuda(),
gt_perm_list.cuda(),
)
gt_perm_list = torch.argsort(gt_perm_list)
else:
raise NotImplementedError
if config.beam_search:
ordered_pieces, predicted_perm_list = diffusion_utils.p_sample_beam_search(
random_pieces, model
)
else:
ordered_pieces, predicted_perm_list = diffusion_utils.p_sample_loop(
random_pieces, model, deterministic=True
)
# Obtain the Kendall-Tau correlation coefficient for the target
# and predicted list of permutation matrices.
for p1, p2 in zip(gt_perm_list, predicted_perm_list):
p1, p2 = p1.cpu(), p2.cpu()
kendall_taus.append(kendalltau(p1, p2)[0])
perm_all_correct += torch.equal(p1, p2)
perm_piece_correct += torch.eq(p1, p2).sum().item()
total_images += gt_perm_list.size(0)
total_pieces += torch.numel(gt_perm_list)
compare_images = ~torch.isclose(
pieces.flatten(start_dim=2), ordered_pieces.flatten(start_dim=2)
) # shape [B, num_piecces**2, num_pixels]
compare_result = compare_images.sum(dim=(1, 2)) == 0
correct_images = compare_result.sum()
correct_image_pieces = (compare_images.sum(-1) == 0).sum()
image_all_correct += correct_images.item()
image_piece_correct += correct_image_pieces.item()
mse = F.mse_loss(pieces, ordered_pieces, reduction="mean") # \sqrt( / bchw)
mean_mse.append(mse.cpu())
rmse = mse.sqrt()
mean_rmse.append(rmse.cpu())
l1 = l1_loss(pieces, ordered_pieces) # mean absolute error
mean_l1.append(l1.cpu())
mean_kendall_tau = np.mean(kendall_taus)
mean_mse = torch.stack(mean_mse).mean()
mean_rmse = torch.stack(mean_rmse).mean()
mean_l1 = torch.stack(mean_l1).mean()
perm_accuracy = perm_all_correct / total_images
perm_prop_correct_pieces = perm_piece_correct / total_pieces
image_accuracy = image_all_correct / total_images
image_prop_correct_pieces = image_piece_correct / total_pieces
if config.train.record_wandb:
wandb.run.summary["mean_kendall_tau"] = mean_kendall_tau
wandb.run.summary["mean_mse"] = mean_mse
wandb.run.summary["mean_rmse"] = mean_rmse
wandb.run.summary["mean_l1"] = mean_l1
wandb.run.summary["permutation_accuracy"] = perm_accuracy
wandb.run.summary["permutation_prop_correct_pieces"] = perm_prop_correct_pieces
wandb.run.summary["pixel_wise_accuracy"] = image_accuracy
wandb.run.summary["pixel_wise_prop_correct_pieces"] = image_prop_correct_pieces
wandb.finish()
logger.info(f"Mean Kendall-Tau: {mean_kendall_tau}")
logger.info(f"Mean mse: {mean_mse}")
logger.info(f"Mean root mse: {mean_rmse}")
logger.info(f"Mean l1 loss: {mean_l1}")
logger.info(f"Permutation Accuracy: {perm_accuracy}")
logger.info(f"Permutation Prop. of correct pieces: {perm_prop_correct_pieces}")
logger.info(f"Pixel-wise Accuracy: {image_accuracy}")
logger.info(f"Pixel-wise Prop. of correct pieces: {image_prop_correct_pieces}")
@torch.inference_mode()
def validate(config, model=None, ckpt_dir="./saved_models"):
if config.dataset == "tsp":
validate_tsp(config, model, ckpt_dir)
@torch.inference_mode()
def validate_tsp(config, model, ckpt_dir="./saved_models"):
if model is None:
local_rank = int(os.environ["LOCAL_RANK"])
model = utils.init_model(config)
ckpt = torch.load(
f"{ckpt_dir}/ckpt_{config.train.run_name}.pth", map_location=f"cuda:{local_rank}"
)
model.load_state_dict(ckpt["model_state_dict"])
model.eval()
diffusion_utils = utils.init_diffusion_utils(config)
test_loader = get_test_loader(config)
logger.info(f"ON TEST SET:")
mean_predicted_tour_length = []
mean_gap = []
mean_gt = []
total_pieces = len(test_loader) * config.eval_batch_size * config.num_pieces
correct_pieces = 0
for i, data in enumerate(test_loader):
_, random_pieces, __, tour = data # random_pieces: points [batch, n, 2]; tour: [batch, n+1]
random_pieces, tour = random_pieces.cuda(), tour.cuda()
evaluator = utils.TSPEvaluator(random_pieces)
gt_tour_length = evaluator.evaluate(tour, tour_is_cycle=True)
if config.beam_search:
ordered_pieces, predicted_perm_list = diffusion_utils.p_sample_beam_search(
random_pieces, model
)
else:
ordered_pieces, predicted_perm_list = diffusion_utils.p_sample_loop(
random_pieces, model, deterministic=True
)
if i == 0:
logger.info(f"gt tour (first graph in batch): {tour[0, :-1]}")
logger.info(f"predicted tour (first graph in batch): {predicted_perm_list[0]}")
correct_pieces += (tour[:, :-1] == predicted_perm_list).sum().cpu().item()
predicted_tour_length = evaluator.evaluate(predicted_perm_list)
gap = (predicted_tour_length - gt_tour_length) / gt_tour_length
mean_predicted_tour_length.append(predicted_tour_length.cpu())
mean_gap.append(gap.cpu())
mean_gt.append(gt_tour_length.cpu())
mean_predicted_tour_length = torch.stack(mean_predicted_tour_length).mean()
mean_gap = torch.stack(mean_gap).mean()
mean_gt = torch.stack(mean_gt).mean()
prop_correct_pieces = correct_pieces / total_pieces
logger.info(f"Mean Ground Truth Tour Length: {mean_gt}")
logger.info(f"Mean Predicted Tour Length: {mean_predicted_tour_length}")
logger.info(f"Mean Gap: {mean_gap}")
logger.info(f"Prop. of Correct Pieces: {prop_correct_pieces}")
if config.train.record_wandb:
wandb.log({"tour_len": mean_predicted_tour_length})
@torch.inference_mode()
def eval_tsp(config, ckpt_dir, model=None):
if model is None:
local_rank = int(os.environ["LOCAL_RANK"])
model = utils.init_model(config)
model.load_state_dict(
torch.load(f"{ckpt_dir}/{config.train.run_name}.pth", map_location=f"cuda:{local_rank}")
)
model.eval()
diffusion_utils = utils.init_diffusion_utils(config)
test_loader = get_test_loader(config)
mean_predicted_tour_length = []
mean_gap = []
mean_gt = []
for i, data in enumerate(test_loader):
logger.info(f"Mini-batch {i}")
_, random_pieces, __, tour = data # random_pieces: points [batch, n, 2]; tour: [batch, n+1]
random_pieces, tour = random_pieces.cuda(), tour.cuda()
evaluator = utils.TSPEvaluator(random_pieces)
gt_tour_length = evaluator.evaluate(tour, tour_is_cycle=True)
if config.beam_search:
ordered_pieces, predicted_perm_list = diffusion_utils.p_sample_beam_search(
random_pieces, model
)
else:
ordered_pieces, predicted_perm_list = diffusion_utils.p_sample_loop(
random_pieces, model, deterministic=True
)
predicted_tour_length = evaluator.evaluate(predicted_perm_list)
gap = (predicted_tour_length - gt_tour_length) / gt_tour_length
mean_predicted_tour_length.append(predicted_tour_length.cpu())
mean_gap.append(gap.cpu())
mean_gt.append(gt_tour_length.cpu())
mean_predicted_tour_length = torch.stack(mean_predicted_tour_length).mean()
mean_gap = torch.stack(mean_gap).mean()
mean_gt = torch.stack(mean_gt).mean()
logger.info(f"Mean Ground Truth Tour Length: {mean_gt}")
logger.info(f"Mean Predicted Tour Length: {mean_predicted_tour_length}")
logger.info(f"Mean Gap: {mean_gap}")
if config.train.record_wandb:
wandb.run.summary["mean_gt_tour_length"] = mean_gt
wandb.run.summary["mean_predicted_tour_length"] = mean_predicted_tour_length
wandb.run.summary["mean_gap"] = mean_gap
wandb.finish()
@torch.inference_mode()
def eval(config, ckpt_dir):
if config.train.record_wandb and config.eval_only:
project_name = config.dataset
wandb.init(
project=project_name, name=f"EVAL_{config.train.run_name}", config=config.to_dict()
)
if config.dataset in [
"unscramble-noisy-MNIST",
"sort-MNIST",
"unscramble-CIFAR10",
]:
eval_image_dataset(config, ckpt_dir)
elif config.dataset == "tsp":
eval_tsp(config, ckpt_dir)
else:
raise NotImplementedError