forked from lv-jiajun/S2FVD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_eval.py
More file actions
441 lines (393 loc) · 17 KB
/
Copy pathtrain_eval.py
File metadata and controls
441 lines (393 loc) · 17 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import io
import math
import time
import dgl
import torch
import torch as th
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
from torch.utils.data import DataLoader
from ParameterConfig import ParameterConfig
def collate(samples):
# The input `samples` is a list of pairs
# (graph, label).
# * unpacks a list or tuple into position arguments.
# ** unpacks a dictionary into keyword arguments.
texts, texts1, db = map(list, zip(*samples))
graphs, labels2 = map(list, zip(*db))
batched_graph = dgl.batch(graphs).to(ParameterConfig.device)
texts = [aa.tolist() for aa in texts]
# texts1 = [aa1.tolist() for aa1 in texts1]
texts1 = [aa1 for aa1 in texts1]
return batched_graph, th.tensor(texts).to(ParameterConfig.device), \
texts1, th.tensor(labels2).to(ParameterConfig.device)
def plot_train_validation_acc_loss(fig_prefix, history):
# acc curve during the training
fig = plt.figure()
acc = history['acc']
val_acc = history['val_acc']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Train accuracy')
plt.plot(epochs, val_acc, 'b', label='Test accuracy')
plt.title('Train and Test accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(['train', 'test'], loc='lower left')
# plt.show()
fig.savefig(fig_prefix + '#accuracy-curve.eps')
plt.close(fig)
# loss curve during the training
fig = plt.figure()
plt.plot(epochs, history['loss'])
plt.plot(epochs, history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
# plt.show()
fig.savefig(fig_prefix + '#loss-curve.eps')
plt.close(fig)
def model_train(fig_prefix, trainset, model, loss_func, optimizer, scheduler, validationset=None):
best_val_acc = 0.
best_model = fig_prefix + '.pt'
# Use PyTorch's DataLoader and the collate function defined to batch data
data_loader = DataLoader(trainset, batch_size=ParameterConfig.BATCH_SIZE, shuffle=True,
collate_fn=collate, pin_memory=ParameterConfig.PIN_MEM)
start = time.time()
for epoch in range(ParameterConfig.EPOCHES):
epoch_start = time.time()
print('-' * 89)
print("epoch | epochs ", str(epoch), " | ", str(ParameterConfig.EPOCHES))
print("training ...")
train(data_loader, loss_func, model, optimizer)
epoch_end = time.time()
print("time", (epoch_end - epoch_start))
print("validation ...")
if validationset is not None:
epoch_start = time.time()
evaluate_loss_and_acc(model, validationset, loss_func)
print('-' * 89)
scheduler.step()
end = time.time()
def train(data_loader, loss_func, model, optimizer):
model.train()
model.is_train_mode = True
all_predictions = []
all_labels = []
for iter, (bg1, bg, bg2, labels) in enumerate(data_loader):
prediction = model(bg, bg1, bg2)
loss = loss_func(prediction, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
# calculate accuracy
predictions = prediction.detach().cpu()
predictions = np.argmax(predictions.numpy(), axis=-1).tolist()
labels = labels.detach().cpu().numpy().tolist()
all_predictions.extend(predictions)
all_labels.extend(labels)
acc = accuracy_score(all_predictions, all_labels)
f1 = f1_score(all_predictions, all_labels)
prec = precision_score(all_predictions, all_labels)
recall = recall_score(all_predictions, all_labels)
print(
'Loss: %0.4f\tAccuracy: %0.5f\tPrecision: %0.5f\tRecall: %0.5f\tF1: %0.5f' % (loss, acc, prec, recall, f1))
def evaluate_loss_and_acc(model, dataset, loss_func):
data_loader = DataLoader(dataset, batch_size=ParameterConfig.BATCH_SIZE, collate_fn=collate,
pin_memory=ParameterConfig.PIN_MEM)
model.eval() # Turn on the evaluation mode
model.is_train_mode = False
total_loss = 0.
with th.no_grad():
all_predictions = []
all_labels = []
for iter, (bg1, bg, bg2, labels) in enumerate(data_loader):
prediction = model(bg, bg1, bg2)
loss = loss_func(prediction, labels)
total_loss += loss.item()
predictions = prediction.detach().cpu()
predictions = np.argmax(predictions.numpy(), axis=-1).tolist()
labels = labels.detach().cpu().numpy().tolist()
all_predictions.extend(predictions)
all_labels.extend(labels)
acc = accuracy_score(all_predictions, all_labels)
f1 = f1_score(all_predictions, all_labels)
prec = precision_score(all_predictions, all_labels)
recall = recall_score(all_predictions, all_labels)
print(
'Loss: %0.4f\tAccuracy: %0.5f\tPrecision: %0.5f\tRecall: %0.5f\tF1: %0.5f' % (loss, acc, prec, recall, f1))
def model_evaluate(model, dataset, loss_func, fig_prefix=''):
"""
When a model is trained, we can use this method to evaluate
the performance of the model on the test dataset :param model:
:param dataset:
:param loss_func:
:return:
"""
# Use PyTorch's DataLoader and the collate function defined.
data_loader = DataLoader(dataset, batch_size=ParameterConfig.BATCH_SIZE, collate_fn=collate,
pin_memory=ParameterConfig.PIN_MEM)
model.eval() # Turn on the evaluation mode
model.is_train_mode = False
all_preds = []
all_labels = []
total_loss = 0.
correct_sampled = 0
correct_argmax = 0
total_sample_num = 0
flag = 0
with th.no_grad():
for iter, (bg1, bg, bg2, labels) in enumerate(data_loader):
prediction = model(bg, bg1, bg2)
# calculate loss
loss = loss_func(prediction, labels)
total_loss += loss.item()
# calculate accuracy
# label = labels.float().view(-1, 1).to(device)
label = labels.float().view(-1, 1)
probs_Y = th.softmax(prediction,
1) # [[0.5295, 0.4705],[0.8905, 0.1095], [0.9143, 0.0857],[0.4138, 0.5862],...]
if flag == 0:
flag = 1
print(probs_Y)
sampled_Y = th.multinomial(probs_Y, 1)
argmax_Y = th.max(probs_Y, 1)[1].view(-1, 1)
correct_sampled += th.sum(label == sampled_Y.float()).item()
correct_argmax += th.sum(label == argmax_Y.float()).item()
total_sample_num += len(label)
all_preds.append(probs_Y)
all_labels.append(labels)
preds = th.cat(all_preds, dim=0)
print('weidupreds' + str(preds.ndim)) # weidupreds2
ground_truth = th.cat(all_labels, dim=0).cpu().numpy()
print('weiduground_truth' + str(ground_truth.ndim)) # weiduground_truth1
rst_acc = 'loss: {:5.2f} | accuracy: {:5.2f} | sampled accuracy {:5.2f}'.format(total_loss / (iter + 1),
correct_argmax / total_sample_num * 100,
correct_sampled / total_sample_num * 100)
print(rst_acc)
with open(fig_prefix + '#classification_report.txt', 'w') as f:
f.write(rst_acc + '\n')
f.close()
return preds, ground_truth
def metric_predictions(pre, ground_truth, fig_prefix=''):
'''
# do transformation so as to utilize sklearn APIs
class_num = pre.shape[1]
print("列数是"+str(class_num)) #列数是2
print("行数是" + str(pre.shape[0])) #行数是33042
ground_truth_for_sklearn = []
for idx in ground_truth:
#print("idx:"+str(idx))
label_arr = np.zeros(class_num)
label_arr[idx] = 1
ground_truth_for_sklearn.append(label_arr)
ground_truth_for_sklearn = np.stack(ground_truth_for_sklearn)
# 计算每一类的ROC Curve和AUC-ROC
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(class_num):
fpr[i], tpr[i], thresholds_ = roc_curve(ground_truth_for_sklearn[:, i], pre[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
fpr['micro'], tpr['micro'], _ = roc_curve(ground_truth_for_sklearn.ravel(), pre.ravel())
roc_auc['micro'] = auc(fpr['micro'], tpr['micro'])
# Compute macro-average ROC curve and ROC area
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(class_num)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(class_num):
mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= class_num
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
# Plot all ROC curves
lw = 2
fig = plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
colors = cycle(['r', 'g', 'b', 'c', 'm', 'y', 'aqua', 'darkorange', 'cornflowerblue', 'pink'])
for i, color in zip(range(class_num), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curve of compiler family identification')
plt.legend(loc="lower right")
# plt.show()
fig.savefig(fig_prefix + '#ROC-curve.eps')
plt.close(fig)
# Test,这是干嘛?
for i in range(len(pre)):
max_value = max(pre[i])
for j in range(len(pre[i])):
if max_value == pre[i][j]:
pre[i][j] = 1
else:
pre[i][j] = 0
# 生成分类评估报告
# report_str = str(classification_report(ground_truth_for_sklearn, pre, digits=4))
# with open(fig_prefix + '#classification_report.txt', 'a') as f:
# f.write(report_str)
# f.close()
# print(report_str)
pre = pre[:, 1]
ground_truth_for_sklearn = ground_truth_for_sklearn[:, 1]
print('weiduone'+str(pre.ndim)) #weiduone1
print('weidutwo' + str(ground_truth_for_sklearn.ndim)) #weidutwo1
lr_precision, lr_recall, _ = precision_recall_curve(ground_truth_for_sklearn, pre)
lr_auc = auc(lr_recall, lr_precision)
plt.figure(1) # 创建图表1
plt.title('PR Curve') # give plot a title
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Recall') # make axis labels
plt.ylabel('Precision')
plt.plot(lr_recall,lr_precision,label='PR (area = {0:0.3f})'
''.format(lr_auc))
plt.legend(loc="lower right")
plt.show()
plt.savefig('p-r.png')
plt.figure(2) # 创建图表2
fpr, tpr, thresholds = roc_curve(ground_truth_for_sklearn, pre)
roc_auc = auc(fpr, tpr)
plt.figure(1)
lw = 2
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.title('ROC Curve') # give plot a title
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.plot(fpr, tpr, color='darkorange',lw = lw, label='ROC (area = {0:0.3f})'
''.format(roc_auc))
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.legend(loc="lower right")
plt.show()
#plt.savefig('roc.png')
plt.title('ROC Curve') # give plot a title
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.plot(fpr, tpr)
plt.show()
plt.savefig('roc.png')
#lr_auc = auc(lr_recall, lr_precision)
print('******************************' +'\n')
# print('PR AUC = %.4f' % (lr_auc))
lr_auc = roc_auc_score(ground_truth_for_sklearn, pre) #https://zhuanlan.zhihu.com/p/349516115
# print('ROC AUC = %.4f' % (lr_auc))
for i in range(len(pre)):
if(pre[i]>=0.5):
pre[i] = 1
else:
pre[i] = 0
f1 = f1_score(ground_truth_for_sklearn, pre)
print('F1 = %.4f' % (f1))
accuracy = accuracy_score(ground_truth_for_sklearn, pre)
print('accuracy = %.4f' % (accuracy))
# print('precision = %.4f' % (precision_score(ground_truth_for_sklearn, pre, average='binary', pos_label=0)))
# print('recall = %.4f' % (recall_score(ground_truth_for_sklearn, pre, average='binary', pos_label=0)))
C2 = confusion_matrix(ground_truth_for_sklearn, pre)
TN, FP, FN, TP = C2.ravel()
numerator = (TP * TN) - (FP * FN) # 马修斯相关系数公式分子部分
denominator = math.sqrt((TP + FP) * (TP + FN) * (TN + FP) * (TN + FN)) # 马修斯相关系数公式分母部分
result = numerator / denominator
print('MCC = %.4f' % (result))
return fpr, tpr,roc_auc
'''
# do transformation so as to utilize sklearn APIs
class_num = pre.shape[1]
print("列数是" + str(class_num)) # 列数是26
print("行数是" + str(pre.shape[0])) # 行数是33042
ground_truth_for_sklearn = []
ground_truth_for_sklearn = ground_truth
'''
for idx in ground_truth:
#print("idx:"+str(idx))
label_arr = np.zeros(class_num)
label_arr[idx] = 1
ground_truth_for_sklearn.append(label_arr)
'''
ground_truth_for_sklearn = np.stack(ground_truth_for_sklearn)
'''
# 计算每一类的ROC Curve和AUC-ROC
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(class_num):
fpr[i], tpr[i], thresholds_ = roc_curve(ground_truth_for_sklearn[:, i], pre[:, i])
roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
fpr['micro'], tpr['micro'], _ = roc_curve(ground_truth_for_sklearn.ravel(), pre.ravel())
roc_auc['micro'] = auc(fpr['micro'], tpr['micro'])
# Compute macro-average ROC curve and ROC area
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(class_num)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(class_num):
mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= class_num
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
# Plot all ROC curves
lw = 2
fig = plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
label='micro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["micro"]),
color='deeppink', linestyle=':', linewidth=4)
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
color='navy', linestyle=':', linewidth=4)
colors = cycle(['r', 'g', 'b', 'c', 'm', 'y', 'aqua', 'darkorange', 'cornflowerblue', 'pink'])
for i, color in zip(range(class_num), colors):
plt.plot(fpr[i], tpr[i], color=color, lw=lw,
label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', lw=lw)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC curve of compiler family identification')
plt.legend(loc="lower right")
# plt.show()
fig.savefig(fig_prefix + '#ROC-curve.eps')
plt.close(fig)
'''
pred = torch.max(pre, 1)[1].cpu().numpy()
'''
# Test,这是干嘛?
for i in range(len(pre)):
max_value = max(pre[i])
for j in range(len(pre[i])):
if max_value == pre[i][j]:
pre[i][j] = 1
else:
pre[i][j] = 0
# 生成分类评估报告
'''
accuracy = accuracy_score(ground_truth_for_sklearn, pred)
print('accuracy = %.4f' % (accuracy))
print(' precision', precision_score(ground_truth_for_sklearn, pred, average='weighted'))
print(' recall', recall_score(ground_truth_for_sklearn, pred, average='weighted'))
print(' f1-score', f1_score(ground_truth_for_sklearn, pred, average='weighted'))
print('******************************' + '\n')