Suneel Varma

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

In [1]:

import numpy as np
import matplotlib.pyplot as plt
from glob import glob
from skimage.io import imread
from sklearn.feature_extraction import image
from sklearn.cluster import KMeans
from skimage.filters import rank, threshold_otsu
from skimage.morphology import closing, square, disk
from skimage import exposure as hist, data, img_as_float
from skimage.segmentation import chan_vese
from skimage.feature import canny
from skimage.color import rgb2gray
from scipy import ndimage as ndi
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from skimage.filters import rank
from skimage.morphology import disk
import numpy as np

In [2]:

mel_images = glob('/content/drive/MyDrive/Skin_Cancer/DermMel/train_sep/Melanoma/*')[:5]
non_mel_images = glob('/content/drive/MyDrive/Skin_Cancer/DermMel/train_sep/NotMelanoma/
*')[:5]

In [3]:

def binary(image):
return image > threshold_otsu(image)

def equalize(image):
return hist.equalize_hist(image)

#https://homepages.inf.ed.ac.uk/rbf/HIPR2/median.htm

def mean_filter(image, raio_disk):


if image.dtype == bool:
# Convert boolean image to uint8 if necessary
image = image.astype(np.uint8) * 255
return rank.mean_percentile(image, footprint=disk(raio_disk))

def preenche_bords(image):
return ndi.binary_fill_holes(image)

#https://www.unioviedo.es/compnum/labs/PYTHON/intro_image.html

def load_images(paths):
tmp = []
for path in paths:
tmp.append(imread(path))
return tmp

def plot_any(arr, title = ''):


plt.figure(figsize = (15, 25))
for i in range(len(arr)):
plt.subplot(1,len(arr),i + 1)
plt.title(title)
plt.imshow(arr[i]);

def plot_camadas(img):
plt.figure(figsize = (15, 25))
for i in range(3):
plt.subplot(1, 3, i + 1)
plt.imshow(img[:,:,i], cmap = 'gray');
def d2Kmeans(img, k):
return KMeans(
random_state=1,
n_clusters=k,
init='k-means++'
).fit(img.reshape((-1, 1))).labels_.reshape(img.shape)

def merge_segmented_mask_ROI(uri_img, img_kluster):


new_img = uri_img.copy()
for ch in range(3):
new_img[:,:, ch] *= img_kluster
return new_img

def elbow(img, k):


hist = []
for kclusters in range(1, k):
# Removed n_jobs=-1 from this line
Km = KMeans(random_state=1, n_clusters=kclusters, init='k-means++').fit(img.resh
ape((-1,1)))
hist.append(Km.inertia_)

plt.figure(figsize=(15, 8))
plt.grid()
plt.plot(range(1, k), hist, 'o-')
plt.ylabel('Sum of squared distances')
plt.xlabel('k clusters')
plt.title('Elbow Method')
plt.show()

In [4]:

mel = load_images(mel_images)
non_mel = load_images(non_mel_images)

In [5]:

plot_any(mel, 'Melanoma')

In [6]:

plot_any(non_mel, 'Non-Melanoma')

In [7]:

img_selected = mel[1]

In [8]:

elbow(img_selected, 6)
elbow(img_selected, 6)

/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_ini
t` explicitly to suppress the warning
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_ini
t` explicitly to suppress the warning
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_ini
t` explicitly to suppress the warning
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_ini
t` explicitly to suppress the warning
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_ini
t` explicitly to suppress the warning
warnings.warn(

In [9]:

k_klusters = 2

In [10]:

result_gray = d2Kmeans(rgb2gray(img_selected), k_klusters)


result_img = d2Kmeans(img_selected, k_klusters)

/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_ini
t` explicitly to suppress the warning
warnings.warn(
/usr/local/lib/python3.10/dist-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: Th
e default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_ini
t` explicitly to suppress the warning
warnings.warn(

In [11]:

klusters_gray = [result_gray == i for i in range(k_klusters)]


plot_any(klusters_gray)
In [12]:

def select_cluster_index(clusters):
minx = clusters[0].mean()
index = 0
for i in clusters:
if i.mean() < minx:
minx = i.mean()
index += 1
return index

In [13]:

index_kluster = select_cluster_index(klusters_gray)
print(index_kluster)
selecionado = klusters_gray[index_kluster]

In [14]:

for ch in range(3):
img_k = []
for K in range(k_klusters):
img_k.append(result_img[:, :, ch] == K)
plot_any(img_k)
In [15]:

clusters = [(result_img[:,:,1] == K) for K in range(k_klusters)]

In [16]:

clusters

Out[16]:

[array([[False, False, False, ..., False, False, False],


[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
...,
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False],
[False, False, False, ..., False, False, False]]),
array([[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
...,
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True]])]
In [17]:

new_img = merge_segmented_mask_ROI(img_selected, selecionado)

In [18]:

plot_any([new_img])

In [19]:

image_mean_filter = mean_filter(selecionado, 20)


test_binary = binary(image_mean_filter)

In [20]:

plot_any([selecionado, image_mean_filter, test_binary])


In [21]:

final_result = merge_segmented_mask_ROI(img_selected ,test_binary)

In [22]:

plot_any([test_binary, new_img, final_result])

In [23]:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
rescale=1./255, # Normalize pixel values
rotation_range=40, # Random rotations
width_shift_range=0.2, # Random horizontal shifts
height_shift_range=0.2, # Random vertical shifts
shear_range=0.2, # Shear transformations
zoom_range=0.2, # Random zoom
horizontal_flip=True, # Random horizontal flips
fill_mode='nearest' # Strategy for filling in newly created pixels
)

val_datagen = ImageDataGenerator(rescale=1./255) # Only rescaling for validation data

In [24]:

train_generator = train_datagen.flow_from_directory(
'/content/drive/MyDrive/Skin_Cancer/DermMel/train_sep', # Path to the training data
directory
target_size=(224, 224), # Resizes all images to 224x224
batch_size=32,
class_mode='binary' # Since you are doing binary classification
)

validation_generator = val_datagen.flow_from_directory(
'/content/drive/MyDrive/Skin_Cancer/DermMel/valid',
target_size=(224, 224),
batch_size=32,
class_mode='binary'
)

Found 10682 images belonging to 2 classes.


Found 3562 images belonging to 2 classes.

In [25]:

from tensorflow.keras.applications import ResNet50


from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam

# Load the pre-trained ResNet50 model


base_model = ResNet50(weights='imagenet', include_top=False)

# Add custom layers


x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)

# This is the model we will train


model = Model(inputs=base_model.input, outputs=predictions)

# First: train only the top layers


for layer in base_model.layers:
layer.trainable = False

model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'


])

Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet


/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94765736/94765736 [==============================] - 4s 0us/step

WARNING:absl:`lr` is deprecated in Keras optimizer, please use `learning_rate` or use the


legacy optimizer, e.g.,tf.keras.optimizers.legacy.Adam.

In [ ]:

history = model.fit(
train_generator,
steps_per_epoch=train_generator.samples // train_generator.batch_size, # Ensures all
images are used per epoch
epochs=20,
validation_data=validation_generator,
validation_steps=validation_generator.samples // validation_generator.batch_size
)

Epoch 1/20
333/333 [==============================] - 6005s 18s/step - loss: 0.7012 - accuracy: 0.5
846 - val_loss: 0.6450 - val_accuracy: 0.5780
Epoch 2/20
333/333 [==============================] - 186s 558ms/step - loss: 0.6147 - accuracy: 0.
6592 - val_loss: 0.5655 - val_accuracy: 0.7635
Epoch 3/20
333/333 [==============================] - 187s 561ms/step - loss: 0.5735 - accuracy: 0.
7008 - val_loss: 0.5183 - val_accuracy: 0.7376
Epoch 4/20
333/333 [==============================] - 187s 560ms/step - loss: 0.5725 - accuracy: 0.
7040 - val_loss: 0.5149 - val_accuracy: 0.7697
Epoch 5/20
333/333 [==============================] - 187s 561ms/step - loss: 0.5483 - accuracy: 0.
7315 - val_loss: 0.5055 - val_accuracy: 0.7376
Epoch 6/20
333/333 [==============================] - 187s 561ms/step - loss: 0.5375 - accuracy: 0.
7363 - val_loss: 0.5673 - val_accuracy: 0.6709
Epoch 7/20
333/333 [==============================] - 185s 556ms/step - loss: 0.5273 - accuracy: 0.
7441 - val_loss: 0.5367 - val_accuracy: 0.7686
Epoch 8/20
333/333 [==============================] - 188s 563ms/step - loss: 0.5207 - accuracy: 0.
7467 - val_loss: 0.4806 - val_accuracy: 0.7644
Epoch 9/20
333/333 [==============================] - 186s 559ms/step - loss: 0.5123 - accuracy: 0.
7546 - val_loss: 0.4698 - val_accuracy: 0.7759
Epoch 10/20
333/333 [==============================] - 190s 570ms/step - loss: 0.4965 - accuracy: 0.
7671 - val_loss: 0.5075 - val_accuracy: 0.7325
Epoch 11/20
333/333 [==============================] - 189s 568ms/step - loss: 0.5208 - accuracy: 0.
333/333 [==============================] - 189s 568ms/step - loss: 0.5208 - accuracy: 0.
7427 - val_loss: 0.4935 - val_accuracy: 0.7858
Epoch 12/20
333/333 [==============================] - 187s 563ms/step - loss: 0.5122 - accuracy: 0.
7557 - val_loss: 0.4723 - val_accuracy: 0.7919
Epoch 13/20
333/333 [==============================] - 188s 564ms/step - loss: 0.5002 - accuracy: 0.
7614 - val_loss: 0.4844 - val_accuracy: 0.7917
Epoch 14/20
333/333 [==============================] - 187s 562ms/step - loss: 0.4958 - accuracy: 0.
7614 - val_loss: 0.4619 - val_accuracy: 0.7973
Epoch 15/20
333/333 [==============================] - 186s 559ms/step - loss: 0.4917 - accuracy: 0.
7671 - val_loss: 0.4521 - val_accuracy: 0.7829
Epoch 16/20
333/333 [==============================] - 187s 561ms/step - loss: 0.5013 - accuracy: 0.
7592 - val_loss: 0.4858 - val_accuracy: 0.7905
Epoch 17/20
333/333 [==============================] - 185s 554ms/step - loss: 0.4933 - accuracy: 0.
7616 - val_loss: 0.4836 - val_accuracy: 0.7917
Epoch 18/20
333/333 [==============================] - 186s 558ms/step - loss: 0.4804 - accuracy: 0.
7744 - val_loss: 0.5363 - val_accuracy: 0.7086
Epoch 19/20
333/333 [==============================] - 186s 559ms/step - loss: 0.4841 - accuracy: 0.
7746 - val_loss: 0.4434 - val_accuracy: 0.8041
Epoch 20/20
333/333 [==============================] - ETA: 0s - loss: 0.4799 - accuracy: 0.7751

In [27]:

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(
'/content/drive/MyDrive/Skin_Cancer/DermMel/test', # Path to the test data director
y
target_size=(224, 224), # Ensure this matches the input size the model expects
batch_size=32, # Can be adjusted depending on your system's memory
class_mode='binary', # Since it's a binary classification problem
shuffle=False # Do not shuffle to keep data in order for later analysis
)

Found 3561 images belonging to 2 classes.

In [28]:

test_loss, test_accuracy = model.evaluate(test_generator, steps=test_generator.samples /


/ test_generator.batch_size)
print(f"Test Accuracy: {test_accuracy * 100:.2f}%")

111/111 [==============================] - 1163s 11s/step - loss: 0.4406 - accuracy: 0.8


004
Test Accuracy: 80.04%

In [31]:

import math
from sklearn.metrics import classification_report, confusion_matrix

# Ensure correct number of steps


steps = math.ceil(test_generator.samples / test_generator.batch_size)

# Predicting
predictions = model.predict(test_generator, steps=steps)
predicted_classes = (predictions > 0.5).astype(int)

# Adjust true_classes if necessary


true_classes = test_generator.classes[:len(predicted_classes)]

# Generate classification report and confusion matrix


class_report = classification_report(true_classes, predicted_classes, target_names=test_
generator.class_indices.keys())
conf_matrix = confusion_matrix(true_classes, predicted_classes)

print(class_report)
print(conf_matrix)

112/112 [==============================] - 23s 204ms/step


precision recall f1-score support

Melanoma 0.82 0.76 0.79 1781


NotMelanoma 0.78 0.84 0.81 1780

accuracy 0.80 3561


macro avg 0.80 0.80 0.80 3561
weighted avg 0.80 0.80 0.80 3561

[[1357 424]
[ 288 1492]]

In [32]:
import seaborn as sns

def plot_confusion_matrix(cm, class_names):


plt.figure(figsize=(10, 7))
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues", xticklabels=class_names, yticklab
els=class_names)
plt.title('Confusion Matrix')
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.show()
# Assuming 'conf_matrix' is already defined as shown in previous steps
class_names = list(test_generator.class_indices.keys())

plot_confusion_matrix(conf_matrix, class_names)
In [33]:
# Assume 'history' is your history object returned by the fit method
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)

# Plot training and validation accuracy


plt.figure(figsize=(12, 5))

plt.subplot(1, 2, 1)
plt.plot(epochs, acc, label='Training Accuracy')
plt.plot(epochs, val_acc, label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

# Plot training and validation loss


plt.subplot(1, 2, 2)
plt.plot(epochs, loss, label='Training Loss')
plt.plot(epochs, val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.tight_layout()
plt.show()

You might also like