-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn.py
194 lines (145 loc) · 5.73 KB
/
cnn.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
import numpy as np
import pandas as pd
from keras.callbacks import EarlyStopping
from keras.callbacks import TensorBoard
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import MaxPooling2D
from keras.layers.convolutional import Conv2D
from keras.models import Sequential
from keras.utils import np_utils
from keras.utils import multi_gpu_model
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.model_selection import train_test_split
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, y_train, kernel_size, nb_filters, channels, nb_epoch, batch_size, nb_classes, nb_gpus):
"""
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=1,
input_shape=(img_rows, img_cols, channels), activation="relu"))
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1]), activation="relu"))
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1]), activation="relu"))
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 = multi_gpu_model(model, gpus=nb_gpus)
model.compile(loss='binary_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='auto',
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 parameters before model is run.
batch_size = 512
nb_classes = 2
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([1 if l >= 1 else 0 for l in labels['level']])
# y = np.array(labels['level'])
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, y_train, kernel_size, nb_filters, channels, nb_epoch, batch_size,
nb_classes, nb_gpus=8)
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_test = np.argmax(y_test, axis=1)
y_pred = np.argmax(y_pred, axis=1)
precision = precision_score(y_test, y_pred)
recall = recall_score(y_test, y_pred)
print("Precision: ", precision)
print("Recall: ", recall)
save_model(model=model, score=recall, model_name="DR_Two_Classes")
print("Completed")