|
import streamlit as st |
|
import pickle |
|
from PIL import Image |
|
import numpy as np |
|
import os |
|
import numpy as np |
|
import pandas as pd |
|
import random |
|
import logging |
|
from PIL import Image |
|
|
|
|
|
import tensorflow as tf |
|
import keras |
|
import keras.backend as K |
|
from keras.models import Model, Sequential |
|
from keras.layers import Input, Dense, Flatten, Dropout, BatchNormalization |
|
from keras.layers import Conv2D, SeparableConv2D, MaxPool2D, LeakyReLU, Activation |
|
from keras.optimizers import Adam |
|
from keras.preprocessing.image import ImageDataGenerator |
|
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping |
|
|
|
|
|
seed = 232 |
|
np.random.seed(seed) |
|
tf.random.set_seed(seed) |
|
|
|
|
|
def build_model(input_shape): |
|
inputs = Input(shape=input_shape) |
|
|
|
|
|
x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(inputs) |
|
x = Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = MaxPool2D(pool_size=(2, 2))(x) |
|
|
|
|
|
x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = SeparableConv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = BatchNormalization()(x) |
|
x = MaxPool2D(pool_size=(2, 2))(x) |
|
|
|
|
|
x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = SeparableConv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = BatchNormalization()(x) |
|
x = MaxPool2D(pool_size=(2, 2))(x) |
|
|
|
|
|
x = SeparableConv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = SeparableConv2D(filters=512, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = BatchNormalization()(x) |
|
x = MaxPool2D(pool_size=(2, 2))(x) |
|
x = Dropout(rate=0.2)(x) |
|
|
|
|
|
x = SeparableConv2D(filters=1024, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = SeparableConv2D(filters=1024, kernel_size=(3, 3), activation='relu', padding='same')(x) |
|
x = BatchNormalization()(x) |
|
x = MaxPool2D(pool_size=(2, 2))(x) |
|
x = Dropout(rate=0.2)(x) |
|
|
|
|
|
x = Flatten()(x) |
|
x = Dense(units=1024, activation='relu')(x) |
|
x = Dropout(rate=0.7)(x) |
|
x = Dense(units=512, activation='relu')(x) |
|
x = Dropout(rate=0.5)(x) |
|
x = Dense(units=256, activation='relu')(x) |
|
x = Dropout(rate=0.3)(x) |
|
|
|
|
|
output = Dense(units=1, activation='sigmoid')(x) |
|
|
|
model = Model(inputs=inputs, outputs=output) |
|
return model |
|
|
|
|
|
input_shape = (150, 150, 3) |
|
model = build_model(input_shape) |
|
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) |
|
|
|
|
|
model.load_weights('best_weights.hdf5') |
|
|
|
|
|
|
|
def preprocess_image(image): |
|
image = image.convert('RGB') |
|
image = image.resize((150, 150)) |
|
image = np.array(image) / 255.0 |
|
image = np.expand_dims(image, axis=0) |
|
return image |
|
|
|
st.title("Pneumonia Detection from Chest X-ray") |
|
|
|
|
|
uploaded_file = st.file_uploader("Choose a chest X-ray image...", type="jpeg") |
|
|
|
if uploaded_file is not None: |
|
image = Image.open(uploaded_file) |
|
st.image(image, caption='Uploaded X-ray.', use_column_width=True) |
|
st.write("") |
|
st.write("Classifying...") |
|
|
|
processed_image = preprocess_image(image) |
|
prediction = model.predict(processed_image) |
|
result = np.round(prediction).astype(int)[0][0] |
|
|
|
if result == 0: |
|
st.write("The image is **not infected**.") |
|
else: |
|
st.write("The image is **infected**.") |