forked from echonet/echo_CLIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzero_shot_mr.py
More file actions
318 lines (260 loc) · 11.2 KB
/
Copy pathzero_shot_mr.py
File metadata and controls
318 lines (260 loc) · 11.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
import argparse
import csv
import json
import logging
import re
from collections import defaultdict
from pathlib import Path
import numpy as np
import pandas as pd
import torch
import torch.nn.functional as F
import matplotlib.pyplot as plt
import torchvision.transforms as T
from open_clip import create_model_and_transforms, tokenize
try:
import seaborn as sns
except ImportError: # pragma: no cover - optional dependency fallback
sns = None
from utils import clean_text, read_avi, resolve_video_path, normalize_label, CLASS_ORDER
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--csv", type=Path, required=True)
parser.add_argument(
"--video-path-col", type=str, default="video_filename", help="CSV column for video path"
)
parser.add_argument("--label-col", type=str, default="label", help="CSV column for label")
parser.add_argument(
"--subject-id-col", type=str, default="patient_id", help="CSV column for subject id"
)
parser.add_argument(
"--template-vocab", type=Path, default=Path("template_vocab.txt"), help="Prompt source"
)
parser.add_argument(
"--model", type=str, default="hf-hub:mkaichristensen/echo-clip", help="OpenCLIP model"
)
parser.add_argument("--device", type=str, default="cuda")
parser.add_argument("--max-frames", type=int, default=40)
parser.add_argument("--frame-step", type=int, default=2)
parser.add_argument(
"--borderline-policy",
type=str,
choices=["round_up", "round_down", "drop"],
default="round_up",
help="How to map MILD/MODERATE and MODERATE/SEVERE labels",
)
parser.add_argument("--output-dir", type=Path, default=Path("zero_shot_mr_outputs"))
return parser.parse_args()
def extract_mr_prompts(template_vocab_path=None):
"""
Returns a dictionary of hardcoded, clinically refined, and balanced
echocardiogram prompts for four classes of mitral regurgitation.
Args:
template_vocab_path: Ignored. Kept for signature compatibility with the legacy pipeline.
Returns:
dict: A dictionary where keys are classes ('normal', 'mild', 'moderate', 'severe')
and values are sorted lists of prompt strings.
"""
prompts = {
"normal": [
"There is no mitral regurgitation.",
"There is trivial to trace mitral valve regurgitation."
],
"mild": [
"There is mild mitral regurgitation.",
"There is mild to mild-moderate mitral valve regurgitation."
],
"moderate": [
"There is moderate mitral regurgitation.",
"There is moderate to moderate-severe mitral valve regurgitation."
],
"severe": [
"There is severe mitral regurgitation.",
"There is severe to very severe mitral valve regurgitation."
]
}
# Match the original function's return format (lists sorted alphabetically)
return {k: sorted(v) for k, v in prompts.items()}
def sample_frames(video_np, max_frames=40, frame_step=2):
frames = video_np[0 : min(max_frames, len(video_np)) : frame_step]
if len(frames) == 0:
return video_np[:1]
return frames
def encode_video(video_path, preprocess_val, model, device, max_frames=40, frame_step=2):
frames = read_avi(video_path, (224, 224))
if len(frames) == 0:
raise RuntimeError(f"No frames found in video: {video_path}")
frames = sample_frames(frames, max_frames=max_frames, frame_step=frame_step)
tensor = torch.stack([preprocess_val(T.ToPILImage()(f)) for f in frames], dim=0)
tensor = tensor.to(device)
tensor = tensor.to(torch.bfloat16)
with torch.no_grad():
frame_embeddings = F.normalize(model.encode_image(tensor), dim=-1)
return frame_embeddings
def compute_class_logits(frame_embeddings, class_text_embeddings):
# Calculate similarities for all frames independently
similarities = frame_embeddings @ class_text_embeddings.T
# REMOVED: similarities.mean(dim=0)
# Returns shape: (num_frames, num_classes)
return similarities
def collapse_clinical_significant(labels):
"""Collapse labels 0/1 vs 2/3 into a clinical-significant binary target."""
labels = np.asarray(labels)
return np.where(np.isin(labels, [2, 3]), 1, 0).astype(int)
def main():
args = parse_args()
args.output_dir.mkdir(parents=True, exist_ok=True)
print("Loading model...")
model, _, preprocess_val = create_model_and_transforms(
args.model, precision="bf16", device=args.device
)
logit_scale = model.logit_scale.exp().item()
print("Logit scale:", logit_scale)
print(f"Number of parameters: {sum(p.numel() for p in model.parameters())}")
prompts_by_class = extract_mr_prompts(args.template_vocab)
with (args.output_dir / "mr_prompts.json").open("w") as f:
json.dump(prompts_by_class, f, indent=2)
print("Prompt counts:")
for c in CLASS_ORDER:
print(f" {c}: {len(prompts_by_class[c])}")
print("Prompts:")
for cls_name in CLASS_ORDER:
for prompt in prompts_by_class[cls_name]:
print(f" {cls_name}: {prompt}")
class_embeddings = []
with torch.no_grad():
for cls_name in CLASS_ORDER:
text_tokens = tokenize(prompts_by_class[cls_name]).to(args.device)
text_embs = F.normalize(model.encode_text(text_tokens), dim=-1)
# Average multiple prompt embeddings to form a class prototype.
class_emb = F.normalize(text_embs.mean(dim=0, keepdim=True), dim=-1)
class_embeddings.append(class_emb)
class_embeddings = torch.cat(class_embeddings, dim=0)
records = []
dropped = 0
with args.csv.open("r", newline="") as f:
reader = csv.DictReader(f)
for row in reader:
label_name = normalize_label(
row[args.label_col], borderline_policy=args.borderline_policy
)
if label_name is None:
print(f"Skipping {row} because no label name found in CSV.")
dropped += 1
continue
video_path = resolve_video_path(row[args.video_path_col], args.csv)
if not video_path.exists():
print(f"Skipping {row} because no video path found in CSV.")
dropped += 1
continue
records.append(
{
"video_path": str(video_path),
"subject_id": str(row[args.subject_id_col]),
"label_name": label_name,
"label_id": CLASS_ORDER.index(label_name),
}
)
if len(records) == 0:
raise RuntimeError("No usable rows found in CSV after filtering.")
print(f"Loaded {len(records)} videos ({dropped} rows skipped).")
video_rows = []
subject_logits = defaultdict(list)
subject_label_ids = {}
for i, rec in enumerate(records, start=1):
print(f"[{i}/{len(records)}] {rec['video_path']}")
frame_embeddings = encode_video(
rec["video_path"],
preprocess_val,
model,
args.device,
max_frames=args.max_frames,
frame_step=args.frame_step,
)
# Get frame-level similarities
frame_similarities = compute_class_logits(frame_embeddings, class_embeddings)
# Apply the logit scale (from our previous fix!)
frame_logits = frame_similarities * logit_scale
# --- TOP-K POOLING ---
# Let's look at the top 25% of frames (roughly corresponding to systole)
k = max(1, len(frame_logits) // 4)
# Extract the highest logit values for each class across the temporal dimension
topk_logits, _ = torch.topk(frame_logits, k, dim=0)
# Average only those top frames to get the final video logits
video_logits = topk_logits.mean(dim=0)
# ---------------------
probs = torch.softmax(video_logits, dim=-1)
pred_id = int(torch.argmax(video_logits).item())
video_rows.append(
{
"video_path": rec["video_path"],
"subject_id": rec["subject_id"],
"label": rec["label_name"],
"pred": CLASS_ORDER[pred_id],
**{f"logit_{c}": float(video_logits[j].item()) for j, c in enumerate(CLASS_ORDER)},
**{f"prob_{c}": float(probs[j].item()) for j, c in enumerate(CLASS_ORDER)},
}
)
numpy_logits = video_logits.detach().cpu().to(torch.float32).numpy()
subject_logits[rec["subject_id"]].append(numpy_logits)
if rec["subject_id"] in subject_label_ids:
if subject_label_ids[rec["subject_id"]] != rec["label_id"]:
raise RuntimeError(
f"Subject {rec['subject_id']} has conflicting labels in CSV."
)
subject_label_ids[rec["subject_id"]] = rec["label_id"]
# Aggregate multiple videos for each subject by mean logits.
subject_rows = []
for sid, all_logits in subject_logits.items():
avg_logits = np.mean(np.stack(all_logits, axis=0), axis=0)
pred_id = int(np.argmax(avg_logits))
true_id = int(subject_label_ids[sid])
subject_rows.append(
{
"subject_id": sid,
"n_videos": len(all_logits),
"label": CLASS_ORDER[true_id],
"pred": CLASS_ORDER[pred_id],
**{f"avg_logit_{c}": float(avg_logits[j]) for j, c in enumerate(CLASS_ORDER)},
}
)
# Build unified predictions (one entry per video) compatible with the unified inference pipeline
unified_predictions = []
for vr in video_rows:
probs = [vr.get(f"prob_{c}", 0.0) for c in CLASS_ORDER]
try:
true_label_id = CLASS_ORDER.index(vr["label"]) if vr.get("label") is not None else None
except ValueError:
true_label_id = None
unified_predictions.append(
{
"subject_id": vr.get("subject_id"),
"video_id": vr.get("video_path"),
"true_label": true_label_id,
"probs": probs,
}
)
# Save unified predictions for downstream, unified-metrics computation
unified_out = args.output_dir / "unified_predictions.json"
with unified_out.open("w") as f:
json.dump(unified_predictions, f, indent=2)
# Also save CSV outputs for convenience
video_out = args.output_dir / "mr_video_predictions.csv"
with video_out.open("w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=list(video_rows[0].keys()))
writer.writeheader()
writer.writerows(video_rows)
subject_out = args.output_dir / "mr_subject_predictions.csv"
with subject_out.open("w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=list(subject_rows[0].keys()))
writer.writeheader()
writer.writerows(subject_rows)
print("Done.")
print(f"Wrote unified predictions to: {unified_out}")
print(f"Wrote prompts to: {args.output_dir / 'mr_prompts.json'}")
print(f"Wrote video predictions to: {video_out}")
print(f"Wrote subject predictions to: {subject_out}")
if __name__ == "__main__":
main()