Spaces:
Runtime error
Runtime error
Upload 5 files
Browse files- app.py +37 -0
- history.pt +3 -0
- model.pt +3 -0
- requirements.txt +3 -0
- utils.py +29 -0
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
from utils import Predict
|
5 |
+
|
6 |
+
# Page settings
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="Image Classification",
|
9 |
+
layout="wide",
|
10 |
+
initial_sidebar_state="expanded"
|
11 |
+
)
|
12 |
+
|
13 |
+
# Title
|
14 |
+
st.title('Classification of Chest X-ray Images')
|
15 |
+
|
16 |
+
# Upload file
|
17 |
+
uploaded_file = st.file_uploader(label="Choose a file", type=['jpg', 'jpeg','png'])
|
18 |
+
|
19 |
+
sidebar = st.sidebar
|
20 |
+
|
21 |
+
if uploaded_file is not None:
|
22 |
+
image = Image.open(uploaded_file).convert('RGB')
|
23 |
+
# image = np.array(image)
|
24 |
+
predict = Predict()
|
25 |
+
|
26 |
+
col1, col2 = st.columns([0.5, 0.5])
|
27 |
+
|
28 |
+
#Col 1
|
29 |
+
with col1:
|
30 |
+
st.markdown('<p style="text-align: center;">Input Image</p>', unsafe_allow_html=True)
|
31 |
+
st.image(image, width=425, caption="X-Ray Image")
|
32 |
+
|
33 |
+
#Col 2
|
34 |
+
with col2:
|
35 |
+
pred,topk = predict.predict(image)
|
36 |
+
st.text("Prediction: "+ pred)
|
37 |
+
st.text("Confidence: "+ str(topk))
|
history.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f8617c399a6ff43caf7dd8f1e4a0d44671608ce5bb7caab017073495a20fa894
|
3 |
+
size 1007
|
model.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ec5f2a733488ccbdac6a9661278b89e927260720b732baf7136fd7fb32abae46
|
3 |
+
size 96471677
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
torch
|
3 |
+
torchvision
|
utils.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import transforms
|
3 |
+
|
4 |
+
class Predict():
|
5 |
+
def __init__(self):
|
6 |
+
self.transform = transforms.Compose([
|
7 |
+
transforms.Resize(size=256),
|
8 |
+
transforms.CenterCrop(size=224),
|
9 |
+
transforms.ToTensor(),
|
10 |
+
transforms.Normalize([0.485, 0.456, 0.406],[0.229, 0.224, 0.225])
|
11 |
+
])
|
12 |
+
|
13 |
+
self.model = torch.load("model.pt",map_location=torch.device('cpu'))
|
14 |
+
self.class_dict = {0: 'COVID', 1: 'Normal', 2: 'Pneumonia'}
|
15 |
+
|
16 |
+
def predict(self, image):
|
17 |
+
test_image_tensor = self.transform(image)
|
18 |
+
if torch.cuda.is_available():
|
19 |
+
test_image_tensor = test_image_tensor.view(1, 3, 224, 224).cuda()
|
20 |
+
else:
|
21 |
+
test_image_tensor = test_image_tensor.view(1, 3, 224, 224)
|
22 |
+
with torch.no_grad():
|
23 |
+
self.model.eval()
|
24 |
+
# Model outputs log probabilities
|
25 |
+
out = self.model(test_image_tensor)
|
26 |
+
ps = torch.exp(out)
|
27 |
+
topk, topclass = ps.topk(1, dim=1)
|
28 |
+
|
29 |
+
return self.class_dict[topclass.cpu().numpy()[0][0]], topk.numpy()[0][0]
|