-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN_molecule_classifier.py
289 lines (226 loc) · 8.21 KB
/
CNN_molecule_classifier.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Flatten, Dropout, Activation, Dense
from keras.optimizers import SGD, Adam
import glob, os
import random
import numpy as np
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import matplotlib as mpl
if os.environ.get('DISPLAY', '') == '':
print('no display found. using non interactive Agg backend')
mpl.use('Agg')
import matplotlib.pyplot as plt
CWD = "/usr4/cs542/tnmcneil"
def build_model():
classifier = Sequential()
# layer 1
classifier.add(Conv2D(32,(3,3), input_shape=(960, 139, 2)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
# layer 2
classifier.add(Conv2D(64,(3,3)))
classifier.add(Activation('relu'))
classifier.add(MaxPooling2D(pool_size=(2,2)))
# flatten
classifier.add(Flatten())
# fully connected layer
classifier.add(Dense(64))
classifier.add(Activation('relu'))
classifier.add(Dropout(0.6))
classifier.add(Dense(2))
classifier.add(Activation('sigmoid'))
#compile model
classifier.compile(optimizer=Adam(lr=0.00001, beta_1=0.9, beta_2=0.99999, epsilon=1e-08, decay=0.0),
loss='binary_crossentropy',
metrics=['accuracy'])
# print summary of model
classifier.summary()
return classifier
# image processing
def process_data():
cwd = CWD + "/column_data"
# read all data
lambda_dir1 = cwd + "/lambda_1"
os.chdir(lambda_dir1)
lambda_data1 = glob.glob("*.jpg")
lambda_data1 = [lambda_dir1 + "/" + data for data in lambda_data1]
lambda_dir2 = cwd + "/lambda_2"
os.chdir(lambda_dir2)
lambda_data2 = glob.glob("*.jpg")
lambda_data2 = [lambda_dir2 + "/" + data for data in lambda_data2]
lambda_dir3 = cwd + "/lambda_3"
os.chdir(lambda_dir3)
lambda_data3 = glob.glob("*.jpg")
lambda_data3 = [lambda_dir3 + "/" + data for data in lambda_data3]
lambda_dir4 = cwd + "/lambda_4"
os.chdir(lambda_dir4)
lambda_data4 = glob.glob("*.jpg")
lambda_data4 = [lambda_dir4 + "/" + data for data in lambda_data4]
lambda_data = lambda_data1 + lambda_data2 + lambda_data3 + lambda_data4
t7_dir1 = cwd + "/t7_1_alligned"
os.chdir(t7_dir1)
t7_data1 = glob.glob("*.jpg")
t7_data1 = [t7_dir1 + "/" + data for data in t7_data1]
t7_dir2 = cwd + "/t7_2_alligned"
os.chdir(t7_dir2)
t7_data2 = glob.glob("*.jpg")
t7_data2 = [t7_dir2 + "/" + data for data in t7_data2]
t7_dir3 = cwd + "/t7_3_alligned"
os.chdir(t7_dir3)
t7_data3 = glob.glob("*.jpg")
t7_data3 = [t7_dir3 + "/" + data for data in t7_data3]
t7_dir4 = cwd + "/t7_4_alligned"
os.chdir(t7_dir4)
t7_data4 = glob.glob("*.jpg")
t7_data4 = [t7_dir4 + "/" + data for data in t7_data4]
t7_data = t7_data1 + t7_data2 + t7_data3 + t7_data4
# train / test / validation data split
random.shuffle(lambda_data)
random.shuffle(t7_data)
lambda_len = len(lambda_data)
t_len = len(t7_data)
l_train_cutoff = int(0.7 * lambda_len)
l_test_cutoff = int(0.2 * lambda_len) + l_train_cutoff
l_val_cutoff = int(0.1 * lambda_len) + l_test_cutoff
t_train_cutoff = int(0.7 * t_len)
t_test_cutoff = int(0.2 * t_len) + t_train_cutoff
t_val_cutoff = int(0.1 * t_len) + t_test_cutoff
lambda_train = lambda_data[:l_train_cutoff]
lambda_test = lambda_data[l_train_cutoff:l_test_cutoff]
lambda_val = lambda_data[l_test_cutoff:]
t7_train = t7_data[:t_train_cutoff]
t7_test = t7_data[t_train_cutoff:t_test_cutoff]
t7_val = t7_data[t_test_cutoff:]
# label data
# [1.0, 0.0] = lambda; [0.0, 1.0] = t7
train_files = lambda_train + t7_train
random.shuffle(train_files)
y_train = [None] * (len(train_files))
i = 0
for i in range(len(train_files)):
if 'lambda' in train_files[i]:
y_train[i] = [1.0, 0.0]
else:
y_train[i] = [0.0, 1.0]
y_train = np.array(y_train)
test_files = lambda_test + t7_test
random.shuffle(test_files)
y_test = [None] * (len(test_files))
j = 0
for j in range(len(test_files)):
if 'lambda' in test_files[j]:
y_test[j] = [1.0, 0.0]
else:
y_test[j] = [0.0, 1.0]
y_test = np.array(y_test)
val_files = lambda_val + t7_val
random.shuffle(val_files)
y_val = [None] * (len(val_files))
k = 0
for k in range(len(val_files)):
if 'lambda' in val_files[k]:
y_val[k] = [1.0, 0.0]
else:
y_val[k] = [0.0, 1.0]
y_val = np.array(y_val)
# convert images to numpy arrays
training_set = np.ndarray(shape=(len(train_files), 960, 139, 2), dtype=np.float32)
i = 0
for _file in train_files:
img = load_img(_file).convert('LA')
x = img_to_array(img)
result = np.zeros((960, 139, 2))
result[:x.shape[0],:x.shape[1]] = x
training_set[i] = result
i += 1
testing_set = np.ndarray(shape=(len(test_files), 960, 139, 2), dtype=np.float32)
j = 0
for _file in test_files:
img = load_img(_file).convert('LA')
x = img_to_array(img)
result = np.zeros((960, 139, 2))
result[:x.shape[0], :x.shape[1]] = x
testing_set[j] = result
j += 1
val_set = np.ndarray(shape=(len(val_files), 960, 139, 2), dtype=np.float32)
k = 0
for _file in val_files:
img = load_img(_file).convert('LA')
x = img_to_array(img)
result = np.zeros((960, 139, 2))
result[:x.shape[0], :x.shape[1]] = x
val_set[k] = result
k += 1
training_set = training_set.astype("float32")/255.0
testing_set = testing_set.astype("float32")/255.0
val_set = val_set.astype("float32")/255.0
return training_set, y_train, testing_set, y_test, val_set, y_val
def calc_precision_recall(prediction, real):
len_test = len(prediction)
incorrect = 0
a = 0
for a in range(len_test):
if not ((prediction[a] == real[a]).all()):
incorrect += 1
proportion_correct = 1 - (incorrect/len_test)
a = 0
tp = 0
fn = 0
tn = 0
fp = 0
for a in range(len_test):
if (real[a] == [1.0, 0.0]).all() and (prediction[a] == [1.0,0.0]).all():
tp += 1
elif (real[a] == [1.0, 0.0]).all() and (prediction[a] == [0.0, 1.0]).all():
fn += 1
elif (real[a] == [0.0, 1.0]).all() and (prediction[a] == [0.0, 1.0]).all():
tn += 1
elif (real[a] == [0.0, 1.0]).all() and (prediction[a] == [1.0, 0.0]).all():
fp += 1
print("true positives = ", tp)
print("false negatives = ", fn)
print("true negatives = ", tn)
print("false positives = ", fp)
print('incorrect:', incorrect)
print('proportion correct:', proportion_correct)
precision_calc = tp / (tp + fp)
recall_calc = tp / (tp + fn)
print("my precision: ", precision_calc)
print("my recall: ", recall_calc)
# Model Training Hyper parameters
EPOCHS = 100
BATCH_SIZE = 32
# create instance of model
classifier = build_model()
training_set, y_train, testing_set, y_test, val_set, y_val = process_data()
hist = classifier.fit(x=training_set, y=y_train, epochs=EPOCHS, verbose=2,
batch_size=BATCH_SIZE, validation_data=(val_set, y_val))
score = classifier.evaluate(testing_set, y_test, verbose=0)
hypothesis = classifier.predict(testing_set)
label = np.empty((len(testing_set), 2))
# summarize history for loss
plt.figure(1)
plt.plot(hist.history["loss"])
plt.plot(hist.history["val_loss"])
plt.title("model loss")
plt.ylabel("loss")
plt.xlabel("epoch")
plt.legend(["train", "val"], loc="upper right")
plt.savefig(CWD + "/loss_history.png", bbox_inches='tight')
# summarize history for accuracy
plt.figure(2)
plt.plot(hist.history["acc"])
plt.plot(hist.history["val_acc"])
plt.title("model accuracy")
plt.ylabel("accuracy")
plt.xlabel("epoch")
plt.legend(["train", "val"], loc="lower right")
plt.savefig(CWD + "/acc_history.png", bbox_inches='tight')
i=0
for i in range(len(testing_set)):
if hypothesis[i][0] > hypothesis[i][1]:
label[i] = [1.0, 0.0]
else:
label[i] = [0.0, 1.0]
calc_precision_recall(label, y_test)