Spaces:
Sleeping
Sleeping
File size: 13,620 Bytes
f49f32e 69edec7 f49f32e 69edec7 f49f32e 82ad703 f49f32e 4ecbe00 497a25e f49f32e 87e0228 f6bd375 e948187 150126e e948187 b5d8e6e f49f32e b02fda4 f49f32e |
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# import system libs
import os
import time
import shutil
import itertools
# import data handling tools
import cv2
import numpy as np
import pandas as pd
import seaborn as sns
sns.set_style('darkgrid')
import matplotlib.pyplot as plt
import gradio as gr
# import Deep learning Libraries
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Activation, Dropout, BatchNormalization
from tensorflow.keras.models import Model, load_model, Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix, classification_report
from sklearn.model_selection import train_test_split
from tensorflow.keras.optimizers import Adam, Adamax
from tensorflow.keras import regularizers
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.utils import to_categorical
from PIL import Image
from sklearn.model_selection import train_test_split
# Ignore Warnings
import warnings
warnings.filterwarnings("ignore")
print ('modules loaded')
#---Training-----------------------------
# ! pip install -q kaggle
# from google.colab import files
# files.upload()
# ! mkdir ~/.kaggle
# ! cp kaggle.json ~/.kaggle/
# ! chmod 600 ~/.kaggle/kaggle.json
# ! kaggle datasets list
# !kaggle datasets download -d kmader/skin-cancer-mnist-ham10000
# ! mkdir kaggle
# ! unzip skin-cancer-mnist-ham10000.zip -d kaggle
# data_dir = '/content/kaggle/hmnist_28_28_RGB.csv'
# data = pd.read_csv(data_dir)
# print(data.shape)
# data.head()
# Label = data["label"]
# Data = data.drop(columns=["label"])
# print(data.shape)
# Data.head()
# from imblearn.over_sampling import RandomOverSampler
# oversample = RandomOverSampler()
# Data, Label = oversample.fit_resample(Data, Label)
# print(Data.shape)
# Data = np.array(Data).reshape(-1,28, 28,3)
# print('Shape of Data :', Data.shape)
# Label = np.array(Label)
# Label
# classes = {4: ('nv', ' melanocytic nevi'),
# 6: ('mel', 'melanoma'),
# 2 :('bkl', 'benign keratosis-like lesions'),
# 1:('bcc' , ' basal cell carcinoma'),
# 5: ('vasc', ' pyogenic granulomas and hemorrhage'),
# 0: ('akiec', 'Actinic keratoses and intraepithelial carcinomae'),
# 3: ('df', 'dermatofibroma')}
# X_train , X_test , y_train , y_test = train_test_split(Data , Label , test_size = 0.25 , random_state = 49)
# print(f'X_train shape: {X_train.shape}\nX_test shape: {X_test.shape}')
# print(f'y_train shape: {y_train.shape}\ny_test shape: {y_test.shape}')
# y_train = to_categorical(y_train)
# y_test = to_categorical(y_test)
# datagen = ImageDataGenerator(rescale=(1./255)
# ,rotation_range=10
# ,zoom_range = 0.1
# ,width_shift_range=0.1
# ,height_shift_range=0.1)
# testgen = ImageDataGenerator(rescale=(1./255))
# from keras.callbacks import ReduceLROnPlateau
# learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy'
# , patience = 2
# , verbose=1
# ,factor=0.5
# , min_lr=0.00001)
# model = keras.models.Sequential()
# # Create Model Structure
# model.add(keras.layers.Input(shape=[28, 28, 3]))
# model.add(keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal'))
# model.add(keras.layers.MaxPooling2D())
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal'))
# model.add(keras.layers.Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal'))
# model.add(keras.layers.MaxPooling2D())
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal'))
# model.add(keras.layers.Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal'))
# model.add(keras.layers.MaxPooling2D())
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal'))
# model.add(keras.layers.Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal'))
# model.add(keras.layers.MaxPooling2D())
# model.add(keras.layers.Flatten())
# model.add(keras.layers.Dropout(rate=0.2))
# model.add(keras.layers.Dense(units=256, activation='relu', kernel_initializer='he_normal'))
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Dense(units=128, activation='relu', kernel_initializer='he_normal'))
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Dense(units=64, activation='relu', kernel_initializer='he_normal'))
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Dense(units=32, activation='relu', kernel_initializer='he_normal', kernel_regularizer=keras.regularizers.L1L2()))
# model.add(keras.layers.BatchNormalization())
# model.add(keras.layers.Dense(units=7, activation='softmax', kernel_initializer='glorot_uniform', name='classifier'))
# model.compile(Adamax(learning_rate= 0.001), loss= 'categorical_crossentropy', metrics= ['accuracy'])
# model.summary()
# history = model.fit(X_train ,
# y_train ,
# epochs=25 ,
# batch_size=128,
# validation_data=(X_test , y_test) ,
# callbacks=[learning_rate_reduction])
# def plot_training(hist):
# tr_acc = hist.history['accuracy']
# tr_loss = hist.history['loss']
# val_acc = hist.history['val_accuracy']
# val_loss = hist.history['val_loss']
# index_loss = np.argmin(val_loss)
# val_lowest = val_loss[index_loss]
# index_acc = np.argmax(val_acc)
# acc_highest = val_acc[index_acc]
# plt.figure(figsize= (20, 8))
# plt.style.use('fivethirtyeight')
# Epochs = [i+1 for i in range(len(tr_acc))]
# loss_label = f'best epoch= {str(index_loss + 1)}'
# acc_label = f'best epoch= {str(index_acc + 1)}'
# plt.subplot(1, 2, 1)
# plt.plot(Epochs, tr_loss, 'r', label= 'Training loss')
# plt.plot(Epochs, val_loss, 'g', label= 'Validation loss')
# plt.scatter(index_loss + 1, val_lowest, s= 150, c= 'blue', label= loss_label)
# plt.title('Training and Validation Loss')
# plt.xlabel('Epochs')
# plt.ylabel('Loss')
# plt.legend()
# plt.subplot(1, 2, 2)
# plt.plot(Epochs, tr_acc, 'r', label= 'Training Accuracy')
# plt.plot(Epochs, val_acc, 'g', label= 'Validation Accuracy')
# plt.scatter(index_acc + 1 , acc_highest, s= 150, c= 'blue', label= acc_label)
# plt.title('Training and Validation Accuracy')
# plt.xlabel('Epochs')
# plt.ylabel('Accuracy')
# plt.legend()
# plt.tight_layout
# plt.show()
# plot_training(history)
# train_score = model.evaluate(X_train, y_train, verbose= 1)
# test_score = model.evaluate(X_test, y_test, verbose= 1)
# print("Train Loss: ", train_score[0])
# print("Train Accuracy: ", train_score[1])
# print('-' * 20)
# print("Test Loss: ", test_score[0])
# print("Test Accuracy: ", test_score[1])
# y_true = np.array(y_test)
# y_pred = model.predict(X_test)
# y_pred = np.argmax(y_pred , axis=1)
# y_true = np.argmax(y_true , axis=1)
# classes_labels = []
# for key in classes.keys():
# classes_labels.append(key)
# print(classes_labels)
# # Confusion matrix
# cm = cm = confusion_matrix(y_true, y_pred, labels=classes_labels)
# plt.figure(figsize= (10, 10))
# plt.imshow(cm, interpolation= 'nearest', cmap= plt.cm.Blues)
# plt.title('Confusion Matrix')
# plt.colorbar()
# tick_marks = np.arange(len(classes))
# plt.xticks(tick_marks, classes, rotation= 45)
# plt.yticks(tick_marks, classes)
# thresh = cm.max() / 2.
# for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
# plt.text(j, i, cm[i, j], horizontalalignment= 'center', color= 'white' if cm[i, j] > thresh else 'black')
# plt.tight_layout()
# plt.ylabel('True Label')
# plt.xlabel('Predicted Label')
# plt.show()
# #Save the model
# model.save('skin_cancer_model.h5')
# converter = tf.lite.TFLiteConverter.from_keras_model(model)
# tflite_model = converter.convert()
# print("model converted")
# # Save the model.
# with open('skin_cancer_model.tflite', 'wb') as f:
# f.write(tflite_model)
#Training End------------------------------------------
skin_classes = {4: ('nv', ' melanocytic nevi'),
6: ('mel', 'melanoma'),
2 :('bkl', 'benign keratosis-like lesions'),
1:('bcc' , ' basal cell carcinoma'),
5: ('vasc', ' pyogenic granulomas and hemorrhage'),
0: ('akiec', 'Actinic keratoses and intraepithelial carcinomae'),
3: ('df', 'dermatofibroma')}
#Use saved model
loaded_model = tf.keras.models.load_model('skin_cancer_model.h5', compile=False)
loaded_model.compile(Adamax(learning_rate= 0.001), loss= 'categorical_crossentropy', metrics= ['accuracy'])
def predict_digit(image):
if image is not None:
#Use saved model
loaded_model = tf.keras.models.load_model('skin_cancer_model.h5', compile=False)
loaded_model.compile(Adamax(learning_rate= 0.001), loss= 'categorical_crossentropy', metrics= ['accuracy'])
img = image.resize((28, 28))
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
print(img_array)
predictions = loaded_model.predict(img_array)
print(predictions)
#class_labels = [] # data classes
score = tf.nn.softmax(predictions[0])*100
print(score)
print(skin_classes[np.argmax(score)])
simple = pd.DataFrame(
{
"skin condition": ["akiec", "bcc", "bkl", "df", "nv", "vasc", "mel"],
"probability": score,
"full skin condition": [ 'Actinic keratoses',
' basal cell carcinoma',
'benign keratosis-like lesions',
'dermatofibroma',
' melanocytic nevi',
' pyogenic granulomas and hemorrhage',
'melanoma'],
}
)
predicted_skin_condition=skin_classes[np.argmax(score)][1]+" ("+ skin_classes[np.argmax(score)][0]+")"
return predicted_skin_condition, gr.BarPlot(
simple,
x="skin condition",
y="probability",
x_title="Skin Condition",
y_title="Classification Probabilities",
title="Skin Cancer Classification Probability",
tooltip=["full skin condition", "probability"],
vertical=False,
y_lim=[0, 100],
color="full skin condition"
)
else:
simple_empty = pd.DataFrame(
{
"skin condition": ["akiec", "bcc", "bkl", "df", "nv", "vasc", "mel"],
"probability": [0,0,0,0,0,0,0],
"full skin condition": [ 'Actinic keratoses',
' basal cell carcinoma',
'benign keratosis-like lesions',
'dermatofibroma',
' melanocytic nevi',
' pyogenic granulomas and hemorrhage',
'melanoma'],
}
)
return " ", gr.BarPlot(
simple_empty,
x="skin condition",
y="probability",
x_title="Digits",
y_title="Identification Probabilities",
title="Identification Probability",
tooltip=["full skin condition", "probability"],
vertical=False,
y_lim=[0, 100],
)
skin_images = [
("skin_image/mel.jpg",'mel'),
("skin_image/nv3.jpg",'nv'),
("skin_image/bkl.jpg",'bkl'),
("skin_image/df.jpg",'df'),
("skin_image/akiec.jpg",'akiec'),
("skin_image/bcc.jpg",'bcc'),
("skin_image/vasc.jpg",'vasc'),
("skin_image/nv2.jpg",'nv'),
("skin_image/akiec2.jpg",'akiec'),
("skin_image/bkl2.jpg",'bkl'),
("skin_image/nv.jpg",'nv'),
]
def image_from_gallary(evt: gr.SelectData):
print(evt.index)
return skin_images[evt.index][0]
css='''
#title_head{
text-align: center;
text-weight: bold;
text-size:30px;
}
#name_head{
text-align: center;
}
'''
with gr.Blocks(css=css) as demo:
with gr.Row():
with gr.Column():
gr.Markdown("<h1>Skin Cancer Classifier</h1>", elem_id='title_head')
with gr.Row():
with gr.Column():
with gr.Row():
gr.Markdown("<h3>Browse or Select from given Image</h3>", elem_id='info')
img_upload=gr.Image(type="pil", height=200, width=300)
with gr.Row():
clear=gr.ClearButton(img_upload)
btn=gr.Button("Identify")
with gr.Column():
gry=gr.Gallery(value=skin_images, columns=5, show_label=False, allow_preview=False)
with gr.Row():
with gr.Column():
gr.Markdown("Most probable skin condition")
label=gr.Label("")
with gr.Row():
with gr.Column():
gr.Markdown("Other possible values")
bar = gr.BarPlot()
btn.click(predict_digit,inputs=[img_upload],outputs=[label,bar])
gry.select(image_from_gallary, outputs=img_upload)
demo.launch(debug=True)
|