-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_multi.py
241 lines (159 loc) · 6.29 KB
/
cnn_multi.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers.convolutional import Conv2D
from keras.utils import np_utils
from keras import backend as K
from keras.callbacks import EarlyStopping
from keras.callbacks import TensorBoard
import pandas as pd
import numpy as np
from keras.layers.normalization import BatchNormalization
from keras.optimizers import SGD
from sklearn.model_selection import train_test_split
from sklearn.metrics import recall_score
from sklearn.metrics import precision_score
from sklearn.utils import class_weight
import os
np.random.seed(1337)
def split_data(X, y, test_data_size):
'''
Split data into test and training datasets.
INPUT
X: NumPy array of arrays
y: Pandas series, which are the labels for input array X
test_data_size: size of test/train split. Value from 0 to 1
OUPUT
Four arrays: X_train, X_test, y_train, and y_test
'''
return train_test_split(X, y, test_size=test_data_size, random_state=42)
def reshape_data(arr, img_rows, img_cols, channels):
'''
Reshapes the data into format for CNN.
INPUT
arr: Array of NumPy arrays.
img_rows: Image height
img_cols: Image width
channels: Specify if the image is grayscale (1) or RGB (3)
OUTPUT
Reshaped array of NumPy arrays.
'''
return arr.reshape(arr.shape[0], img_rows, img_cols, channels)
def cnn_model(X_train, X_test, y_train, y_test, kernel_size, nb_filters, channels, nb_epoch, batch_size, nb_classes):
'''
Define and run the Convolutional Neural Network
INPUT
X_train: Array of NumPy arrays
X_test: Array of NumPy arrays
y_train: Array of labels
y_test: Array of labels
kernel_size: Initial size of kernel
nb_filters: Initial number of filters
channels: Specify if the image is grayscale (1) or RGB (3)
nb_epoch: Number of epochs
batch_size: Batch size for the model
nb_classes: Number of classes for classification
OUTPUT
Fitted CNN model
'''
model = Sequential()
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1]),
padding='valid',
strides=4,
input_shape=(img_rows, img_cols, channels)))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1])))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1])))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
kernel_size = (16,16)
model.add(Conv2D(64, (kernel_size[0], kernel_size[1])))
model.add(Activation('relu'))
# model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
print("Model flattened out to: ", model.output_shape)
model.add(Dense(128))
model.add(Activation('sigmoid'))
model.add(Dropout(0.25))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss = 'categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
stop = EarlyStopping(monitor='val_acc',
min_delta=0.001,
patience=2,
verbose=0,
mode='auto')
tensor_board = TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)
model.fit(X_train,y_train, batch_size=batch_size, epochs=nb_epoch,
verbose=1,
validation_split=0.2,
class_weight=weights,
callbacks=[stop, tensor_board])
return model
def save_model(model, score, model_name):
'''
Saves Keras model to an h5 file, based on precision_score
INPUT
model: Keras model object to be saved
score: Score to determine if model should be saved.
model_name: name of model to be saved
'''
if score >= 0.75:
print("Saving Model")
model.save("../models/" + model_name + "_recall_" + str(round(score,4)) + ".h5")
else:
print("Model Not Saved. Score: ", score)
if __name__ == '__main__':
# Specify GPU's to Use
os.environ["CUDA_VISIBLE_DEVICES"]="0,1,2,3"
# Specify parameters before model is run.
batch_size = 1000
nb_classes = 5
nb_epoch = 30
img_rows, img_cols = 256, 256
channels = 3
nb_filters = 32
kernel_size = (8,8)
# Import data
labels = pd.read_csv("../labels/trainLabels_master_256_v2.csv")
X = np.load("../data/X_train_256_v2.npy")
y = np.array(labels['level'])
# Class Weights (for imbalanced classes)
print("Computing Class Weights")
weights = class_weight.compute_class_weight('balanced', np.unique(y), y)
print("Splitting data into test/ train datasets")
X_train, X_test, y_train, y_test = split_data(X, y, 0.2)
print("Reshaping Data")
X_train = reshape_data(X_train, img_rows, img_cols, channels)
X_test = reshape_data(X_test, img_rows, img_cols, channels)
print("X_train Shape: ", X_train.shape)
print("X_test Shape: ", X_test.shape)
input_shape = (img_rows, img_cols, channels)
print("Normalizing Data")
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
y_train = np_utils.to_categorical(y_train, nb_classes)
y_test = np_utils.to_categorical(y_test, nb_classes)
print("y_train Shape: ", y_train.shape)
print("y_test Shape: ", y_test.shape)
print("Training Model")
model = cnn_model(X_train, X_test, y_train, y_test, kernel_size, nb_filters, channels, nb_epoch, batch_size, nb_classes)
print("Predicting")
y_pred = model.predict(X_test)
score = model.evaluate(X_test, y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
y_pred = [np.argmax(y) for y in y_pred]
y_test = [np.argmax(y) for y in y_test]
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
print("Precision: ", precision)
print("Recall: ", recall)
save_model(model=model, score=recall, model_name="DR_Two_Classes")
print("Completed")