Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,118 +1,82 @@
|
|
| 1 |
import numpy as np
|
| 2 |
-
import tempfile
|
| 3 |
import pandas as pd
|
| 4 |
-
import tensorflow as tf
|
| 5 |
-
from tensorflow import keras
|
| 6 |
-
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization, GlobalAveragePooling2D
|
| 7 |
-
from tensorflow.keras.models import Model, load_model, Sequential
|
| 8 |
-
from tensorflow.keras.optimizers import Adam
|
| 9 |
-
from tensorflow.keras.metrics import Precision, Recall
|
| 10 |
-
from tensorflow.keras.callbacks import EarlyStopping
|
| 11 |
-
from sklearn.utils.class_weight import compute_class_weight
|
| 12 |
-
from sklearn.model_selection import train_test_split
|
| 13 |
-
from tensorflow.image import resize
|
| 14 |
import cv2
|
| 15 |
-
|
| 16 |
-
from tensorflow
|
| 17 |
-
import matplotlib.pyplot as plt
|
| 18 |
-
import warnings
|
| 19 |
-
import warnings
|
| 20 |
-
warnings.filterwarnings("ignore")
|
| 21 |
-
|
| 22 |
-
# print ('modules loaded')
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
import streamlit as st
|
| 26 |
-
import pandas as pd
|
| 27 |
-
import numpy as np
|
| 28 |
from PIL import Image
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
st.title("Skin Cancer Classification App")
|
| 32 |
|
|
|
|
| 33 |
models = {
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
}
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Allow user to select model
|
| 41 |
model_name = st.selectbox("Choose a model", list(models.keys()))
|
| 42 |
model = models[model_name]
|
| 43 |
|
| 44 |
# Upload Image
|
| 45 |
-
|
| 46 |
file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 47 |
-
# file ='hmnist_28_28_RGB.csv'
|
| 48 |
-
|
| 49 |
-
print(file)
|
| 50 |
-
|
| 51 |
-
true_file=pd.read_csv("HAM10000_metadata.csv")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# true_file.apply(lambda x: x["image_id"] ==file)
|
| 55 |
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
2 :('bkl', 'benign keratosis-like lesions'), 1:('bcc' , ' basal cell carcinoma'),
|
| 68 |
-
5: ('vasc', ' pyogenic granulomas and hemorrhage'),
|
| 69 |
-
0: ('akiec', 'Actinic keratoses and intraepithelial carcinomae'),
|
| 70 |
-
3: ('df', 'dermatofibroma')}
|
| 71 |
-
|
| 72 |
-
classes_map = {'nv': 'melanocytic nevi',
|
| 73 |
-
'mel': 'melanoma',
|
| 74 |
-
'bkl':'benign keratosis-like lesions',
|
| 75 |
-
'bcc':' basal cell carcinoma',
|
| 76 |
-
'vasc': ' pyogenic granulomas and hemorrhage',
|
| 77 |
-
'akiec': 'Actinic keratoses and intraepithelial carcinomae',
|
| 78 |
-
'df': 'dermatofibroma'}
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if file is not None:
|
| 82 |
-
|
| 83 |
file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
|
| 84 |
opencv_image = cv2.imdecode(file_bytes, 1)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
# temp_dir = tempfile.TemporaryDirectory()
|
| 88 |
-
# temp_file_path = temp_dir.name + "/" + file.name
|
| 89 |
-
|
| 90 |
-
# # Save the uploaded file to the temporary directory
|
| 91 |
-
# with open(temp_file_path, "wb") as f:
|
| 92 |
-
# f.write(file.read())
|
| 93 |
-
|
| 94 |
-
# img = cv2.imread(file)
|
| 95 |
-
# cv2_imshow(img)
|
| 96 |
img1 = cv2.resize(opencv_image, (32, 32))
|
| 97 |
-
result = model.predict(img1.reshape(1, 32, 32, 3))
|
| 98 |
-
max_prob = max(result[0])
|
| 99 |
-
class_ind = list(result[0]).index(max_prob)
|
| 100 |
-
class_name = classes[class_ind]
|
| 101 |
-
# print(class_name)
|
| 102 |
-
# count+=1
|
| 103 |
-
# if count>10:
|
| 104 |
-
# break
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
# df = pd.read_csv(file)
|
| 108 |
-
# # Get first row
|
| 109 |
-
# img_reshaped = image_resize(df)
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
# Display image and result
|
| 117 |
col1, col2 = st.columns(2)
|
| 118 |
with col1:
|
|
@@ -125,38 +89,6 @@ if file is not None:
|
|
| 125 |
name = file.name.split(".")[0]
|
| 126 |
if name in true_file['image_id'].values:
|
| 127 |
st.write("True Label: ", classes_map[true_file.loc[true_file['image_id']==name, 'dx'].iloc[0]])
|
| 128 |
-
st.write("Prediction:",class_name[1])
|
| 129 |
else:
|
| 130 |
st.write("No match")
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
# st.write(file.name)
|
| 134 |
-
# st.write("True Label",true_file[true_file.image_id==file.name]["dx"][0])
|
| 135 |
-
# st.write("Prediction",class_name[0])
|
| 136 |
-
# st.metric("Category:", class_name[1])
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
# from google.colab.patches import cv2_imshow
|
| 148 |
-
# srcdir = '/kaggle/input/skin-cancer-mnist-ham10000/HAM10000_images_part_1'
|
| 149 |
-
# count=0
|
| 150 |
-
# for temp in os.listdir(srcdir):
|
| 151 |
-
# img = cv2.imread(os.path.join(srcdir, temp))
|
| 152 |
-
# cv2.imwrite(temp, img)
|
| 153 |
-
# cv2_imshow(img)
|
| 154 |
-
# img = cv2.resize(img, (28, 28))
|
| 155 |
-
# result = model.predict(img.reshape(1, 28, 28, 3))
|
| 156 |
-
# max_prob = max(result[0])
|
| 157 |
-
# class_ind = list(result[0]).index(max_prob)
|
| 158 |
-
# class_name = classes[class_ind]
|
| 159 |
-
# print(class_name)
|
| 160 |
-
# count+=1
|
| 161 |
-
# if count>10:
|
| 162 |
-
# break
|
|
|
|
| 1 |
import numpy as np
|
|
|
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import cv2
|
| 4 |
+
import torch
|
| 5 |
+
from tensorflow import keras
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
+
from transformers import ViTForImageClassification, ViTFeatureExtractor
|
| 8 |
+
from tensorflow.keras.models import load_model
|
| 9 |
+
import streamlit as st
|
| 10 |
|
| 11 |
st.title("Skin Cancer Classification App")
|
| 12 |
|
| 13 |
+
# Load TensorFlow models
|
| 14 |
models = {
|
| 15 |
+
"Le_Net": load_model('LeNet_5.h5'),
|
| 16 |
+
"Simple_CNN": load_model('Simple CNN.h5'),
|
| 17 |
+
"Alex_Net": load_model('AlexNet.h5'),
|
| 18 |
+
"Deeper_CNN": load_model('Deeper CNN.h5'),
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Load PyTorch ViT model
|
| 22 |
+
vit_model = ViTForImageClassification.from_pretrained('google/vit-base-patch16-224', num_labels=7)
|
| 23 |
+
vit_model.load_state_dict(torch.load('./vit_skin_cancer_model.pth'))
|
| 24 |
+
vit_model.eval() # Set the model to evaluation mode
|
| 25 |
+
|
| 26 |
+
# Add the PyTorch model to the models dictionary
|
| 27 |
+
models["ViT_Model"] = vit_model
|
| 28 |
+
|
| 29 |
# Allow user to select model
|
| 30 |
model_name = st.selectbox("Choose a model", list(models.keys()))
|
| 31 |
model = models[model_name]
|
| 32 |
|
| 33 |
# Upload Image
|
|
|
|
| 34 |
file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
true_file = pd.read_csv("HAM10000_metadata.csv")
|
| 37 |
|
| 38 |
+
classes = {
|
| 39 |
+
4: ('nv', 'melanocytic nevi'),
|
| 40 |
+
6: ('mel', 'melanoma'),
|
| 41 |
+
2: ('bkl', 'benign keratosis-like lesions'),
|
| 42 |
+
1: ('bcc', 'basal cell carcinoma'),
|
| 43 |
+
5: ('vasc', 'pyogenic granulomas and hemorrhage'),
|
| 44 |
+
0: ('akiec', 'Actinic keratoses and intraepithelial carcinomae'),
|
| 45 |
+
3: ('df', 'dermatofibroma')
|
| 46 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
classes_map = {
|
| 49 |
+
'nv': 'melanocytic nevi',
|
| 50 |
+
'mel': 'melanoma',
|
| 51 |
+
'bkl': 'benign keratosis-like lesions',
|
| 52 |
+
'bcc': 'basal cell carcinoma',
|
| 53 |
+
'vasc': 'pyogenic granulomas and hemorrhage',
|
| 54 |
+
'akiec': 'Actinic keratoses and intraepithelial carcinomae',
|
| 55 |
+
'df': 'dermatofibroma'
|
| 56 |
+
}
|
| 57 |
|
| 58 |
if file is not None:
|
|
|
|
| 59 |
file_bytes = np.asarray(bytearray(file.read()), dtype=np.uint8)
|
| 60 |
opencv_image = cv2.imdecode(file_bytes, 1)
|
| 61 |
|
| 62 |
+
# Resize image for TensorFlow models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
img1 = cv2.resize(opencv_image, (32, 32))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
if model_name == "ViT_Model":
|
| 66 |
+
# PyTorch model inference
|
| 67 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained('google/vit-base-patch16-224')
|
| 68 |
+
image = feature_extractor(images=opencv_image, return_tensors="pt")['pixel_values']
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
outputs = model(image)
|
| 71 |
+
class_ind = outputs.logits.argmax(-1).item()
|
| 72 |
+
class_name = classes[class_ind]
|
| 73 |
+
else:
|
| 74 |
+
# TensorFlow model inference
|
| 75 |
+
result = model.predict(img1.reshape(1, 32, 32, 3))
|
| 76 |
+
max_prob = max(result[0])
|
| 77 |
+
class_ind = list(result[0]).index(max_prob)
|
| 78 |
+
class_name = classes[class_ind]
|
| 79 |
|
|
|
|
| 80 |
# Display image and result
|
| 81 |
col1, col2 = st.columns(2)
|
| 82 |
with col1:
|
|
|
|
| 89 |
name = file.name.split(".")[0]
|
| 90 |
if name in true_file['image_id'].values:
|
| 91 |
st.write("True Label: ", classes_map[true_file.loc[true_file['image_id']==name, 'dx'].iloc[0]])
|
| 92 |
+
st.write("Prediction:", class_name[1])
|
| 93 |
else:
|
| 94 |
st.write("No match")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|