-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_model.py
272 lines (176 loc) · 6.82 KB
/
test_model.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
import os
import random
import glob
import itertools
import sys
import time
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
from torch.utils.data.dataset import Dataset
import matplotlib.animation as animation
# in case it is called from a different location
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import data_loader
import models
from termcolor import colored
import commentjson
from pydoc import locate
import pickle
# is there a config in the current directory?
def get_config(config_path="config.json"):
with open('ModelData/' + config_path) as f:
config = commentjson.load(f)
return config
class ModelData:
def __init__(self, model, fullGameData,fullGameLabels ):
self.model = model
self.fullGameData = fullGameData
self.fullGameLabels = fullGameLabels
def load_pytorch_model(modelPath,config,data):
get_feature_indicies_fn = locate(config["feature_set"])
get_label_indicies_fn = locate(config["lable_set"])
hero_feature_indicies = get_feature_indicies_fn(data)
label_indicies = get_label_indicies_fn(data,config["label_set_arg"])
model_type = locate(config["model"])
inputFeatureSize = len(hero_feature_indicies[0])
outputFeatureSize = len(label_indicies)
model = model_type(inputFeatureSize,outputFeatureSize,**config["model_params"])
if config["optimizer"] == "Adam":
OptimizerType = torch.optim.Adam
elif config["optimizer"] == "SGD":
OptimizerType = torch.optim.SGD
optimizer = OptimizerType
model.load_state_dict(torch.load(modelPath))
print('loaded model')
fullGameData,fullGameLabels = data_loader.getSequencialNaive(data,hero_feature_indicies,label_indicies)
return ModelData(model,fullGameData,fullGameLabels)
heroStuff = []
heroStuffWindow = []
labelStuff = []
def modelPred(model,X):
predX = model(X)
predX = torch.sigmoid(predX)
predX = predX.cpu().detach().numpy()
return predX
def averagePred(models,X):
vals =[]
for m in models:
vals = modelPred(m,X)+ vals
return (vals / len(models))
#def dump_predictions(file,modelPath)
def make_predictions(file,modelPath):
trainingDataFiles = [file]#glob.glob("/scratch/staff/ak1774/shared_folder/data/train/*.h5")
data = data_loader.load_data_from_file(trainingDataFiles[0])
models = []
for i in range(1,2):
print(i)
models.append( load_pytorch_model('ModelData/' +str(i) +'/' +'model.model',
get_config('/' +str(i) +'/config.json'), data) )
#fullGameData,fullGameLabels = data_loader.getSequencialNaive(data,hero_feature_indicies,label_indicies)
xLims = data['time'].values
#¢health = data['player_4_m_iHealth'].values
#######################
# get original health
######################
norm_stats = None
with open("norm_stats.pickle", 'rb') as f:
norm_stats = pickle.load(f)
for label,min_value,max_value in normalization_stats:
if "_m_iHealth" in label:
health_min = min_value
health_max = max_value
if "m_iMaxHealth" in label:
maxhealth_min = min_value
maxhealth_max = max_value
healthes = []
max_healthes = []
relative_healthes = []
for i in range(0,10):
health_vals = data['player_' + str(i) + '_m_iHealth'].values
maxhealth_vals = data['player_' + str(i) + '_m_iMaxHealth'].values
health_vals = health_vals * (health_max - health_min) + health_min
maxhealth_vals = maxhealth_vals * (maxhealth_max - maxhealth_min) + maxhealth_min
relative_health_vals = health_vals / maxhealth_vals # hopefully maxhealth is never 0
healthes.append(health_vals)
max_healthes.append(maxhealth_vals)
relative_healthes.append(relative_health_vals)
#######################
# get death times
######################
labels = [(i,label) for i,label in enumerate(list(data))]
death_time_indicies = preprocess.labels_to_indicies(preprocess.select_features_by_name("time_until_next_death",labels))
death_times = data.values[:,death_time_indicies].astype(np.float32)
for m in models:
X = [torch.from_numpy(hero_X) for hero_X in m.fullGameData]
pred = model(X)
pred = torch.sigmoid(pred)
pred = pred.cpu().detach().numpy()
y = m.fullGameLabels
currentMeanTrueAccuracy = 0
currentMeanFalseAccuracy=0
numTruePos = 0
numFalsePos = 0
numTrueNeg = 0
numFalseNeg = 0
for i in range(0,data.shape[0]):
predX = 0
for m in models:
y = m.fullGameLabels[i]
y = np.array(y)
y = np.expand_dims(y,0)
X = [torch.from_numpy(hero_X[i:(i+1),:]) for hero_X in m.fullGameData]
print(i)
#predX = averagePred(models,X)
predX = modelPred(m.model,X) +predX
predX = predX/len(models)
'''
true_pos = ((predX > 0.5) == (y > 0.5)).reshape(-1).astype(np.float32)
true_neg = ((predX < 0.5) == (y <0.5)).reshape(-1).astype(np.float32)
false_pos = ((predX > 0.5) == (y < 0.5)).reshape(-1).astype(np.float32)
false_neg = ((predX < 0.5) == (y > 0.5)).reshape(-1).astype(np.float32)
for pos in true_neg:
if pos ==1:
numTrueNeg +=1
for pos in false_neg:
if pos ==1:
numFalseNeg +=1
for pos in true_pos:
if pos ==1:
numTruePos +=1
for pos in false_pos:
if pos ==1:
numFalsePos +=1
'''
prediction = predX
currentMeanTrueAccuracy += np.mean(true_pos)
currentMeanFalseAccuracy += np.mean(false_pos)
prediction = np.squeeze(prediction,0)
if i %3000 ==0:
print('Current true pos ' +str(currentMeanTrueAccuracy/(i+1)))
print('Current false pos ' +str(currentMeanFalseAccuracy/(i+1)))
heroStuff.append(prediction)
labelStuff.append(np.squeeze(y,0))
print()
print(numTruePos)
print(numTrueNeg)
print()
print(numFalsePos)
print(numFalseNeg)
print()
print('True Pos = ' + str(currentMeanTrueAccuracy/19326))
print('False pos = ' + str(currentMeanFalseAccuracy/19326))
heroStuff1 = np.swapaxes(heroStuff,0,1)
labelStuff1= np.swapaxes(labelStuff,0,1)
xLims = xLims - xLims[0] - 90
np.save('hero.npy', np.array(heroStuff1))
np.save('label.npy', np.array(labelStuff1))
np.save('xLims.npy', np.array(xLims))
np.save('health.npy',np.array(healthes))
if __name__ == "__main__":
make_predictions('3475430774_414202435_whole_match_normalized.h5','model.model')
#train_pytorch()