Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import torch
|
|
| 7 |
import torchxrayvision as xrv
|
| 8 |
import re
|
| 9 |
import os
|
|
|
|
| 10 |
|
| 11 |
# -----------------------------
|
| 12 |
# Imaging Agent (Chest X-ray, proxy for lung cancer risk)
|
|
@@ -31,16 +32,16 @@ def imaging_agent(image_path: str):
|
|
| 31 |
# TorchXRayVision normalization
|
| 32 |
arr = xrv.datasets.normalize(arr, 4096)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
sample = xrv.datasets.XRayCenterCrop()(sample)
|
| 39 |
-
sample = xrv.datasets.XRayResizer(224)(sample)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# Convert to torch tensor
|
| 44 |
arr = torch.from_numpy(arr).unsqueeze(0).unsqueeze(0).to(DEVICE)
|
| 45 |
|
| 46 |
# Inference
|
|
@@ -48,7 +49,7 @@ def imaging_agent(image_path: str):
|
|
| 48 |
preds = MODEL(arr)[0]
|
| 49 |
probs = torch.sigmoid(preds).cpu().numpy().tolist()
|
| 50 |
|
| 51 |
-
# Focus
|
| 52 |
focus_labels = ["Lung Opacity", "Mass", "Nodule"]
|
| 53 |
focus = [(l, probs[PATHOLOGIES.index(l)]) for l in focus_labels if l in PATHOLOGIES]
|
| 54 |
|
|
@@ -162,3 +163,4 @@ demo.launch()
|
|
| 162 |
|
| 163 |
|
| 164 |
|
|
|
|
|
|
| 7 |
import torchxrayvision as xrv
|
| 8 |
import re
|
| 9 |
import os
|
| 10 |
+
from skimage.transform import resize as sk_resize
|
| 11 |
|
| 12 |
# -----------------------------
|
| 13 |
# Imaging Agent (Chest X-ray, proxy for lung cancer risk)
|
|
|
|
| 32 |
# TorchXRayVision normalization
|
| 33 |
arr = xrv.datasets.normalize(arr, 4096)
|
| 34 |
|
| 35 |
+
# --- Manual preprocessing instead of dict transforms ---
|
| 36 |
+
h, w = arr.shape
|
| 37 |
+
min_dim = min(h, w)
|
| 38 |
+
startx = w // 2 - (min_dim // 2)
|
| 39 |
+
starty = h // 2 - (min_dim // 2)
|
| 40 |
+
arr = arr[starty:starty+min_dim, startx:startx+min_dim] # center crop
|
| 41 |
|
| 42 |
+
arr = sk_resize(arr, (224, 224), preserve_range=True) # resize to 224x224
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Convert to tensor
|
|
|
|
|
|
|
| 45 |
arr = torch.from_numpy(arr).unsqueeze(0).unsqueeze(0).to(DEVICE)
|
| 46 |
|
| 47 |
# Inference
|
|
|
|
| 49 |
preds = MODEL(arr)[0]
|
| 50 |
probs = torch.sigmoid(preds).cpu().numpy().tolist()
|
| 51 |
|
| 52 |
+
# Focus on lung pathologies relevant to cancer
|
| 53 |
focus_labels = ["Lung Opacity", "Mass", "Nodule"]
|
| 54 |
focus = [(l, probs[PATHOLOGIES.index(l)]) for l in focus_labels if l in PATHOLOGIES]
|
| 55 |
|
|
|
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
+
|