-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevaluate_dutoit_datasets.py
More file actions
executable file
·293 lines (229 loc) · 9.39 KB
/
evaluate_dutoit_datasets.py
File metadata and controls
executable file
·293 lines (229 loc) · 9.39 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
#!/usr/bin/env python3
"""
Evaluate SAM-RFI on du Toit et al. (2024) HERA and LOFAR datasets.
Usage:
python scripts/evaluate_dutoit_datasets.py \
--hera-path /mnt/Data/Data/SAM-RFI/HERA_28-03-2023_all.pkl \
--hera-aof-path /mnt/Data/Data/SAM-RFI/HERA_AOF_20-07-2023_all.pkl \
--lofar-path /mnt/Data/Data/SAM-RFI/LOFAR_Full_RFI_dataset.pkl \
--output-dir ./dutoit_evaluation
"""
import argparse
import json
import pickle
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
from rfi_toolbox.evaluation import evaluate_segmentation
from tqdm import tqdm
from samrfi.inference import RFIPredictor
def load_dutoit_dataset(pkl_path):
"""Load du Toit dataset from pickle."""
with open(pkl_path, "rb") as f:
data = pickle.load(f)
# Format: [train_images, train_masks, test_images, test_masks]
return {
"train_images": data[0],
"train_masks": data[1],
"test_images": data[2],
"test_masks": data[3],
}
def evaluate_model_on_dataset(predictor, images, ground_truth, dataset_name, model_name):
"""Evaluate single model on dataset - one baseline at a time to avoid memory issues."""
all_metrics = []
print(f" Evaluating {model_name} on {dataset_name} ({len(images)} samples)...")
# Process one baseline at a time to avoid memory overflow
for idx in tqdm(range(len(images)), desc=f" {model_name}"):
img = images[idx] # Shape: (512, 512, 1 or 2)
# Handle different formats
if img.shape[-1] == 2:
# HERA format: (mag, phase) -> convert to complex
magnitude = img[..., 0]
phase = img[..., 1]
img_complex = magnitude * np.exp(1j * phase)
else:
# LOFAR format: single channel magnitude
img_complex = img[..., 0].astype(np.complex64)
# Shape: (1, 1, 512, 512) for predict_array
img_4d = img_complex[np.newaxis, np.newaxis, :, :]
# Predict on single baseline
pred = predictor.predict_array(img_4d, patch_size=1024, threshold=None)
pred = pred[0, 0, :, :] # Extract (512, 512)
gt = ground_truth[idx][..., 0] # Remove channel dim
# Compute metrics
metrics = evaluate_segmentation(pred, gt)
all_metrics.append(metrics)
# Aggregate
aggregated = {
"iou": [m["iou"] for m in all_metrics],
"precision": [m["precision"] for m in all_metrics],
"recall": [m["recall"] for m in all_metrics],
"f1": [m["f1"] for m in all_metrics],
"dice": [m["dice"] for m in all_metrics],
}
return aggregated
def plot_metrics(results, output_dir):
"""Generate comparison plots."""
output_dir = Path(output_dir)
datasets = list(results.keys())
models = ["tiny", "small", "base_plus", "large"]
metrics = ["iou", "precision", "recall", "f1"]
colors = {
"tiny": "tab:blue",
"small": "tab:orange",
"base_plus": "tab:green",
"large": "tab:red",
}
for dataset in datasets:
fig, axes = plt.subplots(2, 2, figsize=(14, 10))
axes = axes.flatten()
for idx, metric in enumerate(metrics):
ax = axes[idx]
for model in models:
if model in results[dataset]:
values = results[dataset][model][metric]
# mean_val = np.mean(values)
# std_val = np.std(values)
# Box plot
positions = [models.index(model)]
bp = ax.boxplot(
[values], positions=positions, widths=0.6, patch_artist=True, showmeans=True
)
bp["boxes"][0].set_facecolor(colors[model])
bp["boxes"][0].set_alpha(0.6)
ax.set_xticks(range(len(models)))
ax.set_xticklabels(models)
ax.set_ylabel(metric.upper())
ax.set_title(f"{metric.upper()} Distribution", fontweight="bold")
ax.grid(True, alpha=0.3)
plt.suptitle(f"{dataset} - SAM Model Comparison", fontsize=14, fontweight="bold")
plt.tight_layout()
output_path = output_dir / f"{dataset}_comparison.png"
plt.savefig(output_path, dpi=150, bbox_inches="tight")
print(f" ✓ Saved: {output_path}")
plt.close()
def generate_summary_table(results, output_dir):
"""Generate summary statistics table."""
output_dir = Path(output_dir)
datasets = list(results.keys())
models = ["tiny", "small", "base_plus", "large"]
metrics = ["iou", "precision", "recall", "f1"]
table = []
table.append("=" * 100)
table.append("SAM-RFI Evaluation on du Toit et al. (2024) Datasets")
table.append("=" * 100)
for dataset in datasets:
table.append(f"\n{dataset.upper()}")
table.append("-" * 100)
table.append(
f"{'Metric':<12} | {'tiny':<18} | {'small':<18} | {'base_plus':<18} | {'large':<18}"
)
table.append("-" * 100)
for metric in metrics:
row = f"{metric.upper():<12}"
for model in models:
if model in results[dataset]:
values = results[dataset][model][metric]
mean_val = np.mean(values)
std_val = np.std(values)
row += f" | {mean_val:.4f} ± {std_val:.4f}"
else:
row += f" | {'N/A':<18}"
table.append(row)
table.append("=" * 100)
table_text = "\n".join(table)
print("\n" + table_text)
# Save to file
output_path = output_dir / "summary_table.txt"
with open(output_path, "w") as f:
f.write(table_text)
print(f"\n✓ Saved summary table: {output_path}")
return table_text
def main():
parser = argparse.ArgumentParser(
description="Evaluate SAM-RFI on du Toit datasets",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument("--hera-path", required=True, help="HERA dataset pickle")
parser.add_argument("--hera-aof-path", required=True, help="HERA AOFlagger dataset pickle")
parser.add_argument("--lofar-path", required=True, help="LOFAR dataset pickle")
parser.add_argument("--output-dir", default="./dutoit_evaluation", help="Output directory")
parser.add_argument("--device", default="cuda", help="Device (cuda/cpu)")
parser.add_argument(
"--use-test-set", action="store_true", help="Use test set (default: train set)"
)
args = parser.parse_args()
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
# Load datasets
print(f"\n{'='*70}")
print("Loading du Toit Datasets")
print(f"{'='*70}")
print("Loading HERA dataset (3.1GB)...")
hera = load_dutoit_dataset(args.hera_path)
print(" ✓ Loaded HERA")
print("Loading HERA_AOF dataset (3.1GB)...")
hera_aof = load_dutoit_dataset(args.hera_aof_path)
print(" ✓ Loaded HERA_AOF")
print("Loading LOFAR dataset (9.3GB)...")
lofar = load_dutoit_dataset(args.lofar_path)
print(" ✓ Loaded LOFAR")
split = "test" if args.use_test_set else "train"
print(f"Using {split} set")
print(f" HERA: {len(hera[f'{split}_images'])} samples")
print(f" HERA_AOF: {len(hera_aof[f'{split}_images'])} samples")
print(f" LOFAR: {len(lofar[f'{split}_images'])} samples")
datasets = {
"HERA": (hera[f"{split}_images"], hera[f"{split}_masks"]),
"HERA_AOF": (hera_aof[f"{split}_images"], hera_aof[f"{split}_masks"]),
"LOFAR": (lofar[f"{split}_images"], lofar[f"{split}_masks"]),
}
# Evaluate all models
models = ["tiny", "small", "base_plus", "large"]
results = {dataset_name: {} for dataset_name in datasets.keys()}
print(f"\n{'='*70}")
print("Evaluating SAM Models")
print(f"{'='*70}")
for model_name in models:
print(f"\n[{model_name.upper()}]")
model_path = f"polarimetic/sam-rfi/{model_name}"
try:
predictor = RFIPredictor(
model_path=model_path, sam_checkpoint=model_name, device=args.device
)
for dataset_name, (images, masks) in datasets.items():
metrics = evaluate_model_on_dataset(
predictor, images, masks, dataset_name, model_name
)
results[dataset_name][model_name] = metrics
except Exception as e:
print(f" ✗ Error with {model_name}: {e}")
continue
# Save results
print(f"\n{'='*70}")
print("Saving Results")
print(f"{'='*70}")
results_path = output_dir / "results.json"
with open(results_path, "w") as f:
# Convert to serializable format
json_results = {}
for dataset, models_data in results.items():
json_results[dataset] = {}
for model, metrics in models_data.items():
json_results[dataset][model] = {
k: [float(v) for v in vals] for k, vals in metrics.items()
}
json.dump(json_results, f, indent=2)
print(f"✓ Saved metrics: {results_path}")
# Generate plots
plot_metrics(results, output_dir)
# Generate summary table
generate_summary_table(results, output_dir)
print(f"\n{'='*70}")
print("✓ Evaluation Complete")
print(f"{'='*70}")
print(f"Results saved to: {output_dir}")
print(f"{'='*70}\n")
if __name__ == "__main__":
main()