|
import streamlit as st |
|
import numpy as np |
|
import cv2 |
|
import joblib |
|
import tensorflow as tf |
|
from tensorflow.keras.applications import ( |
|
ResNet50, VGG16, EfficientNetV2B0, InceptionV3, ResNet101, DenseNet201 |
|
) |
|
from tensorflow.keras.preprocessing import image |
|
import os |
|
|
|
|
|
MODELS = { |
|
'ResNet50': ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling="avg"), |
|
'VGG16': VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling="avg"), |
|
'EfficientNetV2B0': EfficientNetV2B0(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling="avg"), |
|
'InceptionV3': InceptionV3(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling="avg"), |
|
'ResNet101': ResNet101(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling="avg"), |
|
'DenseNet201': DenseNet201(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling="avg") |
|
} |
|
|
|
|
|
MODEL_PATHS = {model_name: f"{model_name}_catboost.pkl" for model_name in MODELS} |
|
trained_models = {} |
|
|
|
|
|
for model_name, path in MODEL_PATHS.items(): |
|
if os.path.exists(path): |
|
trained_models[model_name] = joblib.load(path) |
|
|
|
|
|
CLASS_NAMES = ['ADI','DEB','LYM','MUC','MUS','NOR','STR','TUM'] |
|
|
|
|
|
st.title("Multi-Model Image Classifier") |
|
st.markdown("Upload an image and select which models and classes to use for prediction.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"]) |
|
|
|
|
|
selected_models = st.multiselect("Select models for prediction:", list(trained_models.keys())) |
|
|
|
|
|
selected_classes = st.multiselect("Select classes to predict:", CLASS_NAMES, default=CLASS_NAMES) |
|
|
|
|
|
def preprocess_image(img_path): |
|
img = image.load_img(img_path, target_size=(224, 224)) |
|
img_array = image.img_to_array(img) / 255.0 |
|
img_array = np.expand_dims(img_array, axis=0) |
|
return img_array |
|
|
|
|
|
if uploaded_file and selected_models: |
|
|
|
image_np = np.array(bytearray(uploaded_file.read()), dtype=np.uint8) |
|
image_np = cv2.imdecode(image_np, cv2.IMREAD_COLOR) |
|
image_np = cv2.resize(image_np, (224, 224)) / 255.0 |
|
image_np = np.expand_dims(image_np, axis=0) |
|
|
|
|
|
extracted_features = {} |
|
for model_name in selected_models: |
|
extracted_features[model_name] = MODELS[model_name].predict(image_np) |
|
|
|
|
|
predictions = {} |
|
for model_name in selected_models: |
|
X_input = extracted_features[model_name] |
|
catboost_model = trained_models[model_name] |
|
y_pred = catboost_model.predict_proba(X_input)[0] |
|
predictions[model_name] = y_pred |
|
|
|
|
|
st.subheader("Prediction Results") |
|
|
|
for model_name, y_pred in predictions.items(): |
|
st.write(f"**Model: {model_name}**") |
|
for i, class_name in enumerate(CLASS_NAMES): |
|
if class_name in selected_classes: |
|
st.write(f" - {class_name}: **{y_pred[i]:.4f}**") |
|
|
|
|