forked from xiaojunxu/dnn-binary-code-similarity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
157 lines (135 loc) · 5.54 KB
/
train.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
import tensorflow as tf
print tf.__version__
#import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
from graphnnSiamese import graphnn
from utils import *
import os
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument('--device', type=str, default='0',
help='visible gpu device')
parser.add_argument('--fea_dim', type=int, default=7,
help='feature dimension')
parser.add_argument('--embed_dim', type=int, default=64,
help='embedding dimension')
parser.add_argument('--embed_depth', type=int, default=2,
help='embedding network depth')
parser.add_argument('--output_dim', type=int, default=64,
help='output layer dimension')
parser.add_argument('--iter_level', type=int, default=5,
help='iteration times')
parser.add_argument('--lr', type=float, default=1e-4,
help='learning rate')
parser.add_argument('--epoch', type=int, default=100,
help='epoch number')
parser.add_argument('--batch_size', type=int, default=5,
help='batch size')
parser.add_argument('--load_path', type=str, default=None,
help='path for model loading, "#LATEST#" for the latest checkpoint')
parser.add_argument('--save_path', type=str,
default='./saved_model/graphnn-model', help='path for model saving')
parser.add_argument('--log_path', type=str, default=None,
help='path for training log')
if __name__ == '__main__':
args = parser.parse_args()
args.dtype = tf.float32
print("=================================")
print(args)
print("=================================")
os.environ["CUDA_VISIBLE_DEVICES"]=args.device
Dtype = args.dtype
NODE_FEATURE_DIM = args.fea_dim
EMBED_DIM = args.embed_dim
EMBED_DEPTH = args.embed_depth
OUTPUT_DIM = args.output_dim
ITERATION_LEVEL = args.iter_level
LEARNING_RATE = args.lr
MAX_EPOCH = args.epoch
BATCH_SIZE = args.batch_size
LOAD_PATH = args.load_path
SAVE_PATH = args.save_path
LOG_PATH = args.log_path
SHOW_FREQ = 1
TEST_FREQ = 1
SAVE_FREQ = 5
DATA_FILE_NAME = './data/acfgSSL_{}/'.format(NODE_FEATURE_DIM)
SOFTWARE=('openssl-1.0.1f-', 'openssl-1.0.1u-')
OPTIMIZATION=('-O0', '-O1','-O2','-O3')
COMPILER=('armeb-linux', 'i586-linux', 'mips-linux')
VERSION=('v54',)
FUNC_NAME_DICT = {}
# Process the input graphs
F_NAME = get_f_name(DATA_FILE_NAME, SOFTWARE, COMPILER,
OPTIMIZATION, VERSION)
FUNC_NAME_DICT = get_f_dict(F_NAME)
Gs, classes = read_graph(F_NAME, FUNC_NAME_DICT, NODE_FEATURE_DIM)
print "{} graphs, {} functions".format(len(Gs), len(classes))
if os.path.isfile('data/class_perm.npy'):
perm = np.load('data/class_perm.npy')
else:
perm = np.random.permutation(len(classes))
np.save('data/class_perm.npy', perm)
if len(perm) < len(classes):
perm = np.random.permutation(len(classes))
np.save('data/class_perm.npy', perm)
Gs_train, classes_train, Gs_dev, classes_dev, Gs_test, classes_test =\
partition_data(Gs,classes,[0.8,0.1,0.1],perm)
print "Train: {} graphs, {} functions".format(
len(Gs_train), len(classes_train))
print "Dev: {} graphs, {} functions".format(
len(Gs_dev), len(classes_dev))
print "Test: {} graphs, {} functions".format(
len(Gs_test), len(classes_test))
# Fix the pairs for validation
if os.path.isfile('data/valid.json'):
with open('data/valid.json') as inf:
valid_ids = json.load(inf)
valid_epoch = generate_epoch_pair(
Gs_dev, classes_dev, BATCH_SIZE, load_id=valid_ids)
else:
valid_epoch, valid_ids = generate_epoch_pair(
Gs_dev, classes_dev, BATCH_SIZE, output_id=True)
with open('data/valid.json', 'w') as outf:
json.dump(valid_ids, outf)
# Model
gnn = graphnn(
N_x = NODE_FEATURE_DIM,
Dtype = Dtype,
N_embed = EMBED_DIM,
depth_embed = EMBED_DEPTH,
N_o = OUTPUT_DIM,
ITER_LEVEL = ITERATION_LEVEL,
lr = LEARNING_RATE
)
gnn.init(LOAD_PATH, LOG_PATH)
# Train
auc, fpr, tpr, thres = get_auc_epoch(gnn, Gs_train, classes_train,
BATCH_SIZE, load_data=valid_epoch)
gnn.say("Initial training auc = {0} @ {1}".format(auc, datetime.now()))
auc0, fpr, tpr, thres = get_auc_epoch(gnn, Gs_dev, classes_dev,
BATCH_SIZE, load_data=valid_epoch)
gnn.say("Initial validation auc = {0} @ {1}".format(auc0, datetime.now()))
best_auc = 0
for i in range(1, MAX_EPOCH+1):
l = train_epoch(gnn, Gs_train, classes_train, BATCH_SIZE)
gnn.say("EPOCH {3}/{0}, loss = {1} @ {2}".format(
MAX_EPOCH, l, datetime.now(), i))
if (i % TEST_FREQ == 0):
auc, fpr, tpr, thres = get_auc_epoch(gnn, Gs_train, classes_train,
BATCH_SIZE, load_data=valid_epoch)
gnn.say("Testing model: training auc = {0} @ {1}".format(
auc, datetime.now()))
auc, fpr, tpr, thres = get_auc_epoch(gnn, Gs_dev, classes_dev,
BATCH_SIZE, load_data=valid_epoch)
gnn.say("Testing model: validation auc = {0} @ {1}".format(
auc, datetime.now()))
if auc > best_auc:
path = gnn.save(SAVE_PATH+'_best')
best_auc = auc
gnn.say("Model saved in {}".format(path))
if (i % SAVE_FREQ == 0):
path = gnn.save(SAVE_PATH, i)
gnn.say("Model saved in {}".format(path))