Cainvas
Model Files
cnn_model_skin1.h5
keras
Model
deepSea Compiled Models
cnn_model_skin1.exe
deepSea
Ubuntu

SKIN CANCER DETECTION USING CNN

Credit: AITS Cainvas Community

Photo by SELECTO on Dribbble

In [1]:
import tensorflow as tf
from tensorflow import keras 
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
%matplotlib inline
import tensorflow.keras.backend as K
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image
from pylab import imread,subplot,imshow,show
import cv2
import os
In [2]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/CNN_model_dataset.zip"
!unzip -qo CNN_model_dataset.zip 
!rm CNN_model_dataset.zip
--2021-07-21 16:12:34--  https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/CNN_model_dataset.zip
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.66.84
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.66.84|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 170317173 (162M) [application/x-zip-compressed]
Saving to: ‘CNN_model_dataset.zip’

CNN_model_dataset.z 100%[===================>] 162.43M  97.2MB/s    in 1.7s    

2021-07-21 16:12:36 (97.2 MB/s) - ‘CNN_model_dataset.zip’ saved [170317173/170317173]

In [3]:
fig = plt.figure(figsize=(13, 10))

path = r'data/train/benign/100.jpg'
Image1 = cv2.imread(path,0)

path_1 = r'data/train/benign/1021.jpg'
Image2 = cv2.imread(path_1,0)

path_2 = r'data/train/malignant/19.jpg'
Image3 = cv2.imread(path_2,0)

path_3 = r'data/train/malignant/55.jpg'
Image4 = cv2.imread(path_3,0)

path_4 = r'data/test/benign/105.jpg'
Image5 = cv2.imread(path_4,0)

path_5 = r'data/test/benign/259.jpg'
Image6 = cv2.imread(path_5,0)

fig.add_subplot(3,3,1)
plt.imshow(Image1)
fig.add_subplot(3,3,2)
plt.imshow(Image2)
fig.add_subplot(3,3,3)
plt.imshow(Image3)
fig.add_subplot(3,3,4)
plt.imshow(Image4)
fig.add_subplot(3,3,5)
plt.imshow(Image5)
fig.add_subplot(3,3,6)
plt.imshow(Image6)
Out[3]:
<matplotlib.image.AxesImage at 0x7ffaae905080>

Rescaling

In [4]:
train = ImageDataGenerator(rescale=1./255)
test =  ImageDataGenerator(rescale=1./255)
val =  ImageDataGenerator(rescale=1./255)
In [5]:
train='data/train/'
In [6]:
train_data = tf.keras.preprocessing.image_dataset_from_directory(
    train,
    validation_split=0.25,
    image_size=(224,224),
    batch_size=35,
    subset='training',
    seed=50 )
Found 2637 files belonging to 2 classes.
Using 1978 files for training.
In [7]:
val='data/train/'
In [8]:
val_data = tf.keras.preprocessing.image_dataset_from_directory(
    val,
    validation_split=0.25,
    image_size=(224,224),
    batch_size=35,
    subset='validation',
    seed=50
    )
Found 2637 files belonging to 2 classes.
Using 659 files for validation.
In [9]:
test='data/test/'
In [10]:
test_data=tf.keras.preprocessing.image_dataset_from_directory(
    test,
    image_size=(224,224),
    batch_size=35,
    seed=50
    )
Found 660 files belonging to 2 classes.
In [11]:
class_names = ['Benign', 'Malignant']
In [12]:
train_data.class_names = class_names
val_data.class_names = class_names
In [13]:
plt.figure(figsize=(14, 14))
for images, labels in train_data.take(1):
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(train_data.class_names[labels[i]])
        plt.axis("off")