Upload 5 files
Browse files- app.py +131 -0
- cnn_model.h5 +3 -0
- elastic_net_model.joblib +3 -0
- hybrid_yolo_cnn_model.pth +3 -0
- yolo_model.pth +3 -0
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import torch.nn as nn
|
6 |
+
import joblib
|
7 |
+
import tensorflow as tf
|
8 |
+
from PIL import Image, ImageDraw
|
9 |
+
import os
|
10 |
+
|
11 |
+
# Load the saved models
|
12 |
+
# --- YOLOv5 Model ---
|
13 |
+
class YOLOv5(nn.Module):
|
14 |
+
def __init__(self):
|
15 |
+
super(YOLOv5, self).__init__()
|
16 |
+
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
|
17 |
+
self.pool = nn.MaxPool2d(2, 2)
|
18 |
+
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
|
19 |
+
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
|
20 |
+
self.fc1 = nn.Linear(64 * 16 * 16, 512)
|
21 |
+
self.fc2 = nn.Linear(512, 128)
|
22 |
+
self.fc3 = nn.Linear(128, 7)
|
23 |
+
|
24 |
+
def forward(self, x):
|
25 |
+
x = self.pool(torch.relu(self.conv1(x)))
|
26 |
+
x = self.pool(torch.relu(self.conv2(x)))
|
27 |
+
x = self.pool(torch.relu(self.conv3(x)))
|
28 |
+
x = x.reshape(-1, 64 * 16 * 16)
|
29 |
+
x = torch.relu(self.fc1(x))
|
30 |
+
x = torch.relu(self.fc2(x))
|
31 |
+
x = self.fc3(x)
|
32 |
+
return x
|
33 |
+
|
34 |
+
yolo_model = YOLOv5()
|
35 |
+
yolo_model.load_state_dict(torch.load('yolo_model.pth'))
|
36 |
+
yolo_model.eval()
|
37 |
+
|
38 |
+
# --- CNN Model ---
|
39 |
+
cnn_model = tf.keras.models.load_model('cnn_model.h5')
|
40 |
+
|
41 |
+
# --- ElasticNet Model ---
|
42 |
+
elastic_net_model = joblib.load('elastic_net_model.joblib')
|
43 |
+
|
44 |
+
# --- Hybrid YOLO-CNN Model ---
|
45 |
+
class HybridYOLOCNN(nn.Module):
|
46 |
+
def __init__(self):
|
47 |
+
super(HybridYOLOCNN, self).__init__()
|
48 |
+
self.conv1 = nn.Conv2d(3, 16, kernel_size=3, stride=1, padding=1)
|
49 |
+
self.pool = nn.MaxPool2d(2, 2)
|
50 |
+
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)
|
51 |
+
self.conv3 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
|
52 |
+
self.fc1 = nn.Linear(64 * 16 * 16, 512)
|
53 |
+
self.fc2 = nn.Linear(512, 128)
|
54 |
+
self.fc3 = nn.Linear(128, 7)
|
55 |
+
|
56 |
+
def forward(self, x):
|
57 |
+
x = self.pool(torch.relu(self.conv1(x)))
|
58 |
+
x = self.pool(torch.relu(self.conv2(x)))
|
59 |
+
x = self.pool(torch.relu(self.conv3(x)))
|
60 |
+
x = x.reshape(-1, 64 * 16 * 16)
|
61 |
+
x = torch.relu(self.fc1(x))
|
62 |
+
x = torch.relu(self.fc2(x))
|
63 |
+
x = self.fc3(x)
|
64 |
+
return x
|
65 |
+
|
66 |
+
hybrid_model = HybridYOLOCNN()
|
67 |
+
hybrid_model.load_state_dict(torch.load('hybrid_yolo_cnn_model.pth'))
|
68 |
+
hybrid_model.eval()
|
69 |
+
|
70 |
+
# Define prediction functions with bounding boxes for each model
|
71 |
+
def predict_with_box(img, model_type="yolo"):
|
72 |
+
img_tensor = torch.tensor(img).unsqueeze(0).permute(0, 3, 1, 2).float()
|
73 |
+
|
74 |
+
# Make predictions based on the model type
|
75 |
+
if model_type == "yolo":
|
76 |
+
output = yolo_model(img_tensor)
|
77 |
+
pred_class = torch.argmax(output, dim=1).item()
|
78 |
+
x_center, y_center = 64, 64 # Center position
|
79 |
+
elif model_type == "hybrid":
|
80 |
+
output = hybrid_model(img_tensor)
|
81 |
+
pred_class = torch.argmax(output, dim=1).item()
|
82 |
+
x_center, y_center = 64, 64 # Center position
|
83 |
+
elif model_type == "cnn":
|
84 |
+
img_array = np.expand_dims(img, axis=0) / 255.0
|
85 |
+
output = cnn_model.predict(img_array)
|
86 |
+
pred_class = np.argmax(output, axis=1)[0]
|
87 |
+
x_center, y_center = 64, 64 # Center position
|
88 |
+
elif model_type == "elastic_net":
|
89 |
+
img_flattened = img.flatten().reshape(1, -1)
|
90 |
+
pred_class = int(np.clip(np.round(elastic_net_model.predict(img_flattened)), 0, 3)[0])
|
91 |
+
x_center, y_center = 84, 84 # Slightly offset from center
|
92 |
+
|
93 |
+
# Draw red square on the image
|
94 |
+
img_with_box = Image.fromarray(img)
|
95 |
+
draw = ImageDraw.Draw(img_with_box)
|
96 |
+
box_size = 20 # Example box size
|
97 |
+
box = (x_center - box_size, y_center - box_size, x_center + box_size, y_center + box_size)
|
98 |
+
draw.rectangle(box, outline="red", width=3)
|
99 |
+
|
100 |
+
return img_with_box, pred_class
|
101 |
+
|
102 |
+
# Streamlit app
|
103 |
+
st.title("Skin Lesion Classification with Multiple Models")
|
104 |
+
st.write("Upload up to 10 images, and get predictions from each model with highlighted areas.")
|
105 |
+
|
106 |
+
uploaded_files = st.file_uploader("Choose images", accept_multiple_files=True, type=["jpg", "jpeg", "png"])
|
107 |
+
if uploaded_files:
|
108 |
+
for file in uploaded_files[:10]: # Limit to 10 images
|
109 |
+
# Load and resize image
|
110 |
+
img = Image.open(file).convert("RGB")
|
111 |
+
img = img.resize((128, 128))
|
112 |
+
img_np = np.array(img)
|
113 |
+
|
114 |
+
st.image(img, caption="Uploaded Image", use_column_width=True)
|
115 |
+
|
116 |
+
# Model Predictions with bounding boxes
|
117 |
+
yolo_img, yolo_pred = predict_with_box(img_np, model_type="yolo")
|
118 |
+
cnn_img, cnn_pred = predict_with_box(img_np, model_type="cnn")
|
119 |
+
elastic_net_img, elastic_net_pred = predict_with_box(img_np, model_type="elastic_net")
|
120 |
+
hybrid_img, hybrid_pred = predict_with_box(img_np, model_type="hybrid")
|
121 |
+
|
122 |
+
# Display results in columns with images
|
123 |
+
col1, col2, col3, col4 = st.columns(4)
|
124 |
+
with col1:
|
125 |
+
st.image(yolo_img, caption=f"YOLOv5 Prediction: {yolo_pred}", use_column_width=True)
|
126 |
+
with col2:
|
127 |
+
st.image(cnn_img, caption=f"CNN Prediction: {cnn_pred}", use_column_width=True)
|
128 |
+
with col3:
|
129 |
+
st.image(elastic_net_img, caption=f"ElasticNet Prediction: {elastic_net_pred}", use_column_width=True)
|
130 |
+
with col4:
|
131 |
+
st.image(hybrid_img, caption=f"Hybrid YOLO-CNN Prediction: {hybrid_pred}", use_column_width=True)
|
cnn_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1e41a9d0fb11c52ee7c1ced99b5ed120fdcdcca261bd27d736606c15fde6d740
|
3 |
+
size 88749880
|
elastic_net_model.joblib
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b4290d86eab12aa70a9497098c402652c9d1a714bfe8d6a1ede0662a604cdcd1
|
3 |
+
size 393863
|
hybrid_yolo_cnn_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a4ad9e2f8b64728f3e2974df6fd6250aa222f7efc7f33efebd7ed80635763d01
|
3 |
+
size 33921554
|
yolo_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f202ad7762f75130119df1763ed6e80c848701e62342d5ce2a3b7e5b6626c104
|
3 |
+
size 33921378
|