File size: 3,081 Bytes
25bc673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import tensorflow as tf
import pickle
from tensorflow.keras.preprocessing import image
import numpy as np

# Function to preprocess the image
def preprocess_image(img_path, img_height, img_width, model_type="CNN"):
    # Load the image and convert to grayscale
    img = image.load_img(img_path, target_size=(img_height, img_width), color_mode='grayscale')
    img_array = image.img_to_array(img)

    # Normalize the image array
    img_array = img_array / 255.0  # Normalize for all models

    if model_type in ["Logistic Regression", "Decision Tree"]:
        img_array = img_array.flatten()  # Flatten for Logistic Regression and Decision Tree
        img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension
    else:
        img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension for CNN and ANN

    return img_array

# Load the Keras model
@st.cache_resource
def load_keras_model():
    return tf.keras.models.load_model('best_model_.keras')

# Load the other models
@st.cache_resource
def load_pickle_model(model_path):
    with open(model_path, 'rb') as f:
        return pickle.load(f)

# Define model paths and validation accuracies
models_info = {
    "ANN Model": {
        "path": "ann_model.pkl",
        "accuracy": 0.60
    },
    "Decision Tree": {
        "path": "decision_tree_classifier_model.pkl",
        "accuracy": 0.70
    },
    "Logistic Regression": {
        "path": "logistic_regression_model.pkl",
        "accuracy": 0.60
    },
    "CNN Model": {
        "path": "best_model_.keras",
        "accuracy": 0.90
    }
}

# Streamlit UI
st.title("X-ray Image Classification")
st.write("Upload an X-ray image to classify it as Normal or Pneumonia.")

# Model selection
model_name = st.selectbox("Choose a model:", list(models_info.keys()))

# Display selected model accuracy
st.write(f"Selected Model: {model_name}")
st.write(f"Validation Accuracy: {models_info[model_name]['accuracy'] * 100:.2f}%")

# Load the selected model
if model_name == "CNN Model":
    model = load_keras_model()
else:
    model = load_pickle_model(models_info[model_name]["path"])

# File uploader for image
uploaded_file = st.file_uploader("Choose an X-ray image...", type="jpeg")

if uploaded_file is not None:
    with open("temp.jpeg", "wb") as f:
        f.write(uploaded_file.getbuffer())
    
    # Use the appropriate preprocessing for the selected model
    img_height, img_width = 224, 224  # Use the same dimensions as used during training
    preprocessed_img = preprocess_image(
        "temp.jpeg", 
        img_height, 
        img_width,
        model_type=model_name  # Pass the model name directly
    )
    
    st.image(uploaded_file, caption="Uploaded X-ray Image", use_column_width=True)
    
    # Prediction logic
    prediction = model.predict(preprocessed_img)
    prediction_label = "Pneumonia" if prediction[0] > 0.5 else "Normal"

    st.write(f"Prediction: {prediction_label} (Model: {model_name})")