-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathensemble_classifier.py
474 lines (380 loc) · 16.5 KB
/
ensemble_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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# giang temporarily switch EnsembleModel to EnsembleModelFileLevelCNN
# for lineLSTM and lineGRU, just use EnsembleModel
# for hunk-level FCN, use EnsembleModelHunkLevelFCN
import os
import json
import utils
from torch.utils.data import DataLoader
from entities import EnsembleDataset, EnsemblePcaDataset
from model import EnsembleModel
import torch
from torch import cuda
from torch import nn as nn
from transformers import AdamW
from transformers import get_scheduler
from torch.nn import functional as F
from tqdm import tqdm
import numpy as np
from sklearn import metrics
import csv
import argparse
from variant_ensemble import write_feature_to_file
directory = os.path.dirname(os.path.abspath(__file__))
dataset_name = 'ase_dataset_sept_19_2021.csv'
# dataset_name = 'huawei_sub_dataset.csv'
FINAL_MODEL_PATH = None
JAVA_RESULT_PATH = None
PYTHON_RESULT_PATH = None
TRAIN_BATCH_SIZE = 128
VALIDATION_BATCH_SIZE = 128
TEST_BATCH_SIZE = 128
TRAIN_PARAMS = {'batch_size': TRAIN_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
VALIDATION_PARAMS = {'batch_size': VALIDATION_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
TEST_PARAMS = {'batch_size': TEST_BATCH_SIZE, 'shuffle': True, 'num_workers': 8}
LEARNING_RATE = 1e-5
NUMBER_OF_EPOCHS = 20
use_cuda = cuda.is_available()
device = torch.device("cuda:0" if use_cuda else "cpu")
random_seed = 109
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
def write_prob_to_file(file_path, urls, probs):
with open(file_path, 'w') as file:
writer = csv.writer(file)
for i, url in enumerate(urls):
writer.writerow([url, probs[i]])
def read_features_from_file(file_path):
file_path = os.path.join(directory, file_path)
with open(file_path, 'r') as reader:
data = json.loads(reader.read())
return data
def read_feature_list(file_path_list, reshape=False, need_list=False, need_extend=False):
url_to_feature = {}
for file_path in file_path_list:
data = read_features_from_file(file_path)
for url, feature in data.items():
if url not in url_to_feature:
url_to_feature[url] = []
if not need_extend:
url_to_feature[url].append(feature)
else:
url_to_feature[url].extend(feature)
if not reshape:
return url_to_feature
else:
url_to_combined = {}
if reshape:
for url in url_to_feature.keys():
features = url_to_feature[url]
combine = []
for feature in features:
combine.extend(feature)
if not need_list:
combine = torch.FloatTensor(combine)
url_to_combined[url] = combine
return url_to_combined
def predict_test_data(model, testing_generator, device, need_prob=False, need_features=False):
y_pred = []
y_test = []
probs = []
features = []
urls = []
with torch.no_grad():
model.eval()
for ids, url_batch, feature_1, feature_2, feature_3, feature_5, feature_6, feature_7, feature_8, label_batch in tqdm(testing_generator):
feature_1 = feature_1.to(device)
feature_2 = feature_2.to(device)
feature_3 = feature_3.to(device)
feature_5 = feature_5.to(device)
feature_6 = feature_6.to(device)
feature_7 = feature_7.to(device)
feature_8 = feature_8.to(device)
label_batch = label_batch.to(device)
outs, pca_features = model(feature_1, feature_2, feature_3, feature_5, feature_6, feature_7, feature_8, need_features=True)
outs = F.softmax(outs, dim=1)
y_pred.extend(torch.argmax(outs, dim=1).tolist())
y_test.extend(label_batch.tolist())
probs.extend(outs[:, 1].tolist())
urls.extend(list(url_batch))
features.extend(list(pca_features.tolist()))
precision = metrics.precision_score(y_pred=y_pred, y_true=y_test)
recall = metrics.recall_score(y_pred=y_pred, y_true=y_test)
f1 = metrics.f1_score(y_pred=y_pred, y_true=y_test)
try:
auc = metrics.roc_auc_score(y_true=y_test, y_score=probs)
except Exception:
auc = 0
print("Finish testing")
if not need_prob:
return precision, recall, f1, auc
else:
return precision, recall, f1, auc, urls, probs, features
def train(model, learning_rate, number_of_epochs, training_generator, test_java_generator, test_python_generator):
loss_function = nn.NLLLoss()
optimizer = AdamW(model.parameters(), lr=learning_rate)
num_training_steps = NUMBER_OF_EPOCHS * len(training_generator)
lr_scheduler = get_scheduler(
"linear",
optimizer=optimizer,
num_warmup_steps=0,
num_training_steps=num_training_steps
)
train_losses = []
for epoch in range(number_of_epochs):
model.train()
total_loss = 0
current_batch = 0
for ids, url_batch, feature_1, feature_2, feature_3, feature_5, feature_6, feature_7, feature_8, label_batch in tqdm(training_generator):
feature_1 = feature_1.to(device)
feature_2 = feature_2.to(device)
feature_3 = feature_3.to(device)
feature_5 = feature_5.to(device)
feature_6 = feature_6.to(device)
feature_7 = feature_7.to(device)
feature_8 = feature_8.to(device)
label_batch = label_batch.to(device)
outs = model(feature_1, feature_2, feature_3, feature_5, feature_6, feature_7, feature_8)
outs = F.log_softmax(outs, dim=1)
loss = loss_function(outs, label_batch)
train_losses.append(loss.item())
model.zero_grad()
loss.backward()
optimizer.step()
lr_scheduler.step()
total_loss += loss.detach().item()
current_batch += 1
if current_batch % 50 == 0:
print("Train commit iter {}, total loss {}, average loss {}".format(current_batch, np.sum(train_losses),
np.average(train_losses)))
print("epoch {}, training commit loss {}".format(epoch, np.sum(train_losses)))
train_losses = []
model.eval()
print("Result on Java testing dataset...")
precision, recall, f1, auc, urls, probs, features = predict_test_data(model=model,
testing_generator=test_java_generator,
device=device, need_prob=True)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
write_prob_to_file(JAVA_RESULT_PATH, urls, probs)
print("Result on Python testing dataset...")
precision, recall, f1, auc, urls, probs, features = predict_test_data(model=model,
testing_generator=test_python_generator,
device=device, need_prob=True)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
write_prob_to_file(PYTHON_RESULT_PATH, urls, probs)
torch.save(model.state_dict(), FINAL_MODEL_PATH)
return model
def do_train(args):
global FINAL_MODEL_PATH
global JAVA_RESULT_PATH
global PYTHON_RESULT_PATH
FINAL_MODEL_PATH = args.model_path
if FINAL_MODEL_PATH is None or FINAL_MODEL_PATH == '':
raise Exception("Model path must not be None or empty")
JAVA_RESULT_PATH = args.java_result_path
if JAVA_RESULT_PATH is None or JAVA_RESULT_PATH == '':
raise Exception("Java result path must not be None or empty")
PYTHON_RESULT_PATH = args.python_result_path
if PYTHON_RESULT_PATH is None or PYTHON_RESULT_PATH == '':
raise Exception("Java result path must not be None or empty")
variant_to_drop = []
if args.variant_to_drop is not None:
for variant in args.variant_to_drop:
variant_to_drop.append(int(variant))
train_feature_path = [
'features/feature_variant_1_train.txt',
'features/feature_variant_2_train.txt',
'features/feature_variant_3_train.txt',
'features/feature_variant_5_train.txt',
'features/feature_variant_6_train.txt',
'features/feature_variant_7_train.txt',
'features/feature_variant_8_train.txt'
]
val_feature_path = [
'features/feature_variant_1_val.txt',
'features/feature_variant_2_val.txt',
'features/feature_variant_3_val.txt',
'features/feature_variant_5_val.txt',
'features/feature_variant_6_val.txt',
'features/feature_variant_7_val.txt',
'features/feature_variant_8_val.txt'
]
test_java_feature_path = [
'features/feature_variant_1_test_java.txt',
'features/feature_variant_2_test_java.txt',
'features/feature_variant_3_test_java.txt',
'features/feature_variant_5_test_java.txt',
'features/feature_variant_6_test_java.txt',
'features/feature_variant_7_test_java.txt',
'features/feature_variant_8_test_java.txt'
]
test_python_feature_path = [
'features/feature_variant_1_test_python.txt',
'features/feature_variant_2_test_python.txt',
'features/feature_variant_3_test_python.txt',
'features/feature_variant_5_test_python.txt',
'features/feature_variant_6_test_python.txt',
'features/feature_variant_7_test_python.txt',
'features/feature_variant_8_test_python.txt'
]
print("Reading data...")
url_to_features = {}
print("Reading train data")
url_to_features.update(read_feature_list(train_feature_path))
print("Reading test java data")
url_to_features.update(read_feature_list(test_java_feature_path))
print("Reading test python data")
url_to_features.update(read_feature_list(test_python_feature_path))
print("Finish reading")
url_data, label_data = utils.get_data(dataset_name)
feature_data = {}
feature_data['train'] = []
feature_data['test_java'] = []
feature_data['test_python'] = []
for url in url_data['train']:
feature_data['train'].append(url_to_features[url])
for url in url_data['test_java']:
feature_data['test_java'].append(url_to_features[url])
for url in url_data['test_python']:
feature_data['test_python'].append(url_to_features[url])
val_ids, test_java_ids, test_python_ids = [], [], []
index = 0
id_to_url = {}
id_to_label = {}
id_to_feature = {}
for i, url in enumerate(url_data['train']):
val_ids.append(index)
id_to_url[index] = url
id_to_label[index] = label_data['train'][i]
id_to_feature[index] = feature_data['train'][i]
index += 1
for i, url in enumerate(url_data['test_java']):
test_java_ids.append(index)
id_to_url[index] = url
id_to_label[index] = label_data['test_java'][i]
id_to_feature[index] = feature_data['test_java'][i]
index += 1
for i, url in enumerate(url_data['test_python']):
test_python_ids.append(index)
id_to_url[index] = url
id_to_label[index] = label_data['test_python'][i]
id_to_feature[index] = feature_data['test_python'][i]
index += 1
training_set = EnsembleDataset(val_ids, id_to_label, id_to_url, id_to_feature)
test_java_set = EnsembleDataset(test_java_ids, id_to_label, id_to_url, id_to_feature)
test_python_set = EnsembleDataset(test_python_ids, id_to_label, id_to_url, id_to_feature)
training_generator = DataLoader(training_set, **TRAIN_PARAMS)
test_java_generator = DataLoader(test_java_set, **TEST_PARAMS)
test_python_generator = DataLoader(test_python_set, **TEST_PARAMS)
model = EnsembleModel(args.ablation_study, variant_to_drop)
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)
train(model=model,
learning_rate=LEARNING_RATE,
number_of_epochs=NUMBER_OF_EPOCHS,
training_generator=training_generator,
test_java_generator=test_java_generator,
test_python_generator=test_python_generator)
def infer_dataset(model_path, partition, ablation_study, variant_to_drop, prob_path):
# val_feature_path = [
# 'features/feature_variant_1_val.txt',
# 'features/feature_variant_2_val.txt',
# 'features/feature_variant_3_val.txt',
# 'features/feature_variant_5_val.txt',
# 'features/feature_variant_6_val.txt',
# 'features/feature_variant_7_val.txt',
# 'features/feature_variant_8_val.txt'
# ]
test_java_feature_path = [
'features/feature_variant_1_test_java.txt',
'features/feature_variant_2_test_java.txt',
'features/feature_variant_3_test_java.txt',
'features/feature_variant_5_test_java.txt',
'features/feature_variant_6_test_java.txt',
'features/feature_variant_7_test_java.txt',
'features/feature_variant_8_test_java.txt'
]
test_python_feature_path = [
'features/feature_variant_1_test_python.txt',
'features/feature_variant_2_test_python.txt',
'features/feature_variant_3_test_python.txt',
'features/feature_variant_5_test_python.txt',
'features/feature_variant_6_test_python.txt',
'features/feature_variant_7_test_python.txt',
'features/feature_variant_8_test_python.txt'
]
# model = EnsembleModelHunkLevelFCN(False, [])
model = EnsembleModel(ablation_study, variant_to_drop)
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.load_state_dict(torch.load('model/patch_ensemble_model.sav'))
model.to(device)
print("Reading data")
url_to_features = read_feature_list(test_python_feature_path)
print("Finish reading")
url_data, label_data = utils.get_data(dataset_name)
feature_data = {}
feature_data[partition] = []
for url in url_data[partition]:
feature_data[partition].append(url_to_features[url])
val_ids = []
index = 0
id_to_url = {}
id_to_label = {}
id_to_feature = {}
for i, url in enumerate(url_data[partition]):
val_ids.append(index)
id_to_url[index] = url
id_to_label[index] = label_data[partition][i]
id_to_feature[index] = feature_data[partition][i]
index += 1
val_set = EnsembleDataset(val_ids, id_to_label, id_to_url, id_to_feature)
val_generator = DataLoader(val_set, **TEST_PARAMS)
print("Result on dataset...")
precision, recall, f1, auc, urls, probs, features = predict_test_data(model=model,
testing_generator=val_generator,
device=device, need_prob=True)
write_prob_to_file(prob_path, urls, probs)
# write_feature_to_file(feature_path, urls, features)
print("Precision: {}".format(precision))
print("Recall: {}".format(recall))
print("F1: {}".format(f1))
print("AUC: {}".format(auc))
print("-" * 32)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Ensemble Classifier')
parser.add_argument('--ablation_study',
type=bool,
default=False,
help='Do ablation study or not')
parser.add_argument('-v',
'--variant_to_drop',
action='append',
required=False,
help='Select index of variant to drop, 1, 2, 3, 5, 6, 7, 8')
parser.add_argument('--model_path',
type=str,
help='IMPORTANT select path to save model')
parser.add_argument('--java_result_path',
type=str,
help='path to save prediction for Java projects')
parser.add_argument('--python_result_path',
type=str,
help='path to save prediction for Python projects')
args = parser.parse_args()
do_train(args)
# infer_dataset('test_python', 'features/feature_ensemble_test_python.txt')