-
Notifications
You must be signed in to change notification settings - Fork 0
/
breast_eff_net_v2_b1.py
190 lines (137 loc) · 5.71 KB
/
breast_eff_net_v2_b1.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#Full research work coded and implemented by Sheekar Banerjee. The runtime was partially tested by Md. Kamrul Hasan Monir
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import glob, os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers, activations, optimizers, losses, metrics, initializers
from tensorflow.keras.preprocessing import image, image_dataset_from_directory
from tensorflow.keras.applications import MobileNetV3Small, MobileNet, InceptionV3
from tensorflow.keras.applications.efficientnet import preprocess_input, decode_predictions
seed = 42
tf.random.set_seed(seed)
np.random.seed(seed)
dir_path = '/content/drive/MyDrive/Breast Cancer/breast_cancer_data'
IMAGE_SHAPE = (224, 224)
# create prepare_image method
# used to preprocess the image for efficientNet model
def prepare_image(file):
img = image.load_img(file, target_size=IMAGE_SHAPE)
img_array = image.img_to_array(img)
return tf.keras.applications.efficientnet.preprocess_input (img_array)
directories = os.listdir(dir_path) # read the folders
files = [] # save all images for each folder
labels = [] # set for each image the name of it
# read files for each directory
for folder in directories:
fileList = glob.glob(dir_path + '/'+ folder + '/*')
labels.extend([folder for l in fileList])
files.extend(fileList)
len(files), len(labels)
# create two lists to hold only non-mask images and label for each one
selected_files = []
selected_labels = []
for file, label in zip(files, labels):
if 'mask' not in file:
selected_files.append(file)
selected_labels.append(label)
len(selected_files), len(selected_labels)
# the dictionary holds list of images and for each one has its target/label
images = {
'image': [],
'target': []
}
print('Preparing the image...')
for i, (file, label) in enumerate(zip(selected_files, selected_labels)):
images['image'].append(prepare_image(file))
images['target'].append(label)
print('Finished.')
# convert lists to arrays
images['image'] = np.array(images['image'])
images['target'] = np.array(images['target'])
# encode the target
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
images['target'] = le.fit_transform(images['target'])
classes = le.classes_ # get the classes for each target
print(f'the target classes are: {classes}')
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(images['image'], images['target'], test_size=.10)
x_train.shape, x_test.shape, y_train.shape, y_test.shape
from keras.src.applications import EfficientNetV2B1
#Build Model
from keras.applications.efficientnet import EfficientNetV2B1
base_model = EfficientNetV2B1(
include_top=False,
weights='imagenet',
input_shape=(*IMAGE_SHAPE, 3),
classes=3)
# Freeze the base_model
base_model.trainable = False
# append my own layers on the top of the model for Transfer Learning
x = base_model.output
# 1st conv block
x = layers.Conv2D(256, 3, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.GlobalAveragePooling2D(keepdims = True)(x)
# 2nd conv block
x = layers.Conv2D(128, 3, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.GlobalAveragePooling2D(keepdims = True)(x)
# 1st FC layer
x = layers.Flatten()(x)
x = layers.Dense(64)(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
# 2nd FC layer
x = layers.Dense(32, activation = 'relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Activation('relu')(x)
x = layers.Dropout(.2)(x)
x = layers.Dense(3, 'softmax')(x)
incept_model = keras.models.Model(inputs = base_model.input, outputs = x)
# compile the model
incept_model.compile(optimizer=optimizers.RMSprop(.001), loss = losses.sparse_categorical_crossentropy, metrics= [metrics.SparseCategoricalAccuracy()])
incept_model.summary()
earlyStop = keras.callbacks.EarlyStopping(patience=60)
best_model = keras.callbacks.ModelCheckpoint(filepath='breast_VGG16_sheekar.h5', save_best_only=True)
with tf.device('/gpu:0'):
history = incept_model.fit(x_train, y_train, batch_size=32, epochs=100, validation_data=(x_test, y_test), callbacks=[earlyStop, best_model])
# hist = history.history
# plt.plot(hist['loss'], label= 'loss')
# plt.plot(hist['val_loss'], label = 'val_loss')
# plt.plot(hist['sparse_categorical_accuracy'], label='accuracy')
# plt.plot(hist['val_sparse_categorical_accuracy'], label='val_accuracy')
# plt.legend()
# Summarize history for accu
plt.plot(history.history['sparse_categorical_accuracy'])
plt.plot(history.history['val_sparse_categorical_accuracy'])
plt.title('Model AUC')
plt.ylabel('AUC')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left', bbox_to_anchor=(1,1))
plt.show()
# Summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left', bbox_to_anchor=(1,1))
plt.show()
# used to predict the model and visualize the orignal image with title of true and pred values
def predict_image(img_path, label):
img1 = prepare_image(img_path) # preprocess the image
res = incept_model.predict(np.expand_dims(img1, axis = 0)) # predict the image
pred = classes[np.argmax(res)]
# Visualize the image
img = image.load_img(img_path)
plt.imshow(np.array(img))
plt.title(f'True: {label}\nPredicted: {pred}')
predict_image('/content/drive/MyDrive/Breast Cancer/benign.png', 'benign')
predict_image('/content/drive/MyDrive/Breast Cancer/malignant.png', 'malignant')
predict_image('/content/drive/MyDrive/Breast Cancer/normal.png', 'normal')