File size: 2,189 Bytes
1024e04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Check temporal tiling using real published weights and a supplied public cine."""
import argparse
import json
import time
from pathlib import Path

import numpy as np
import torch

from .architecture import artery_model, classifier_stream
from .dicom import read_video
from .preprocessing import preprocess_classifier, to_uint8
from .runtime import _device, _load
from .weights import MANIFEST


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--dicom", required=True, type=Path)
    parser.add_argument("--output", required=True, type=Path)
    args = parser.parse_args()
    source = to_uint8(read_video(args.dicom))
    # An in-memory repeated fixture crosses multiple temporal tiles without
    # changing any source DICOM or claiming this is a new clinical study.
    video = np.concatenate([source] * ((195 + len(source) - 1) // len(source)))[:195]
    device = _device()
    started = time.monotonic()
    with torch.inference_mode():
        model = _load(artery_model, MANIFEST["classifier"], device, None)
        whole_clip = preprocess_classifier(video).unsqueeze(0).to(device)
        whole = model(whole_clip)
        del whole_clip
        tiled = classifier_stream(model, len(video), lambda a, b: preprocess_classifier(video[a:b]), device)
        torch.testing.assert_close(whole, tiled, rtol=2e-5, atol=2e-5)
        result = {"frames": len(video), "fixture": "repeated source frames in memory; original DICOM unchanged",
                  "whole_logit": float(whole.cpu()[0, 0]), "tiled_logit": float(tiled.cpu()[0, 0]),
                  "absolute_logit_difference": float(abs(whole - tiled).cpu()[0, 0]),
                  "whole_probability_right": float(whole.sigmoid().cpu()[0, 0]),
                  "tiled_probability_right": float(tiled.sigmoid().cpu()[0, 0]),
                  "classifier_sha256": MANIFEST["classifier"]["sha256"],
                  "classifier_preprocessing": MANIFEST["preprocessing"]["classifier"],
                  "seconds": time.monotonic() - started, "pass": True}
    args.output.write_text(json.dumps(result, indent=2))
    print(json.dumps(result), flush=True)


if __name__ == "__main__":
    main()