Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
108 lines (89 loc) · 3 KB
/
Copy pathapp.py
File metadata and controls
108 lines (89 loc) · 3 KB
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
# -*- coding: utf-8 -*-
<<<<<<< HEAD
"""app.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/18sbK2UNnXPQ3JNHmJmA2uhHuTz1R1bEC
"""
=======
>>>>>>> 484acfacc5217f60647c28cdaa12e61e89a4365c
import torch
import torch.nn as nn
import torchvision.transforms as T
from PIL import Image
import gradio as gr
# ---- Model definition (must match training) ----
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 16, 3, padding=1)
self.bn1 = nn.BatchNorm2d(16)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(16, 32, 3, padding=1)
self.bn2 = nn.BatchNorm2d(32)
<<<<<<< HEAD
self.fc1 = nn.Linear(32 * 56 * 56, 112) # (224 -> 112 -> 56 after two pools)
=======
self.fc1 = nn.Linear(32 * 56 * 56, 112)
>>>>>>> 484acfacc5217f60647c28cdaa12e61e89a4365c
self.drop1 = nn.Dropout(0.5)
self.fc2 = nn.Linear(112, 84)
self.drop2 = nn.Dropout(0.2)
self.fc3 = nn.Linear(84, 2)
def forward(self, x):
x = self.pool(torch.relu(self.bn1(self.conv1(x))))
x = self.pool(torch.relu(self.bn2(self.conv2(x))))
x = x.view(x.size(0), -1)
x = self.drop1(torch.relu(self.fc1(x)))
x = self.drop2(torch.relu(self.fc2(x)))
x = self.fc3(x)
return x
# ---- Load model once (CPU is fine for Spaces unless you enable GPU) ----
MODEL_PATH = "pneumonia_model.pth"
device = torch.device("cpu")
model = Net()
state_dict = torch.load(MODEL_PATH, map_location=device)
model.load_state_dict(state_dict)
model.eval()
# ---- Preprocess must match training ----
transform = T.Compose([
T.Resize((224, 224)),
T.ToTensor(),
T.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
])
CLASSES = ["Normal", "Pneumonia"]
def predict(img_pil: Image.Image):
img = img_pil.convert("RGB")
<<<<<<< HEAD
x = transform(img).unsqueeze(0) # 1,C,H,W
=======
x = transform(img).unsqueeze(0)
>>>>>>> 484acfacc5217f60647c28cdaa12e61e89a4365c
with torch.no_grad():
logits = model(x)
probs = torch.softmax(logits, dim=1)[0].tolist()
return {CLASSES[i]: float(probs[i]) for i in range(2)}
<<<<<<< HEAD
=======
# ---- Gradio interface ----
>>>>>>> 484acfacc5217f60647c28cdaa12e61e89a4365c
demo = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil", label="Chest X-ray"),
outputs=gr.Label(num_top_classes=2),
title="Pneumonia Detector (CNN)",
description="Upload a chest X-ray image. The model predicts Normal vs Pneumonia with probabilities."
)
<<<<<<< HEAD
if __name__ == "__main__":
demo.launch()
from google.colab import files
# Upload your model file
uploaded = files.upload()
!ls -lh
!mv "pneumonia_model(2).pth" "pneumonia_model.pth"
from google.colab import files
files.download("app.ipynb")
=======
demo.launch()
>>>>>>> 484acfacc5217f60647c28cdaa12e61e89a4365c