-
Notifications
You must be signed in to change notification settings - Fork 4
/
breast_cancer_detection_ai_project.py
158 lines (112 loc) · 4.59 KB
/
breast_cancer_detection_ai_project.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
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
"""
# Breast Cancer Image Classification Using CNN
"""
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
"""# Data Importation
**Importing basic libraries**
"""
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras import Sequential
import matplotlib.pyplot as plt
from PIL import Image
import os
import pathlib
import random
"""**Defining the path**"""
path = '/content/drive/MyDrive/Dataset_Images'
data_dir = pathlib.Path(path)
"""**Getting class names**"""
class_names = np.array([item.name for item in data_dir.glob("*")])
class_names
"""**Define paths and image count**"""
benignPath = pathlib.Path(os.path.join(data_dir,'benign'))
normalPath = pathlib.Path(os.path.join(data_dir,'normal'))
malignantPath = pathlib.Path(os.path.join(data_dir,'malignant'))
"""**Image count**"""
benignImageCount = len(list(benignPath.glob('*.png')))
malignantImageCount = len(list(malignantPath.glob('*.png')))
normalImageCount = len(list(normalPath.glob('*.png')))
totalImageCount = benignImageCount + malignantImageCount + normalImageCount
print("Total number of Images: ", totalImageCount)
print("No. of Benign (non-dangerous) Images: {}({})".format(benignImageCount, round(benignImageCount*100/totalImageCount, 2)))
print("No. of Malignant (dangerous) Images: {}({})".format(malignantImageCount, round(malignantImageCount*100/totalImageCount, 2)))
print("No. of Normal (No Traces) Images: {}({})".format(normalImageCount, round(normalImageCount*100/totalImageCount, 2)))
"""# Build the CNN"""
batch_size = 32
img_height = 224
img_width = 224
"""**Separating data sets**"""
from tensorflow.keras.utils import image_dataset_from_directory
train_data = image_dataset_from_directory(data_dir,validation_split=0.2,subset="training",seed=123,image_size=(img_height, img_width),batch_size=batch_size)
val_data = image_dataset_from_directory(data_dir,validation_split=0.2,subset="validation",seed=123,image_size=(img_height,img_width),batch_size=batch_size)
"""# Define the Model
**Roadmap**
##### We rescale images add a Dropout to avoid the overfitting as we have 4 class the last layer contain the number of class and we have softmax as activation,it will give us a pourcentage of each class and we'll choice the maximum pourcentage as the class
"""
# 7 layer CNN Model Architecture with 3 Convolution layer each followed by max pooling layer
# Filter size =3X3 and Activation function = Relu
from tensorflow.keras import layers
model = tf.keras.Sequential([
layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.5),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(3,activation="softmax")
])
"""# Compile the Model"""
model.compile(optimizer="Adam",
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=["accuracy"])
epochs = 2
history = model.fit(train_data,
epochs=epochs,
validation_data=val_data,
batch_size=batch_size)
"""**Keys**"""
history.history.keys()
"""# Accuracy vs Lost"""
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs_range = range(epochs)
plt.figure(figsize=(8,8))
plt.subplot(1,2,1)
plt.plot(epochs_range,acc,label='Accuracy')
plt.plot(epochs_range,val_acc,label="Validation Accuracy")
plt.legend()
plt.subplot(1,2,2)
plt.plot(epochs_range,loss,label='Loss')
plt.plot(epochs_range,val_loss,label="Validation Loss")
plt.legend()
plt.show()
"""**Evaluating - İt return the lost and accuracy**"""
model.evaluate(val_data)
"""**Model summary**"""
model.summary()
"""# Predictions Testing Model"""
plt.figure(figsize=(15, 15))
class_names = val_data.class_names
result = ' | False'
for images, labels in val_data.take(1):
for i in range(25):
ax = plt.subplot(5, 5, i + 1)
img = images[i].numpy().astype("uint8")
img = tf.expand_dims(img, axis=0)
predictions = model.predict(img)
predicted_class = np.argmax(predictions)
if class_names[predicted_class] == class_names[labels[i]]:
result = ' | TRUE'
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[predicted_class]+result)
plt.axis("off")