CFGP00 commited on
Commit ·
5e0bfa2
1
Parent(s): 5b6be83
adding app
Browse files- .DS_Store +0 -0
- XRay_app/.DS_Store +0 -0
- XRay_app/app.py +27 -0
- XRay_app/utils/model_utils.py +41 -0
- XRay_app/utils/preprocessing.py +11 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
XRay_app/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
XRay_app/app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
from utils.model_utils import load_model, predict
|
| 6 |
+
from utils.preprocessing import preprocess_image
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="X-ray Diagnosis Demo", layout="centered")
|
| 9 |
+
st.title("🩻 X-ray Multi-Label Diagnosis App (CheXNet)")
|
| 10 |
+
|
| 11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model_path = "model/dannynet-55-best_model_20250422-211522.pth"
|
| 13 |
+
model = load_model(model_path, device)
|
| 14 |
+
|
| 15 |
+
uploaded_file = st.file_uploader("Upload a chest X-ray", type=["jpg", "jpeg", "png"])
|
| 16 |
+
|
| 17 |
+
if uploaded_file:
|
| 18 |
+
image = Image.open(uploaded_file).convert("RGB")
|
| 19 |
+
st.image(image, caption="Uploaded X-ray", use_column_width=True)
|
| 20 |
+
|
| 21 |
+
img_tensor = preprocess_image(image)
|
| 22 |
+
|
| 23 |
+
probs = predict(model, img_tensor, device)
|
| 24 |
+
|
| 25 |
+
st.subheader("Predictions")
|
| 26 |
+
for disease, prob in probs.items():
|
| 27 |
+
st.write(f"**{disease}**: {prob:.4f}")
|
XRay_app/utils/model_utils.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/model_utils.py
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
from torchvision.models import densenet121, DenseNet121_Weights
|
| 5 |
+
|
| 6 |
+
# Disease labels
|
| 7 |
+
DISEASE_LIST = [
|
| 8 |
+
'Atelectasis', 'Cardiomegaly', 'Consolidation', 'Edema', 'Effusion',
|
| 9 |
+
'Emphysema', 'Fibrosis', 'Hernia', 'Infiltration', 'Mass',
|
| 10 |
+
'Nodule', 'Pleural_Thickening', 'Pneumonia', 'Pneumothorax'
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
# Load trained CheXNet model
|
| 14 |
+
class CheXNet(nn.Module):
|
| 15 |
+
def __init__(self, num_classes=14):
|
| 16 |
+
super().__init__()
|
| 17 |
+
base_model = densenet121(weights=DenseNet121_Weights.IMAGENET1K_V1)
|
| 18 |
+
self.features = base_model.features
|
| 19 |
+
self.classifier = nn.Linear(base_model.classifier.in_features, num_classes)
|
| 20 |
+
|
| 21 |
+
def forward(self, x):
|
| 22 |
+
x = self.features(x)
|
| 23 |
+
x = nn.functional.adaptive_avg_pool2d(x, (1, 1))
|
| 24 |
+
x = torch.flatten(x, 1)
|
| 25 |
+
return self.classifier(x)
|
| 26 |
+
|
| 27 |
+
def load_model(url, device):
|
| 28 |
+
model_path = "dannynet.pth"
|
| 29 |
+
torch.hub.download_url_to_file(url,model_path)
|
| 30 |
+
model = torch.load(model_path, map_location = device)
|
| 31 |
+
model.eval()
|
| 32 |
+
return model
|
| 33 |
+
|
| 34 |
+
def predict(model, img_tensor, device):
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
output = model(img_tensor.unsqueeze(0).to(device))
|
| 37 |
+
probs = torch.sigmoid(output[0]).cpu().numpy()
|
| 38 |
+
return dict(zip(DISEASE_LIST, probs))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
XRay_app/utils/preprocessing.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils/preprocessing.py
|
| 2 |
+
from torchvision import transforms
|
| 3 |
+
|
| 4 |
+
def preprocess_image(img):
|
| 5 |
+
transform = transforms.Compose([
|
| 6 |
+
transforms.Resize(256),
|
| 7 |
+
transforms.CenterCrop(224),
|
| 8 |
+
transforms.ToTensor(),
|
| 9 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
| 10 |
+
])
|
| 11 |
+
return transform(img)
|