-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanticheat.py
154 lines (120 loc) · 4.2 KB
/
anticheat.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
# Sloppy Keras code to train a very basic MLP
# Please have mercy on me.
from __future__ import print_function
import numpy as np
np.set_printoptions(suppress=True)
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.utils import np_utils
from keras.optimizers import SGD
import os, math, time
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name:
print('[%s]' % self.name)
print('Elapsed: %s' % (time.time() - self.tstart))
DATA = "D:\\Downloads\\HALData-master"
def categorical_probas_to_classes(p):
return np.argmax(p, axis=1)
def probas_to_classes(y_pred):
if len(y_pred.shape) > 1 and y_pred.shape[1] > 1:
return categorical_probas_to_classes(y_pred)
return np.array([1 if p > 0.5 else 0 for p in y_pred])
def shuffle_in_unison_inplace(a, b):
assert len(a) == len(b)
p = np.random.permutation(len(a))
return a[p], b[p]
## This returns two vectors:
## examples, which looks like [[feature, feature, feature, feature],
# [features, feature, feature, feature]]
# labels, which looks like [[0, 1], [1, 0]] ...
def parse(d, keyword, op, Y_fn=False):
examples = []
labels = []
for f in os.listdir(d):
if keyword in f:
lines = [line.rstrip('\n') for line in open(os.path.join(d, f))]
ts = 0
init_ts = -1
for l in lines:
if not l.startswith("#"): #and l.startswith("attackInfo"):
s = l.split(',')
if len(s) < 12:
continue
ts = long(s[11])
if init_ts < 0:
init_ts = ts
num_attacks = 0
max_dist = -1.0
max_angle = -1.0
vel_total = []
vel_count = 0
if l.startswith("attackInfo"):
num_attacks += 1
max_dist = max(float(s[8]), max_dist)
max_angle = max(abs(float(s[7])), max_angle)
if l.startswith("move"):
if s[12].isdigit():
if int(s[12]) != 0:
vel_count += 1
vel_total.append(abs(float(s[1]) / (float(s[12])/1000)))
#print(abs(float(s[1]) / (float(s[12])/1000)), f, ts)
if ts - init_ts > 5*1000 - 1:
init_ts = -1
if num_attacks != 0:
examples.append([num_attacks / 10.0, max_angle, max_dist, np.median(vel_total)])
if Y_fn:
labels.append(f)
else:
labels.append(op[:])
return np.asarray(examples), np.asarray(labels)
X_van, Y_van = parse(DATA + "\\Vanilla", ".csv", [1, 0])
X_hac, Y_hac = parse(DATA + "\\Hacks", ".csv", [0, 1])
print("Shuffling")
shuffle_in_unison_inplace(X_van, Y_van)
shuffle_in_unison_inplace(X_hac, Y_hac)
# Each class must have equal support
X_hac = X_hac[:len(X_van)]
Y_hac = Y_hac[:len(X_van)]
# print(X_van)
# print("Hack:")
# print(X_hac)
# exit()
print("Support for each class: ", len(X_van))
van_l = math.floor(len(X_van) * 1)
hac_l = math.floor(len(X_hac) * 1)
X_train = np.concatenate((X_van[:van_l], X_hac[:hac_l]), axis=0)
Y_train = np.concatenate((Y_van[:van_l], Y_hac[:hac_l]), axis=0)
X_test = np.concatenate((X_van[van_l:], X_hac[hac_l:]), axis=0)
Y_test = np.concatenate((Y_van[van_l:], Y_hac[hac_l:]), axis=0)
model = Sequential()
model.add(Dense(20, input_dim=4, init='uniform'))
model.add(Activation('tanh'))
model.add(Dense(2, init='uniform'))
model.add(Activation('softmax'))
sgd = SGD(lr=0.005, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='binary_crossentropy',
optimizer=sgd,
metrics=['accuracy'])
model.load_weights('prelim_4feats_as.2.h5')
# model.fit(X_train, Y_train,
# nb_epoch=500, batch_size=16)
# score = model.evaluate(X_test, Y_test, batch_size=16)
# for i in range(len(model.metrics_names)):
# print(model.metrics_names[i], score[i])
#model.save_weights('prelim_4feats_as.2.h5')
X_test, Y_test = parse("C:\\Users\\shrey\\Downloads\\Telegram Desktop", ".csv", [0, 1], Y_fn=True)
with Timer("perdiction"):
pred_probas = model.predict_proba(X_test, batch_size=16)
pred = probas_to_classes(pred_probas)
actual = probas_to_classes(Y_test)
for p, a, c in zip(pred, Y_test, pred_probas):
print(p, a, c)
# print('Test score:', score[0])
# print('Test accuracy:', score[1])
print(X_test)
print(Y_test)