Spaces:
Sleeping
Sleeping
Download engine/validate_classifier.py from MesserMMP/syntax-video-infer: direct link, hf CLI and curl.
- Browser
- Download file 3 kB
-
https://huggingface.co/spaces/MesserMMP/syntax-video-infer/resolve/main/engine/validate_classifier.py
- Command line
-
hf download hf://spaces/MesserMMP/syntax-video-infer/engine/validate_classifier.py
-
curl -L -o validate_classifier.py https://huggingface.co/spaces/MesserMMP/syntax-video-infer/resolve/main/engine/validate_classifier.py
3 kB
| """Compare explicit reference classifier transforms with archived probabilities.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from safetensors.torch import load_file | |
| from torchvision.models.video import r3d_18 | |
| from torchvision.transforms import Resize | |
| from torchvision.transforms._transforms_video import ToTensorVideo | |
| from .dicom import read_video | |
| from .preprocessing import to_uint8 | |
| from .runtime import _device | |
| from .weights import MANIFEST, weight_file | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--fixtures", type=Path, required=True) | |
| parser.add_argument("--output", type=Path, required=True) | |
| parser.add_argument("--dataset", default="philips_97") | |
| parser.add_argument("--all-frames-only", action="store_true") | |
| parser.add_argument("--antialias", choices=("true", "false", "both"), default="both") | |
| args = parser.parse_args() | |
| device = _device() | |
| model = r3d_18(weights=None) | |
| model.fc = torch.nn.Linear(512, 1) | |
| model.load_state_dict(load_file(str(weight_file(MANIFEST["classifier"])), device="cpu"), strict=True) | |
| model.to(device).eval() | |
| report = [] | |
| with torch.inference_mode(): | |
| for case in json.loads(args.fixtures.read_text()): | |
| if case["dataset"] != args.dataset: | |
| continue | |
| for projection in case["study"]["videos"]: | |
| raw = read_video(args.fixtures.parent / projection["path"]) | |
| video = to_uint8(raw) | |
| item = {"filename": Path(projection["path"]).name, "frames": len(video), "dtype": str(raw.dtype), | |
| "max_pixel": int(raw.max()), "stored_probability": projection["artery_prob"], "variants": {}} | |
| for temporal in (("all_frames",) if args.all_frames_only else ("all_frames", "center32")): | |
| frames = video | |
| if temporal == "center32": | |
| while len(frames) < 32: | |
| frames = np.concatenate([frames, frames]) | |
| start = (len(frames) - 32) // 2 | |
| frames = frames[start:start + 32] | |
| for antialias in ((True, False) if args.antialias == "both" else (args.antialias == "true",)): | |
| x = ToTensorVideo()(torch.from_numpy(np.stack([frames] * 3, axis=-1))) | |
| x = Resize((224, 224), antialias=antialias)(x) | |
| x = (x - torch.tensor([.485, .456, .406])[:, None, None, None]) / torch.tensor([.229, .224, .225])[:, None, None, None] | |
| probability = float(model(x.unsqueeze(0).to(device)).sigmoid().cpu()[0, 0]) | |
| item["variants"][f"{temporal}_antialias_{antialias}"] = probability | |
| del x | |
| print(json.dumps(item), flush=True) | |
| report.append(item) | |
| args.output.write_text(json.dumps(report, indent=2)) | |
| if __name__ == "__main__": | |
| main() | |