-
Notifications
You must be signed in to change notification settings - Fork 0
/
EfficientNetB2.py
176 lines (135 loc) · 4.74 KB
/
EfficientNetB2.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
# -*- coding: utf-8 -*-
###Spyder Editor
#import libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import cv2
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tqdm import tqdm
import os
from sklearn.utils import shuffle
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from tensorflow.keras.applications import EfficientNetB2,EfficientNetB6
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
import keras
# %%%
#create labels
classes=[]
filename=r"C:\\Users\diego\OneDrive\Desktop\DS\brain_cancer\Brain_cancer_classification\data"
for sub_folder in os.listdir(os.path.join(filename,'Training')):
classes.append(sub_folder)
print(classes)
# %%%
#resize images and put together Training and Testing folder
X_train = []
y_train = []
image_size = 160
for i in classes:
path_train = os.path.join(filename,'Training',i)
for j in tqdm(os.listdir(path_train)): #Instantly make your loops show a smart progress meter
img = cv2.imread(os.path.join(path_train,j))
img = cv2.resize(img,(image_size, image_size))
X_train.append(img)
y_train.append(i)
path_test = os.path.join(filename,'Testing',i)
for j in tqdm(os.listdir(path_test)):
img = cv2.imread(os.path.join(path_test,j))
img = cv2.resize(img,(image_size,image_size))
X_train.append(img)
y_train.append(i)
X_train = np.array(X_train)
y_train = np.array(y_train)
#%%%
#data augmentation
X_train, y_train = shuffle(X_train,y_train, random_state=42)
datagen = ImageDataGenerator(
rotation_range=7,
width_shift_range=0.05,
height_shift_range=0.05,
zoom_range=0.1,
horizontal_flip=True)
datagen.fit(X_train)
X_train.shape
lb = LabelEncoder()
#train and test splitting
X_train,X_test,y_train,y_test = train_test_split(X_train,y_train, test_size=0.15,random_state=42,stratify=y_train)
labels_train=lb.fit(y_train)
y_train=lb.transform(y_train)
y_test=lb.transform(y_test)
#%%%
print(y_train)
#%%%
#load EfficientNet
EfficientNet=EfficientNetB2(weights='imagenet', include_top=False,input_shape=(image_size,image_size,3))
#%%%
#train the model
tf.random.set_seed(79)
model = EfficientNet.output
model = tf.keras.layers.GlobalAveragePooling2D()(model)
model = tf.keras.layers.Dropout(rate=0.5)(model)
model = tf.keras.layers.Dense(4,activation='softmax')(model)
model = tf.keras.models.Model(inputs=EfficientNet.input, outputs = model)
opt = Adam(
learning_rate=0.00006,
epsilon=1e-08,
clipnorm=1.0)
model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# summarize the model
print(model.summary())
# fit the model
early_stopping_cb=keras.callbacks.EarlyStopping(patience=9,restore_best_weights=True)
#bert_model.trainable = False
history=model.fit(X_train ,y_train,validation_data = (X_test,y_test),epochs=70,
batch_size=12,callbacks=[early_stopping_cb])
#%%%
# change directory
os.chdir(r'C:\\Users\diego\OneDrive\Desktop\DS\brain_cancer\Brain_cancer_classification')
print(os.getcwd())
#save the model
#model.save(os.path.join('models/','EfficientNetB2.h5'))
model.save_weights(os.path.join('models/','EfficientNetB2.h5'))
#plot loss and accuracy
pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
#plt.gca().set_xlim(0,33)
plt.gca().set_ylim(0,1)
plt.savefig(os.path.join('plots/','EfficientNetB2.png'), dpi=500)
loss, accuracy = model.evaluate(X_test,y_test)
#print accuracy
print('Accuracy: %f' % (accuracy*100))
#%%%
#load_weights
image_size = 160
EfficientNet=EfficientNetB2(weights='imagenet', include_top=False,input_shape=(image_size,image_size,3))
model = EfficientNet.output
model = tf.keras.layers.GlobalAveragePooling2D()(model)
model = tf.keras.layers.Dropout(rate=0.5)(model)
model = tf.keras.layers.Dense(4,activation='softmax')(model)
model = tf.keras.models.Model(inputs=EfficientNet.input, outputs = model)
#%%%
#load the model
#model=keras.models.load_model(os.path.join('models/','EfficientNetB2.h5'))
model.load_weights(os.path.join('models/','EfficientNetB2.h5'))
#More details about the model
model.summary()
#loss, accuracy = model.evaluate(X_test,y_test)
#print accuracy
#print('Accuracy: %f' % (accuracy*100))
# visualize activation functions
#for i, layer in enumerate (model.layers):
# print (i, layer)
# try:
# print (" ",layer.activation)
# except AttributeError:
# print(' no activation attribute')
#specific info about each layer
#for i in range(len(model.layers)):
# print(f'{i} {model.layers[i]}: \n{model.layers[i].get_config()} \n')
#info about optimizers
#model.optimizer.get_config()
#%%%