-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_model.py
218 lines (186 loc) · 8.92 KB
/
run_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
from collections import defaultdict
from itertools import product
from datetime import datetime as dt
import pandas as pd
from pathlib import Path
from sklearn.metrics import mean_absolute_error, accuracy_score
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from config import project_config
import pickle
import os
import time
import sys
from os.path import dirname, abspath
d = dirname(dirname(abspath(__file__)))
sys.path.append(d)
from util.file_processor import FileProcessor
from util.file_processor import FileValidator
from util.data_splitter import KfoldCVOverFiles
from models.rtp_ml import RTP_ML
from models.rtp_heuristic import RTP_Heuristic
from models.ip_udp_ml import IP_UDP_ML
from models.ip_udp_heuristic import IP_UDP_Heuristic
class ModelRunner:
def __init__(self, metric, estimation_method, feature_subset, data_dir, cv_index):
self.metric = metric
self.estimation_method = estimation_method
self.feature_subset = 'none' if feature_subset is None else feature_subset
self.data_dir = data_dir
if feature_subset:
feature_subset_tag = '-'.join(feature_subset)
else:
feature_subset_tag = 'none'
data_bname = os.path.basename(data_dir)
self.trial_id = '_'.join(
[metric, estimation_method, feature_subset_tag, data_bname, f'cv_{cv_index}'])
self.intermediates_dir = f'{self.data_dir}_intermediates/{self.trial_id}'
self.cv_index = cv_index
self.model = None
def save_intermediate(self, data_object, pickle_filename):
pickle_filename = f'{self.trial_id}_{pickle_filename}'
with open(f'{self.intermediates_dir}/{pickle_filename}.pkl', 'wb') as fd:
pickle.dump(data_object, fd)
def load_intermediate(self, pickle_filename):
with open(f'{self.intermediates_dir}/{pickle_filename}.pkl', 'rb') as fd:
data_object = pickle.load(fd)
return data_object
def fps_prediction_accuracy(self, pred, truth):
n = len(pred)
df = pd.DataFrame({'pred': pred.to_numpy(), 'truth': truth.to_numpy()})
df['deviation'] = df['pred']-df['truth']
df['deviation'] = df['deviation'].abs()
return len(df[df['deviation'] <= 2])/n
def train_model(self, split_files):
bname = os.path.basename(self.data_dir)
vca_model = {}
importances = {}
for vca in split_files:
print(f'\nVCA = {vca}')
if self.estimation_method == 'ip-udp-ml':
estimator = RandomForestClassifier(
) if self.metric == 'frameHeight' else RandomForestRegressor()
model = IP_UDP_ML(
vca=vca,
feature_subset=self.feature_subset,
estimator=estimator,
config=project_config,
metric=self.metric,
dataset=bname
)
model.train(split_files[vca]['train'])
elif self.estimation_method == 'rtp-ml':
estimator = RandomForestClassifier(
) if self.metric == 'frameHeight' else RandomForestRegressor()
model = RTP_ML(
vca=vca,
feature_subset=self.feature_subset,
estimator=estimator,
config=project_config,
metric=self.metric,
dataset=bname
)
model.train(split_files[vca]['train'])
elif self.estimation_method == 'ip-udp-heuristic':
model = IP_UDP_Heuristic(
vca=vca, metric=self.metric, config=project_config, dataset=bname)
elif self.estimation_method == 'rtp-heuristic':
model = RTP_Heuristic(
vca=vca, metric=self.metric, config=project_config, dataset=bname)
vca_model[vca] = model
# self.save_intermediate(vca_model, 'vca_model')
return vca_model
def get_test_set_predictions(self, split_files, vca_model):
predictions = {}
maes = {}
accs = {}
for vca in split_files:
predictions[vca] = []
maes[vca] = []
accs[vca] = []
idx = 1
total = len(split_files[vca]['test'])
for file_tuple in split_files[vca]['test']:
print(file_tuple[0])
model = vca_model[vca]
print(
f'Testing {self.estimation_method} on file {idx} out of {total}...')
output = model.estimate(file_tuple)
if output is None:
idx += 1
predictions[vca].append(output)
continue
if self.metric != 'frameHeight':
mae = mean_absolute_error(
output[f'{self.metric}_gt'], output[f'{self.metric}_{self.estimation_method}'])
if self.metric == 'framesPerSecond' or self.metric == 'framesReceived' or self.metric == 'framesReceivedPerSecond' or self.metric == 'framesDecodedPerSecond' or self.metric == 'framesRendered':
acc = self.fps_prediction_accuracy(
output[f'{self.metric}_gt'], output[f'{self.metric}_{self.estimation_method}'])
accs[vca].append(acc)
print(f'Accuracy = {round(acc, 2)}')
if self.metric != 'frameHeight':
print(f'MAE = {round(mae, 2)}')
maes[vca].append(mae)
else:
a = accuracy_score(
output[f'{self.metric}_gt'], output[f'{self.metric}_{self.estimation_method}'])
print(f'Accuracy = {round(a, 2)}')
accs[vca].append(a)
idx += 1
predictions[vca].append(output)
for vca in split_files:
if self.metric == 'frameHeight':
mae_avg = "None"
else:
mae_avg = round(sum(maes[vca])/len(maes[vca]), 2)
accuracy_str = ''
if self.metric == 'framesPerSecond' or self.metric == 'framesReceivedPerSecond' or self.metric == 'framesDecodedPerSecond' or self.metric == 'framesRendered':
acc_avg = round(100*sum(accs[vca])/len(accs[vca]), 2)
accuracy_str = f'|| Accuracy_avg = {acc_avg}'
line = f'{dt.now()}\tVCA: {vca} || Experiment : {self.trial_id} || MAE_avg = {mae_avg} {accuracy_str}\n'
with open('log.txt', 'a') as fd:
fd.write(line)
# self.save_intermediate(predictions, 'predictions')
return predictions
if __name__ == '__main__':
# Example usage
metrics = ['framesReceivedPerSecond', 'bitrate',
'frame_jitter', 'frameHeight'] # what to predict
estimation_methods = ['ip-udp-heuristic', 'rtp-heuristic', 'ip-udp-ml', 'rtp-ml'] # how to predict
# groups of features as per `features.feature_extraction.py`
feature_subsets = [['LSTATS', 'TSTATS']]
data_dir = ['/home/taveesh/Documents/vcaml/data/in_lab_data']
bname = os.path.basename(data_dir[0])
# Create a directory for saving model intermediates
intermediates_dir = f'{data_dir[0]}_intermediates'
Path(intermediates_dir).mkdir(exist_ok=True, parents=True)
# Get a list of pairs (trace_csv_file, ground_truth)
fp = FileProcessor(data_directory=data_dir[0])
file_dict = fp.get_linked_files()
# Create 5-fold cross validation splits and validate files. Refer `util/validator.py` for more details
kcv = KfoldCVOverFiles(5, file_dict, project_config, bname)
file_splits = kcv.split()
with open(f'{intermediates_dir}/cv_splits.pkl', 'wb') as fd:
pickle.dump(file_splits, fd)
vca_preds = defaultdict(list)
param_list = [metrics, estimation_methods, feature_subsets, data_dir]
# Run models over 5 cross validations
for metric, estimation_method, feature_subset, data_dir in product(*param_list):
if metric == 'frameHeight' and 'heuristic' in estimation_method:
continue
models = []
cv_idx = 1
for fsp in file_splits:
model_runner = ModelRunner(
metric, estimation_method, feature_subset, data_dir, cv_idx)
vca_model = model_runner.train_model(fsp)
Path(f'{intermediates_dir}/{model_runner.trial_id}').mkdir(exist_ok=True, parents=True)
predictions = model_runner.get_test_set_predictions(fsp, vca_model)
models.append(vca_model)
with open(f'{intermediates_dir}/{model_runner.trial_id}/model.pkl', 'wb') as fd:
pickle.dump(vca_model, fd)
for vca in predictions:
preds = pd.concat(predictions[vca], axis=0)
vca_preds[vca].append(preds)
with open(f'{intermediates_dir}/{model_runner.trial_id}/predictions_{vca}.pkl', 'wb') as fd:
pickle.dump(preds, fd)
cv_idx += 1