-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
84 lines (69 loc) · 2.71 KB
/
app.py
File metadata and controls
84 lines (69 loc) · 2.71 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
import streamlit as st
import cv2 as cv
import numpy as np
import tensorflow as tf
import keras
from keras import layers
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.callbacks import ReduceLROnPlateau
from sklearn.metrics import classification_report, confusion_matrix
# Function to preprocess the input image
def preprocess_image(image):
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
image = cv.resize(image, (256, 256))
image = image.astype('float32') / 255.0
return image.reshape(-1, 256, 256, 1)
# Class for Monte Carlo Dropout
class MCDropout(Dropout):
def call(self, inputs, training=None):
return super().call(inputs, training=True)
# Load your pre-trained Bayesian CNN model
def create_bayesian_cnn():
model = keras.Sequential([
keras.Input(shape=(256, 256, 1)),
Conv2D(16, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(32, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
MCDropout(0.5),
Dense(1, activation='sigmoid')
])
model.compile(
loss='binary_crossentropy',
optimizer=keras.optimizers.Adam(learning_rate=0.001),
metrics=['accuracy'],
)
return model
# Load the model
cnn_bayesian = create_bayesian_cnn()
cnn_bayesian.load_weights('./tb.weights.h5') # Adjust this path
# Streamlit app layout
st.title("Tuberculosis Image Classifier")
st.write("Upload a chest X-ray image to classify as Normal or Tuberculosis.")
# Image upload
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
# Read the uploaded image
image = cv.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), cv.IMREAD_COLOR)
st.image(image, caption='Uploaded Image', channels='BGR')
st.write("")
# Preprocess the image
preprocessed_image = preprocess_image(image)
# Make predictions
T = 10 # Number of stochastic forward passes
predictions = np.stack([cnn_bayesian.predict(preprocessed_image) for _ in range(T)], axis=0)
# Calculate mean predictions and uncertainty
mean_predictions = np.mean(predictions, axis=0)
predicted_label = (mean_predictions > 0.5).astype('int32')[0][0]
std_dev = np.std(predictions, axis=0)[0][0]
# Display results
if predicted_label == 0:
st.success("The image is classified as Normal.")
else:
st.error("The image is classified as Tuberculosis.")
st.write(f"Mean Prediction: {mean_predictions[0][0]:.4f}")
st.write(f"Uncertainty (Standard Deviation): {std_dev:.4f}")