-
Notifications
You must be signed in to change notification settings - Fork 0
/
skin_cancer_detection.py
70 lines (63 loc) · 4 KB
/
skin_cancer_detection.py
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
# The primary goal of this work is to build up a Model of Skin Cancer Detection System utilizing Machine Learning Algorithms. After experimenting with many different architectures for the CNN model It is found that adding the BatchNormalization layer after each Dense, and MaxPooling2D layer can help increase the validation accuracy. In future, a mobile application can be made.
# reference: https://www.kaggle.com/kmader/skin-cancer-mnist-ham10000/discussion/183083
# Data: https://www.kaggle.com/kmader/skin-cancer-mnist-ham10000
# https://keras.io/api/models/sequential/
# https://keras.io/api/layers/core_layers/dense/
# https://keras.io/api/layers/merging_layers/add/
# https://keras.io/api/layers/convolution_layers/convolution2d
# https://keras.io/api/layers/convolution_layers/convolution2d
# https://www.tensorflow.org/api_docs/python/tf/keras/layers/BatchNormalization
import tensorflow as tf
from keras.callbacks import ReduceLROnPlateau
from imblearn.over_sampling import RandomOverSampler
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from tensorflow import keras
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix , classification_report
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow.keras.layers import Conv2D, Flatten, Dense, MaxPool2D
from tensorflow.keras.models import Sequential
classes = {
0: ("actinic keratoses and intraepithelial carcinomae(Cancer)"),
1: ("basal cell carcinoma(Cancer)"),
2: ("benign keratosis-like lesions(Non-Cancerous)"),
3: ("dermatofibroma(Non-Cancerous)"),
4: ("melanocytic nevi(Non-Cancerous)"),
5: ("pyogenic granulomas and hemorrhage(Can lead to cancer)"),
6: ("melanoma(Cancer)"),
}
def My_Model():
input_ = keras.layers.Input(shape = [28,28,3])
x = keras.layers.Conv2D(32 , (3,3) , activation='relu',padding='same' , kernel_initializer='he_normal')(input_)
x = keras.layers.MaxPooling2D()(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(64 , (3,3) , activation='relu',padding='same' , kernel_initializer='he_normal')(x)
x = keras.layers.Conv2D(64 , (3,3) , activation='relu',padding='same' , kernel_initializer='he_normal')(x)
x = keras.layers.MaxPooling2D()(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(128 , (3,3) , activation='relu',padding='same' , kernel_initializer='he_normal')(x)
x = keras.layers.Conv2D(128 , (3,3) , activation='relu',padding='same' , kernel_initializer='he_normal')(x)
x = keras.layers.MaxPooling2D()(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(256 , (3,3) , activation='relu' ,padding='same', kernel_initializer='he_normal')(x)
x = keras.layers.Conv2D(256 , (3,3) , activation='relu' ,padding='same', kernel_initializer='he_normal')(x)
x = keras.layers.MaxPooling2D()(x)
flatten = keras.layers.Flatten()(x)
classifier = keras.layers.Dropout(rate = 0.2)(flatten)
classifier = keras.layers.Dense(units = 256 , activation = 'relu' , kernel_initializer = 'he_normal')(classifier)
classifier = keras.layers.BatchNormalization()(classifier)
classifier = keras.layers.Dense(units = 128 , activation = 'relu' , kernel_initializer = 'he_normal')(classifier)
classifier = keras.layers.BatchNormalization()(classifier)
classifier = keras.layers.Dense(units = 64 , activation = 'relu' , kernel_initializer = 'he_normal')(classifier)
classifier = keras.layers.BatchNormalization()(classifier)
classifier = keras.layers.Dense(units = 32 , activation = 'relu' , kernel_initializer = 'he_normal' , kernel_regularizer=keras.regularizers.L1L2())(classifier)
classifier = keras.layers.BatchNormalization()(classifier)
classifier = keras.layers.Dense(units = 7 , activation='softmax' ,kernel_initializer="glorot_uniform" , name = 'classifier')(classifier)
return keras.models.Model(inputs = input_ ,outputs = classifier )
model = My_Model()
model.load_weights("Skin_Classification.h5")