-
Notifications
You must be signed in to change notification settings - Fork 56
/
cnn_keras.py
226 lines (173 loc) · 6.47 KB
/
cnn_keras.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
import numpy as np
import pandas as pd
from keras.callbacks import EarlyStopping
from keras.callbacks import TensorBoard
from keras.layers import Dense, Activation, Flatten, Dropout
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, recall_score, f1_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
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()
'''
First set of three layers
Image size: 256 x 256
nb_filters = 32
kernel_size = (2,2)
'''
model.add(Conv2D(nb_filters, (kernel_size[0], kernel_size[1]),
padding='valid',
strides=1,
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)))
'''
Second set of three layers
Image Size: 128 x 128
nb_filters = 64
kernel_size = 4,4
'''
nb_filters = 64
kernel_size = (4, 4)
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(Conv2D(nb_filters, (kernel_size[0], kernel_size[1])))
# model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
'''
Third set of three layers
Image Size: 64 x 64
nb_filters = 128
kernel_size = 8,8
'''
nb_filters = 128
kernel_size = (8, 8)
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(Conv2D(nb_filters, (kernel_size[0], kernel_size[1])))
# model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(12, 12)))
model.add(Flatten())
print("Model flattened out to: ", model.output_shape)
model.add(Dense(4096))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(4096))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model = multi_gpu_model(model, gpus=nb_gpus)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
print(model.summary())
stop = EarlyStopping(monitor='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
if __name__ == '__main__':
batch_size = 100
nb_classes = 15
nb_epoch = 20
nb_gpus = 8
img_rows, img_cols = 512, 512
channels = 1
nb_filters = 32
kernel_size = (2, 2)
# Import data
labels = pd.read_csv("../data/sample_labels.csv")
X = np.load("../data/X_sample.npy")
y = labels.Finding_Labels
# y = np.array(pd.get_dummies(y))
label_encoder = LabelEncoder()
y = label_encoder.fit_transform(y)
y = y.reshape(-1, 1)
print("Splitting data into test/ train datasets")
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
print("Reshaping Data")
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, channels)
X_test = X_test.reshape(X_test.shape[0], 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)
model = cnn_model(X_train, y_train, kernel_size, nb_filters, channels, nb_epoch, batch_size, nb_classes, nb_gpus)
print("Predicting")
y_pred = model.predict(X_test)
y_test = np.argmax(y_test, axis=1)
y_pred = np.argmax(y_pred, axis=1)
precision = precision_score(y_test, y_pred, average='weighted')
recall = recall_score(y_test, y_pred, average='weighted')
f1 = f1_score(y_test, y_pred, average="weighted")
print("Precision: ", precision)
print("Recall: ", recall)
print("F1: ", f1)