-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate.py
More file actions
290 lines (217 loc) · 11.4 KB
/
evaluate.py
File metadata and controls
290 lines (217 loc) · 11.4 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
281
282
283
284
285
286
287
288
289
290
import os
import torch
import numpy as np
import argparse
import joblib
import math
import pandas as pd
from tqdm import tqdm
from omegaconf import OmegaConf
from einops import rearrange
from scipy.spatial.distance import jensenshannon
from eval import build_model, MODEL2BATCHSIZE, DATASET_CONFIG, DATA_CONFIG, VOXEL_SIZE
from eval.metric_utils import compute_batch_logits, volume_sum_update, voxelize_pcd, compute_pairwise_cd_batch
from eval.fid_score import calculate_frechet_distance
MODEL2METRICS = {
'rangevit_wo_i': 'FRID',
'rangevit_w_i': 'FRD',
'minkowskinet': 'FSVD',
'spvcnn': 'FPVD',
'liploc': 'FLD',
}
def save(results, results_path, latex_path):
results = results.copy()
results.to_csv(results_path, index=False)
print(f"Results saved to {results_path}")
if os.path.exists(latex_path):
os.remove(latex_path)
results.drop(columns=['samples path'], inplace=True)
model_col = results.columns[0]
metric_cols = results.columns[1:]
exponents = {}
scaled_cols = {}
for col in metric_cols:
col_vals = results[col].astype(float)
col_vals_nonzero = col_vals.replace(0, np.nan).dropna()
if len(col_vals_nonzero) == 0:
exp = 0
else:
exp = int(np.floor(np.log10(np.abs(col_vals_nonzero)).min()))
exponents[col] = exp
scaled_cols[col] = col_vals / (10.0 ** exp)
scaled_df = results.copy()
for col in metric_cols:
scaled_df[col] = scaled_cols[col]
header_main = [model_col] + [col for col in metric_cols]
header_exp = [""] + [f"$\\times 10^{{{exponents[col]}}}$" for col in metric_cols]
def fmt(x):
if isinstance(x, float):
return f"{x:.3f}"
return x
formatted = scaled_df.applymap(fmt)
with open(latex_path, 'w') as f:
f.write("\\begin{table}[htbp]\n")
f.write("\\centering\n\\small\n")
f.write("\\begin{tabular}{l" + "c" * len(metric_cols) + "}\n")
f.write("\\toprule\n")
# Write column names
f.write(" & ".join(header_main) + " \\\\\n")
f.write(" & ".join(header_exp) + " \\\\\n")
f.write("\\midrule\n")
# Write data rows
for row in formatted.itertuples(index=False):
f.write(" & ".join(str(cell) for cell in row) + " \\\\\n")
f.write("\\bottomrule\n")
f.write("\\end{tabular}\n")
f.write("\\caption{State-of-the-art comparison with per-row scaled metrics.}\n")
f.write("\\label{tab:sota_comparison}\n")
f.write("\\end{table}\n")
print(f"LaTeX table saved to {latex_path}")
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
models = ['minkowskinet', 'spvcnn', 'liploc', 'rangevit_wo_i', 'rangevit_w_i']
#Parse command line arguments
parser = argparse.ArgumentParser(description="Evaluation script for Repa3D")
parser.add_argument('--config-path', type=str, required=True, help='Path to the configuration file')
parser.add_argument('--sanity-check', action='store_true', help='Perform sanity check on the samples')
args = parser.parse_args()
config = OmegaConf.load(args.config_path)
model_names = config.models.keys()
# Dataframe to store results
results_all = pd.DataFrame(columns=['Method', 'FRD', 'FRID', 'FLD', 'FSVD', 'FPVD', 'JSD', 'TV', 'MMD', 'samples path'])
results_val = pd.DataFrame(columns=['Method', 'FRD', 'FRID', 'FLD', 'FSVD', 'FPVD', 'JSD', 'TV', 'MMD', 'samples path'])
time_stamp = pd.Timestamp.now().strftime("%Y%m%d_%H%M%S")
models_string = "_".join(list(config.models.keys()))
results_all_path = os.path.join(config.save_path, "trainval", f'eval_{time_stamp}_{models_string}.csv')
results_val_path = os.path.join(config.save_path, "valonly", f'eval_{time_stamp}_{models_string}.csv')
latex_all_path = os.path.join(config.save_path, "trainval", f'eval_{time_stamp}_{models_string}.tex')
latex_val_path = os.path.join(config.save_path, "valonly", f'eval_{time_stamp}_{models_string}.tex')
# Load the dataset statistics
dataset_stats = np.load(config.dataset_stat_path, allow_pickle=True).item()
for model_name in config.models.keys():
sample_path = config.models[model_name].samples_path
metrics = config.models[model_name].metrics
print(f"Evaluating model {model_name} with samples from {sample_path}")
results_all_row = {'Method': model_name, 'samples path': sample_path}
results_val_row = {'Method': model_name, 'samples path': sample_path}
# Load the model and samples
if sample_path.endswith('.npz'):
all_imgs = np.load(sample_path)['arr_0']
if all_imgs.ndim == 3:
all_imgs = np.expand_dims(all_imgs, axis=1)
depth = rearrange(all_imgs[:, 0],'b w h -> b (w h)')
depth_range = DATASET_CONFIG['kitti']['depth_range']
mask = (depth > depth_range[0]) & (depth < depth_range[1])
if all_imgs.shape[1] < 4:
pcd_path = sample_path.replace('img.npz', 'samples.pcd')
all_pcds = joblib.load(pcd_path)
else:
pcd = rearrange(all_imgs[:, 1:4],'b c w h -> b (w h) c') #xyz
all_pcds = [pcd[i][mask[i]] for i in range(pcd.shape[0])]
mask = rearrange(mask,'b (h w) -> b h w',w=all_imgs.shape[-1])
depth = all_imgs[:, 0]*mask - (1-mask)
print(depth.min(), depth.max())
intensity = all_imgs[:, -1]*mask
all_range_imgs = np.concatenate([depth[:,None], intensity[:,None]], axis=1) # depth and intensity
#remove from ram all_imgs
del all_imgs
del mask
del depth
del intensity
if sample_path.endswith('.pcd'):
all_pcds = joblib.load(sample_path)
all_range_imgs = [0]*len(all_pcds) # Placeholder for range images
if args.sanity_check:
all_pcds = all_pcds[:10]
all_range_imgs = all_range_imgs[:10]
else:
assert len(all_pcds) >= 10000, "Prepare 10000 samples before evaluation"
all_pcds = all_pcds[:10000]
all_range_imgs = all_range_imgs[:10000]
#Compute Metrics
for m in models:
if m not in metrics:
print(f"Model {m} not in metrics for {model_name}, skipping...")
continue
is_voxel = m in ['minkowskinet', 'spvcnn']
bs = MODEL2BATCHSIZE[m]
print(f"loading model {m}...")
model = build_model('kitti',m, device)
print(f"model {m} loaded.")
print(f"Computing logits of {model_name} with model {m}...")
all_logits_list = []
if is_voxel:
for i in range(math.ceil(len(all_pcds) / bs)):
batch = all_pcds[i * bs:(i + 1) * bs]
logits = compute_batch_logits(batch, model, is_voxel=is_voxel, dataset_config=DATASET_CONFIG['kitti'])
all_logits_list.extend(logits)
all_logits = np.stack(all_logits_list)
else:
for i in range(math.ceil(len(all_range_imgs) / bs)):
batch = all_range_imgs[i * bs:(i + 1) * bs]
logits = compute_batch_logits(batch, model, is_voxel=is_voxel, dataset_config=DATASET_CONFIG['kitti'])
all_logits_list.append(logits)
all_logits = np.vstack(all_logits_list)
mu = np.mean(all_logits, axis=0)
sigma = np.cov(all_logits, rowvar=False)
mu_all_dataset = dataset_stats['all'][m]['mu']
sigma_all_dataset = dataset_stats['all'][m]['sigma']
mu_val_dataset = dataset_stats['val'][m]['mu']
sigma_val_dataset = dataset_stats['val'][m]['sigma']
fid_all = calculate_frechet_distance(mu, sigma, mu_all_dataset, sigma_all_dataset)
fid_val = calculate_frechet_distance(mu, sigma, mu_val_dataset, sigma_val_dataset)
# Store the results in the dataframe
results_all_row[MODEL2METRICS[m]] = fid_all
results_val_row[MODEL2METRICS[m]] = fid_val
print(f"###--- Computed {MODEL2METRICS[m]}: {fid_all:.4f} for model {model_name} ---###")
x_range, y_range = DATA_CONFIG['64']['x'], DATA_CONFIG['64']['y']
vol_shape = (math.ceil((x_range[1] - x_range[0]) / VOXEL_SIZE), math.ceil((y_range[1] - y_range[0]) / VOXEL_SIZE))
min_bound = (math.ceil((x_range[0]) / VOXEL_SIZE), math.ceil((y_range[0]) / VOXEL_SIZE))
#Compute voxelized point cloud bev
if 'mmd' in metrics:
print("Computing dataset histogram...")
all_pcds_hists = []
for pcd in all_pcds[:2000]:
pcd_voxel = voxelize_pcd(pcd, x_range, y_range, VOXEL_SIZE, min_bound, vol_shape)
all_pcds_hists.append(pcd_voxel)
print("Dataset histogram computed.")
print("Computing Maximum Mean Discrepancy (MMD)...")
val_histograms = dataset_stats['val']['valset_voxelized_bev']['points']
val_histograms = val_histograms[:1000] # Use a subset of 1000 samples for MMD computation
if args.sanity_check:
mmd = 0.0
else:
results_dist = []
for r in tqdm(val_histograms):
dists = compute_pairwise_cd_batch(r, all_pcds_hists)
results_dist.append(min(dists))
mmd = sum(results_dist) / len(results_dist)
results_all_row['MMD'] = mmd
results_val_row['MMD'] = mmd
print(f"###--- Computed MMD: {mmd:.4f} for model {model_name} ---###")
## Compute bev histogram ##
if 'jsd' in metrics or 'tv' in metrics:
print("Computing histograms...")
volume_sum = np.zeros(vol_shape, np.float32)
for pcd in all_pcds:
volume_sum = volume_sum_update(volume_sum, pcd, x_range, y_range, VOXEL_SIZE, min_bound)
all_dataset_volume_sum = dataset_stats['all']['dataset_bev_hist']
val_dataset_volume_sum = dataset_stats['val']['dataset_bev_hist']
print("Validation computed.")
all_dataset_volume_sum = (all_dataset_volume_sum / np.sum(all_dataset_volume_sum)).flatten()
val_dataset_volume_sum = (val_dataset_volume_sum / np.sum(val_dataset_volume_sum)).flatten()
volume_sum = (volume_sum / np.sum(volume_sum)).flatten()
jsd_all = jensenshannon(all_dataset_volume_sum, volume_sum)
tv_all = np.abs(all_dataset_volume_sum - volume_sum).sum()
jsd_val = jensenshannon(val_dataset_volume_sum, volume_sum)
tv_val = np.abs(val_dataset_volume_sum - volume_sum).sum()
results_all_row['JSD'] = jsd_all
results_all_row['TV'] = tv_all
results_val_row['JSD'] = jsd_val
results_val_row['TV'] = tv_val
print(f"###--- Computed JSD: {jsd_all:.4f} for model {model_name} ---###")
print(f"###--- Computed TV: {tv_all:.4f} for model {model_name} ---###")
results_all.loc[len(results_all)] = results_all_row
results_val.loc[len(results_val)] = results_val_row
save(results_all,results_all_path, latex_all_path)
save(results_val,results_val_path, latex_val_path)