File size: 3,299 Bytes
6d4eaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44f41ed
6d4eaca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76dbafd
6d4eaca
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
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

# Define available models
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")
}

# Load trained models
MODEL_PATHS = {model_name: f"{model_name}_catboost.pkl" for model_name in MODELS}
trained_models = {}

# Load the trained models into memory
for model_name, path in MODEL_PATHS.items():
    if os.path.exists(path):
        trained_models[model_name] = joblib.load(path)

# Define class names (modify based on dataset)
CLASS_NAMES = ['ADI','DEB','LYM','MUC','MUS','NOR','STR','TUM']  # Update with actual labels

# Streamlit UI
st.title("Multi-Model Image Classifier")
st.markdown("Upload an image and select which models and classes to use for prediction.")

# Upload an image
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])

# Select Models
selected_models = st.multiselect("Select models for prediction:", list(trained_models.keys()))

# Select Classes
selected_classes = st.multiselect("Select classes to predict:", CLASS_NAMES, default=CLASS_NAMES)

# Function to preprocess image
def preprocess_image(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    img_array = image.img_to_array(img) / 255.0  # Normalize
    img_array = np.expand_dims(img_array, axis=0)
    return img_array

# Perform prediction
if uploaded_file and selected_models:
    # Read and preprocess the image
    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)

    # Extract Features
    extracted_features = {}
    for model_name in selected_models:
        extracted_features[model_name] = MODELS[model_name].predict(image_np)

    # Predict using each selected CatBoost model
    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]  # Get probabilities
        predictions[model_name] = y_pred

    # Display results
    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}**")