Suneel Varma
Suneel Varma
Suneel Varma
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 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_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)
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]:
/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]:
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]:
In [16]:
clusters
Out[16]:
In [18]:
plot_any([new_img])
In [19]:
In [20]:
In [22]:
In [23]:
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
)
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'
)
In [25]:
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
)
In [28]:
In [31]:
import math
from sklearn.metrics import classification_report, confusion_matrix
# Predicting
predictions = model.predict(test_generator, steps=steps)
predicted_classes = (predictions > 0.5).astype(int)
print(class_report)
print(conf_matrix)
[[1357 424]
[ 288 1492]]
In [32]:
import seaborn as sns
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)
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()
plt.tight_layout()
plt.show()