File size: 2,914 Bytes
71512a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Improved Model Inference β€” Usage Guide (`download_imp`)



## What this runner does



`run_inference.py` runs the **latest improved model** trained from the notebooks in `improvement/`:

- EfficientNet-B4 backbone (`tf_efficientnet_b4`)
- 2.5D input (prev + center + next slices β†’ 9 channels)
- 6 outputs (`any` + 5 hemorrhage subtypes)
- 5-fold ensemble (`best_model_fold0..4.pth`)
- Saved calibration (`isotonic`/`temperature`) from `calibration_params.json`

Outputs:

- Per-slice JSON report (`outputs/reports/*.json`)
- Slice-level CSV (`outputs/slice_predictions.csv`)
- Patient-level CSV (`outputs/patient_predictions.csv`)

---

## Required files (already in `download_imp/`)



- `best_model_fold0.pth`

- `best_model_fold1.pth`

- `best_model_fold2.pth`

- `best_model_fold3.pth`

- `best_model_fold4.pth`

- `calibration_params.json`
- `isotonic_models.pkl`
- `normalization_stats.json`
- `manifest.csv` (optional at inference time; used only for `true_any` if IDs match)

---

## Python package requirements

```bash

pip install -r requirements.txt

```

Notes:

- `timm` is required for `tf_efficientnet_b4` model construction.
- `scikit-learn` is needed to deserialize and use `isotonic_models.pkl`.

---

## Folder setup

Create this folder and place DICOM files there:

```text

download_imp/

β”œβ”€β”€ run_inference.py

β”œβ”€β”€ run_interface.py

β”œβ”€β”€ best_model_fold0.pth

β”œβ”€β”€ best_model_fold1.pth

β”œβ”€β”€ best_model_fold2.pth

β”œβ”€β”€ best_model_fold3.pth

β”œβ”€β”€ best_model_fold4.pth

β”œβ”€β”€ calibration_params.json

β”œβ”€β”€ isotonic_models.pkl

β”œβ”€β”€ normalization_stats.json

β”œβ”€β”€ manifest.csv

└── dicom_inputs/

    β”œβ”€β”€ ID_xxx1.dcm

    β”œβ”€β”€ ID_xxx2.dcm

    └── ...

```

---

## Run commands

From workspace root:

```bash

cd download_imp

python run_inference.py

```

or (same thing):

```bash

python run_interface.py

```

---

## Important behavior

- No CLI arguments; all settings are at top of `run_inference.py` (`CONFIG` section).
- `FOLD_SELECTION` controls checkpoint selection:
    - `"ensemble"` = use all available folds and average logits
    - `0..4` = use one specific fold only
- If `best_method` is `isotonic`, the runner uses `isotonic_models.pkl`.
- Missing prev/next slice in a series is handled exactly like training cache logic: neighbor falls back to center slice.
- Decision threshold defaults to `threshold_at_spec90` from `calibration_params.json` unless overridden in config.

---

## Recommended production checklist

1. Keep all fold checkpoints and calibration files in the same `download_imp/` directory.
2. Verify DICOMs are non-contrast head CT slices before inference.
3. Run once on a small sample and review `slice_predictions.csv` and JSON reports.
4. Have radiologist review all flagged and uncertain cases.