davron04 commited on
Commit
b6d4fe2
·
verified ·
1 Parent(s): e7de3fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -1,21 +1,21 @@
1
  import streamlit as st
2
  import tensorflow as tf
3
  import torch
 
4
  import numpy as np
5
  from PIL import Image
6
  from torchvision import transforms, models
7
 
8
  torch_model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V2)
9
- torch_model.fc = torch.nn.Sequential(
10
- torch.nn.Dropout(0.5),
11
- torch.nn.Linear(2048, 256),
12
- torch.nn.ReLU(),
13
- torch.nn.Linear(256, 1),
14
  )
15
 
16
  tf_model = tf.keras.models.load_model('./models/tensorflow_model.keras')
17
  torch_model.load_state_dict(torch.load('./models/pytorch_model.pth', weights_only=True, map_location='cpu'))
18
- torch_model.to('cpu')
19
  torch_model.eval()
20
 
21
  selected_model = st.selectbox(
@@ -35,7 +35,7 @@ if uploaded_file is not None:
35
  image = Image.open(uploaded_file).convert("RGB")
36
 
37
  # Display Uploaded Image
38
- st.image(image, caption="Uploaded X-ray", use_column_width=True)
39
 
40
  image_size = 256 # Match model input size
41
  if selected_model == "PyTorch":
@@ -62,6 +62,5 @@ if uploaded_file is not None:
62
  prediction = tf_model.predict(image_array)[0][0] # Extract single value
63
 
64
  # Display Prediction
65
- result = "Pneumonia Detected 🛑" if prediction > 0.5 else "No Pneumonia ✅"
66
- st.write(f"**Prediction:** {result} ({prediction:.4f})")
67
 
 
1
  import streamlit as st
2
  import tensorflow as tf
3
  import torch
4
+ import torch.nn as nn
5
  import numpy as np
6
  from PIL import Image
7
  from torchvision import transforms, models
8
 
9
  torch_model = models.resnet50(weights=models.ResNet50_Weights.IMAGENET1K_V2)
10
+ torch_model.fc = nn.Sequential(
11
+ nn.Dropout(0.5),
12
+ nn.Linear(2048, 256),
13
+ nn.ReLU(),
14
+ nn.Linear(256, 1),
15
  )
16
 
17
  tf_model = tf.keras.models.load_model('./models/tensorflow_model.keras')
18
  torch_model.load_state_dict(torch.load('./models/pytorch_model.pth', weights_only=True, map_location='cpu'))
 
19
  torch_model.eval()
20
 
21
  selected_model = st.selectbox(
 
35
  image = Image.open(uploaded_file).convert("RGB")
36
 
37
  # Display Uploaded Image
38
+ st.image(image, caption="Uploaded X-ray", use_container_width=True)
39
 
40
  image_size = 256 # Match model input size
41
  if selected_model == "PyTorch":
 
62
  prediction = tf_model.predict(image_array)[0][0] # Extract single value
63
 
64
  # Display Prediction
65
+ st.write(f"**According to the model, there is a {prediction:.2f}% chance of having pneumonia!**")
 
66