-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvisualize_metrics.py
More file actions
548 lines (453 loc) · 20.2 KB
/
visualize_metrics.py
File metadata and controls
548 lines (453 loc) · 20.2 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import json
import matplotlib.pyplot as plt
import numpy as np
import re
import os
def load_evaluation_data(json_path):
"""Load evaluation data from a JSON file."""
with open(json_path, 'r') as f:
return json.load(f)
def visualize_metrics_over_steps(evaluation_data, output_path=None, task_name='coco',
figsize=(18, 6), selected_metrics=None):
"""
Plot metrics, diversity and skewness over steps for all runs.
Args:
evaluation_data (dict): The loaded JSON data
output_path (str): Path to save the output plot
task_name (str): Name of the task to plot ('coco' or 'SGP-Single-9k')
figsize (tuple): Base figure size per run
selected_metrics (list): List of specific metrics to plot, None plots all
"""
# Count number of runs
runs = list(evaluation_data.keys())
n_runs = len(runs)
if n_runs == 0:
print("No runs found in the data.")
return
# Create figure with proper size
fig, axes = plt.subplots(n_runs, 3, figsize=(figsize[0], figsize[1] * n_runs))
# If there's only one run, wrap axes in a list to maintain indexing consistency
if n_runs == 1:
axes = [axes]
# Process each run
for i, run_name in enumerate(runs):
run_data = evaluation_data[run_name]
# Extract step numbers and sort them
steps = list(run_data.keys())
step_nums = [int(re.search(r'step_(\d+)', step).group(1)) for step in steps]
pairs = sorted(zip(step_nums, steps))
sorted_step_nums = [pair[0] for pair in pairs]
sorted_steps = [pair[1] for pair in pairs]
# Data containers
metrics_data = {}
diversity_data = {}
skewness_data = {}
# Extract data for each step
for step, step_num in zip(sorted_steps, sorted_step_nums):
if task_name in run_data[step]:
step_data = run_data[step][task_name]
# Extract metrics
for metric_name, value in step_data.get('metrics', {}).items():
if selected_metrics is None or metric_name in selected_metrics:
if metric_name not in metrics_data:
metrics_data[metric_name] = {'values': [], 'steps': []}
metrics_data[metric_name]['values'].append(value)
metrics_data[metric_name]['steps'].append(step_num)
# Extract diversity
for div_name, value in step_data.get('diversity', {}).items():
if div_name not in diversity_data:
diversity_data[div_name] = {'values': [], 'steps': []}
diversity_data[div_name]['values'].append(value)
diversity_data[div_name]['steps'].append(step_num)
# Extract skewness
for skew_name, value in step_data.get('skewness', {}).items():
if selected_metrics is None or skew_name in selected_metrics:
if skew_name not in skewness_data:
skewness_data[skew_name] = {'values': [], 'steps': []}
skewness_data[skew_name]['values'].append(value)
skewness_data[skew_name]['steps'].append(step_num)
# Plot metrics
for metric_name, data in metrics_data.items():
axes[i][0].plot(data['steps'], data['values'], marker='o', label=metric_name)
axes[i][0].set_title(f"{run_name}: Metrics ({task_name})")
axes[i][0].set_xlabel("Step")
axes[i][0].set_ylabel("Value")
axes[i][0].grid(True, alpha=0.3)
if metrics_data:
axes[i][0].legend(loc='best', fontsize='small')
# Plot diversity
for div_name, data in diversity_data.items():
axes[i][1].plot(data['steps'], data['values'], marker='o', label=div_name)
axes[i][1].set_title(f"{run_name}: Diversity ({task_name})")
axes[i][1].set_xlabel("Step")
axes[i][1].set_ylabel("Value")
axes[i][1].grid(True, alpha=0.3)
if diversity_data:
axes[i][1].legend(loc='best', fontsize='small')
# Plot skewness
for skew_name, data in skewness_data.items():
axes[i][2].plot(data['steps'], data['values'], marker='o', label=skew_name)
axes[i][2].set_title(f"{run_name}: Skewness ({task_name})")
axes[i][2].set_xlabel("Step")
axes[i][2].set_ylabel("Value")
axes[i][2].grid(True, alpha=0.3)
if skewness_data:
axes[i][2].legend(loc='best', fontsize='small')
# Adjust layout
plt.tight_layout()
# Save or show the plot
if output_path:
plt.savefig(output_path, dpi=300, bbox_inches='tight')
print(f"Plot saved to {output_path}")
else:
plt.show()
return fig
def test_visualize_metrics_over_steps():
"""
# Basic usage - visualizes coco task metrics
python visualize_metrics.py --input evaluation_summary.json --output metrics_visualization.png
# Visualize SGP-Single-9k task
python visualize_metrics.py --task SGP-Single-9k --output sgp_metrics_visualization.png
# Only plot specific metrics
python visualize_metrics.py --metrics clip_small clip_large siglip_small
# Customize figure size
python visualize_metrics.py --figsize-width 20 --figsize-height 8
"""
import argparse
parser = argparse.ArgumentParser(description='Visualize evaluation metrics over steps')
parser.add_argument('--input', default='evaluation_summary.json',
help='Input JSON file containing evaluation data')
parser.add_argument('--output', default='metrics_visualization.png',
help='Output path for the visualization')
parser.add_argument('--task', default='coco', choices=['coco', 'SGP-Single-9k'],
help='Task to visualize metrics for')
parser.add_argument('--figsize-width', type=int, default=18,
help='Figure width')
parser.add_argument('--figsize-height', type=int, default=6,
help='Figure height per run')
parser.add_argument('--metrics', nargs='+',
help='Specific metrics to plot (e.g., clip_small clip_large)')
args = parser.parse_args()
# Load data
data = load_evaluation_data(args.input)
# Generate visualization
visualize_metrics_over_steps(
data,
output_path=args.output,
task_name=args.task,
figsize=(args.figsize_width, args.figsize_height),
selected_metrics=args.metrics
)
def print_step_as_csv(evaluation_data, step="step_00750", task_name='coco', print_skewness=False):
"""
Print data for a specific step in CSV format with 4 decimal precision.
Also calculates and includes average metrics.
Args:
evaluation_data (dict): The loaded JSON data
step (str): Step name to extract (e.g., "step_00750")
task_name (str): Name of the task to print data for
print_skewness (bool): If True, also include skewness values in output
"""
# Define ordered metrics to display
ordered_metrics = [
"clip_small",
"clip_large",
"siglip_small",
"siglip_large",
"dino_small",
"dino_base",
"dino_large",
"dino_giant",
"reward",
]
# Define metric groupings for averages
clip_metrics = ["clip_small", "clip_large"]
siglip_metrics = ["siglip_small", "siglip_large"]
dino_metrics = ["dino_small", "dino_base", "dino_large", "dino_giant"]
# Create column headers
header = ["run_name"]
# Add ordered metric columns
for metric in ordered_metrics:
header.append(f"metric_{metric}")
# Add calculated average columns
header.append("metric_clip_avg")
header.append("metric_siglip_avg")
header.append("metric_dino_avg")
header.append("metric_avg")
# Add just average diversity
header.append("diversity_average")
# Add skewness columns if requested
if print_skewness:
for metric in ordered_metrics:
header.append(f"skewness_{metric}")
# Add other standard columns
header.extend(["valid_count", "total_count", "success_rate"])
# Print header
print(','.join(header))
# Print data for each run
for run_name, run_data in evaluation_data.items():
if step not in run_data or task_name not in run_data[step]:
continue
step_data = run_data[step][task_name]
row = [run_name]
# Store metric values for average calculations
metric_values = {}
# Add metrics in specified order
for metric in ordered_metrics:
if 'metrics' in step_data and metric in step_data['metrics']:
value = step_data['metrics'][metric]
metric_values[metric] = value
# Format floating point values to 4 decimal places
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Calculate and add clip_avg
if all(m in metric_values for m in clip_metrics):
clip_avg = sum(metric_values[m] for m in clip_metrics) / len(clip_metrics)
row.append(f"{clip_avg:.4f}")
else:
row.append("")
# Calculate and add siglip_avg
if all(m in metric_values for m in siglip_metrics):
siglip_avg = sum(metric_values[m] for m in siglip_metrics) / len(siglip_metrics)
row.append(f"{siglip_avg:.4f}")
else:
row.append("")
# Calculate and add dino_avg
if all(m in metric_values for m in dino_metrics):
dino_avg = sum(metric_values[m] for m in dino_metrics) / len(dino_metrics)
row.append(f"{dino_avg:.4f}")
else:
row.append("")
# Calculate and add overall average
if metric_values:
overall_avg = sum(metric_values.values()) / len(metric_values)
row.append(f"{overall_avg:.4f}")
else:
row.append("")
# Add only average diversity
if 'diversity' in step_data and 'average' in step_data['diversity']:
value = step_data['diversity']['average']
# Format floating point values to 4 decimal places
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Add skewness values if requested
if print_skewness:
for metric in ordered_metrics:
if 'skewness' in step_data and metric in step_data['skewness']:
value = step_data['skewness'][metric]
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Add standard columns
if 'valid_count' in step_data:
row.append(str(step_data['valid_count'])) # Integer, no formatting
else:
row.append("")
if 'total_count' in step_data:
row.append(str(step_data['total_count'])) # Integer, no formatting
else:
row.append("")
if 'success_rate' in step_data:
value = step_data['success_rate']
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Print row
print(','.join(row))
def test_print_step_as_csv():
import argparse
parser = argparse.ArgumentParser(description='Visualize evaluation metrics over steps')
parser.add_argument('--input', default='evaluation_summary.json',
help='Input JSON file containing evaluation data')
parser.add_argument('--output', default='metrics_visualization.png',
help='Output path for the visualization')
parser.add_argument('--task', default='coco', choices=['coco', 'SGP-Single-9k'],
help='Task to visualize metrics for')
parser.add_argument('--figsize-width', type=int, default=18,
help='Figure width')
parser.add_argument('--figsize-height', type=int, default=6,
help='Figure height per run')
parser.add_argument('--metrics', nargs='+',
help='Specific metrics to plot (e.g., clip_small clip_large)')
parser.add_argument('--csv-only', action='store_true',
help='Only print CSV data without generating plots')
parser.add_argument('--step', default='step_00750',
help='Step to use for CSV output')
args = parser.parse_args()
# Load data
data = load_evaluation_data(args.input)
# Print CSV data for step 750
print("\n=== CSV Format Data for Step 750 ===\n")
print_step_as_csv(data, step=args.step, task_name=args.task)
# Generate visualization if not csv-only mode
if not args.csv_only:
visualize_metrics_over_steps(
data,
output_path=args.output,
task_name=args.task,
figsize=(args.figsize_width, args.figsize_height),
selected_metrics=args.metrics
)
def print_as_csv(evaluation_data, task_name='coco', print_skewness=False):
"""
Print data for all steps in CSV format with 4 decimal precision.
Also calculates and includes average metrics.
Args:
evaluation_data (dict): The loaded JSON data
task_name (str): Name of the task to print data for
print_skewness (bool): If True, also include skewness values in output
"""
# Define ordered metrics to display
ordered_metrics = [
"clip_small",
"clip_large",
"siglip_small",
"siglip_large",
"dino_small",
"dino_base",
"dino_large",
"dino_giant",
"reward",
]
# Define metric groupings for averages
clip_metrics = ["clip_small", "clip_large"]
siglip_metrics = ["siglip_small", "siglip_large"]
dino_metrics = ["dino_small", "dino_base", "dino_large", "dino_giant"]
# Create column headers
header = ["run_name", "step"] # Added step column
# Add ordered metric columns
for metric in ordered_metrics:
header.append(f"metric_{metric}")
# Add calculated average columns
header.append("metric_clip_avg")
header.append("metric_siglip_avg")
header.append("metric_dino_avg")
header.append("metric_avg")
# Add just average diversity
header.append("diversity_average")
# Add skewness columns if requested
if print_skewness:
for metric in ordered_metrics:
header.append(f"skewness_{metric}")
# Add other standard columns
header.extend(["valid_count", "total_count", "success_rate"])
# Print header
print(','.join(header))
# Process each run
for run_name, run_data in evaluation_data.items():
# Extract step numbers and sort them
steps = list(run_data.keys())
step_nums = [int(re.search(r'step_(\d+)', step).group(1)) for step in steps if re.search(r'step_(\d+)', step)]
pairs = sorted(zip(step_nums, steps))
sorted_steps = [pair[1] for pair in pairs]
# Print data for each step
for step in sorted_steps:
if task_name not in run_data[step]:
continue
step_data = run_data[step][task_name]
row = [run_name, step] # Add run name and step
# Store metric values for average calculations
metric_values = {}
# Add metrics in specified order
for metric in ordered_metrics:
if 'metrics' in step_data and metric in step_data['metrics']:
value = step_data['metrics'][metric]
metric_values[metric] = value
# Format floating point values to 4 decimal places
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Calculate and add clip_avg
if all(m in metric_values for m in clip_metrics):
clip_avg = sum(metric_values[m] for m in clip_metrics) / len(clip_metrics)
row.append(f"{clip_avg:.4f}")
else:
row.append("")
# Calculate and add siglip_avg
if all(m in metric_values for m in siglip_metrics):
siglip_avg = sum(metric_values[m] for m in siglip_metrics) / len(siglip_metrics)
row.append(f"{siglip_avg:.4f}")
else:
row.append("")
# Calculate and add dino_avg
if all(m in metric_values for m in dino_metrics):
dino_avg = sum(metric_values[m] for m in dino_metrics) / len(dino_metrics)
row.append(f"{dino_avg:.4f}")
else:
row.append("")
# Calculate and add overall average
if metric_values:
overall_avg = sum(metric_values.values()) / len(metric_values)
row.append(f"{overall_avg:.4f}")
else:
row.append("")
# Add only average diversity
if 'diversity' in step_data and 'average' in step_data['diversity']:
value = step_data['diversity']['average']
# Format floating point values to 4 decimal places
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Add skewness values if requested
if print_skewness:
for metric in ordered_metrics:
if 'skewness' in step_data and metric in step_data['skewness']:
value = step_data['skewness'][metric]
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Add standard columns
if 'valid_count' in step_data:
row.append(str(step_data['valid_count']))
else:
row.append("")
if 'total_count' in step_data:
row.append(str(step_data['total_count']))
else:
row.append("")
if 'success_rate' in step_data:
value = step_data['success_rate']
if isinstance(value, float):
row.append(f"{value:.4f}")
else:
row.append(str(value))
else:
row.append("")
# Print row
print(','.join(row))
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Visualize evaluation metrics')
parser.add_argument('--input', default='./evaluation_summary.json',
help='Input JSON file containing evaluation data')
parser.add_argument('--output', default=None,
help='Output path for the visualization')
args = parser.parse_args()
# Load data
data = load_evaluation_data(args.input)
for task in ['coco', 'SGP-Single-9k']:
print(f"\n=== CSV Format Data for {task} ===\n")
print_as_csv(data, task_name=task)