-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathensemble_evaluate.py
More file actions
215 lines (173 loc) · 6.47 KB
/
Copy pathensemble_evaluate.py
File metadata and controls
215 lines (173 loc) · 6.47 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
#!/usr/bin/env python3
"""
Evaluate ensemble of models by averaging their predictions.
"""
import csv
import argparse
import numpy as np
from pathlib import Path
from tqdm import tqdm
import timm
import torch
from torch.utils.data import DataLoader
from dataset import RareTestSet
from metrics import compute_metrics
def find_model_paths(seeds, output_dir="output"):
"""Find model paths for given seeds."""
model_paths = []
output_path = Path(output_dir)
for seed in seeds:
# Look for experiment directories that might contain this seed
for exp_dir in output_path.iterdir():
if not exp_dir.is_dir():
continue
# Check if this experiment used the seed
config_file = exp_dir / "config.txt"
if config_file.exists():
with open(config_file) as f:
for line in f:
if line.startswith("seed:"):
exp_seed = int(line.split(":", 1)[1].strip())
if exp_seed == seed:
model_path = exp_dir / "best_model.pth"
if model_path.exists():
model_paths.append(
(seed, model_path, exp_dir)
)
break
return model_paths
def load_model(model_name, model_path, device):
"""Load a trained model."""
model = timm.create_model(model_name, pretrained=False, num_classes=1)
model.load_state_dict(torch.load(model_path, map_location=device))
model = model.to(device)
model.eval()
return model
def evaluate_ensemble(
model_paths, data_path, model_name, device, input_size=224, use_tta=False
):
"""Evaluate ensemble by averaging predictions from all models."""
print(f"Loading {len(model_paths)} models for ensemble evaluation...")
# Load all models
models = []
for seed, model_path, exp_dir in model_paths:
print(f"Loading model from seed {seed}: {model_path}")
model = load_model(model_name, model_path, device)
models.append(model)
# Load dataset
dataset = RareTestSet(data_path, return_paths=True, input_size=input_size)
dataloader = DataLoader(
dataset, batch_size=1, shuffle=False
) # batch_size=1 for TTA
all_predictions = []
all_labels = []
all_paths = []
print("Evaluating ensemble...")
with torch.no_grad():
for images, labels, paths in tqdm(
dataloader, desc="Ensemble evaluation"
):
# Get predictions from all models
ensemble_predictions = []
for model in models:
if use_tta:
# Apply TTA for each model
tta_predictions = []
# Original image
img = images[0].to(device)
pred = torch.sigmoid(model(img.unsqueeze(0)))
tta_predictions.append(pred)
# Horizontal flip
img_flip = torch.flip(img, [2]).to(device) # flip width
pred_flip = torch.sigmoid(model(img_flip.unsqueeze(0)))
tta_predictions.append(pred_flip)
# Average TTA predictions
model_pred = torch.stack(tta_predictions).mean(dim=0)
else:
img = images[0].to(device)
model_pred = torch.sigmoid(model(img.unsqueeze(0)))
ensemble_predictions.append(model_pred)
# Average predictions from all models
ensemble_pred = torch.stack(ensemble_predictions).mean(dim=0)
all_predictions.append(ensemble_pred.cpu().numpy()[0])
all_labels.append(labels[0].numpy())
all_paths.append(str(paths[0]))
return all_predictions, all_labels, all_paths
def save_predictions(predictions, labels, paths, output_file):
"""Save ensemble predictions to CSV."""
with open(output_file, "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["filename", "label", "score"])
for path, label, score in zip(paths, labels, predictions):
filename = Path(path).name
writer.writerow([filename, int(label), float(score)])
def main():
parser = argparse.ArgumentParser(description="Evaluate ensemble of models")
parser.add_argument(
"--data_path", type=str, required=True, help="Path to test dataset"
)
parser.add_argument(
"--seeds",
type=int,
nargs="+",
required=True,
help="Seeds of models to ensemble",
)
parser.add_argument(
"--model", type=str, default="resnet50", help="Model architecture"
)
parser.add_argument(
"--input_size", type=int, default=224, help="Input image size"
)
parser.add_argument(
"--use_tta", action="store_true", help="Use test-time augmentation"
)
parser.add_argument(
"--output_file",
type=str,
default="ensemble_predictions.csv",
help="Output file",
)
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
# Find model paths
model_paths = find_model_paths(args.seeds)
if not model_paths:
print(f"No models found for seeds: {args.seeds}")
return
print(f"Found {len(model_paths)} models for ensemble")
# Evaluate ensemble
predictions, labels, paths = evaluate_ensemble(
model_paths,
args.data_path,
args.model,
device,
args.input_size,
args.use_tta,
)
# Save predictions
save_predictions(predictions, labels, paths, args.output_file)
print(f"Ensemble predictions saved to: {args.output_file}")
# Compute metrics
predictions = np.array(predictions)
labels = np.array(labels)
metrics = compute_metrics(labels, predictions)
print("\nEnsemble Metrics:")
for key, value in metrics.items():
print(f"{key}: {value:.4f}")
# Bootstrap evaluation
from evaluate import bootstrap_evaluation
summary = bootstrap_evaluation(
args.output_file,
n_bootstrap=1000,
min_neoplasia=1000,
ndbe_multiplier=100,
output_dir=".",
prefix="ensemble",
)
if summary is not None:
print("\nEnsemble Bootstrap Summary:")
print(summary)
if __name__ == "__main__":
main()