-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.py
428 lines (340 loc) · 17 KB
/
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
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
import os
import json
from .exceptions import AzureException
from a2ml.api.utils.dataframe import DataFrame
from a2ml.api.utils import fsclient
from a2ml.api.utils.decorators import error_handler, authenticated
from a2ml.api.model_review.model_helper import ModelHelper
from a2ml.api.model_review.model_review import ModelReview
from .credentials import Credentials
class AzureModel(object):
def __init__(self, ctx):
super(AzureModel, self).__init__()
self.ctx = ctx
self.credentials = Credentials(self.ctx).load()
@error_handler
@authenticated
def deploy(self, model_id, locally, review):
if locally:
is_loaded, model_path = self.verify_local_model(model_id)
if is_loaded:
self.ctx.log('Model already deployed to %s' % model_path)
return {'model_id': model_id}
from azureml.train.automl.run import AutoMLRun
ws, experiment = self._get_experiment()
model_run = AutoMLRun(experiment = experiment, run_id = model_id)
result = self._deploy_locally(model_id, model_run, ws, experiment) if locally else \
self._deploy_remotly(model_id, model_run, ws, experiment)
model_features, target_categories = self._get_remote_model_features(model_run)
feature_importance = self._get_feature_importance(model_run)
options = {
'uid': model_id,
'targetFeature': self.ctx.config.get('target'),
'support_review_model': review,
'provider': self.ctx.config.name,
'scoreNames': [self.ctx.config.get('experiment/metric')],
'scoring': self.ctx.config.get('experiment/metric'),
"score_name": self.ctx.config.get('experiment/metric'),
"originalFeatureColumns": model_features
}
options.update(self._get_a2ml_info())
fsclient.write_json_file(os.path.join(self.ctx.config.get_model_path(model_id), "options.json"),
options)
fsclient.write_json_file(os.path.join(self.ctx.config.get_model_path(model_id), "target_categoricals.json"),
{self.ctx.config.get('target'): {"categories": target_categories}})
metric_path = ModelHelper.get_metric_path( options, model_id)
fsclient.write_json_file(os.path.join(metric_path, "metric_names_feature_importance.json"),
{'feature_importance_data': {
'features': list(feature_importance.keys()),
'scores': list(feature_importance.values())
}})
return result
def _get_a2ml_info(self):
return {'augerInfo':{
'projectPath': self.ctx.config.get_path(),
'experiment_id': self.ctx.config.get('experiment/name', None),
'experiment_session_id':self.ctx.config.get('experiment/run_id', None),
}};
def _deploy_remotly(self, model_id, model_run, ws, experiment):
from azureml.core.model import Model
from azureml.core.model import InferenceConfig
from azureml.core.webservice import Webservice
from azureml.core.webservice import AciWebservice
from azureml.exceptions import WebserviceException
from azureml.train.automl.run import AutoMLRun
# ws, experiment = self._get_experiment()
iteration, run_id = self._get_iteration(model_id)
experiment_run = AutoMLRun(experiment = experiment, run_id = run_id)
model_name = model_run.properties['model_name']
self.ctx.log('Registering model: %s' % model_id)
description = '%s-%s' % (model_name, iteration)
model = experiment_run.register_model(
model_name = model_name, iteration=iteration,
description = description, tags = None)
script_file_name = '.azureml/score_script.py'
model_run.download_file(
'outputs/scoring_file_v_1_0_0.py', script_file_name)
self._edit_score_script(script_file_name)
# Deploying ACI Service
aci_service_name = self._aci_service_name(model_name)
self.ctx.log('Deploying AciWebservice %s ...' % aci_service_name)
inference_config = InferenceConfig(
environment = model_run.get_environment(),
entry_script = script_file_name)
aciconfig = AciWebservice.deploy_configuration(
cpu_cores = 1,
memory_gb = 2,
tags = {'type': "inference-%s" % aci_service_name},
description = "inference-%s" % aci_service_name)
# Remove any existing service under the same name.
try:
Webservice(ws, aci_service_name).delete()
self.ctx.log('Remove any existing service under the same name...')
except WebserviceException:
pass
aci_service = Model.deploy(
ws, aci_service_name, [model], inference_config, aciconfig)
aci_service.wait_for_deployment(True)
self.ctx.log('%s state %s' % (aci_service_name, str(aci_service.state)))
return {'model_id': model_id, 'aci_service_name': aci_service_name}
def _edit_score_script(self, script_file_name):
text = fsclient.read_text_file(script_file_name)
text = text.replace("@input_schema('data', PandasParameterType(input_sample))",
"""
def convert_simple_numpy_type(obj):
if isinstance(obj, (np.int_, np.intc, np.intp, np.int8,
np.int16, np.int32, np.int64, np.uint8,
np.uint16, np.uint32, np.uint64)):
return int(obj)
elif isinstance(obj, (np.float_, np.float16, np.float32,
np.float64)):
return float(obj)
return None
class NumpyJSONEncoder(json.JSONEncoder):
def default(self, obj):
res = convert_simple_numpy_type(obj)
if res is not None:
return res
return json.JSONEncoder.default(self, obj)
def json_dumps_np(data, allow_nan=True):
return json.dumps(data, cls=NumpyJSONEncoder, allow_nan=allow_nan)
@input_schema('data', PandasParameterType(input_sample))
def get_df(data):
return data
"""
)
text = text.replace("result = model.predict(data)",
"""
df = get_df(data['data'])
proba_classes = []
if data['method'] == "predict_proba":
result = model.predict_proba(df)
proba_classes = list(model.classes_)
else:
result = model.predict(df)
"""
)
text = text.replace("return json.dumps({\"result\": result.tolist()})",
"return json_dumps_np({\"result\": result.tolist(), \"proba_classes\": proba_classes})")
fsclient.write_text_file(script_file_name, text)
@error_handler
@authenticated
def predict(self, filename, model_id, threshold=None, locally=False, data=None, columns=None,
predicted_at=None, output=None, json_result=False, count_in_result=False, prediction_id=None
):
ds = DataFrame.create_dataframe(filename, data, columns)
model_path = self.ctx.config.get_model_path(model_id)
options = fsclient.read_json_file(os.path.join(model_path, "options.json"))
results, results_proba, proba_classes, target_categories = \
self._predict_locally(ds.df, model_id, threshold) if locally else self._predict_remotely(ds.df, model_id, threshold)
if target_categories and len(target_categories) == 2:
for idx, item in enumerate(target_categories):
if item == "False":
target_categories[idx] = False
if item == "True":
target_categories[idx] = True
ModelHelper.process_prediction(ds,
results, results_proba, proba_classes,
threshold,
options.get('minority_target_class', self.ctx.config.get('minority_target_class')),
options.get('targetFeature', self.ctx.config.get('target', None)),
target_categories)
predicted = ModelHelper.save_prediction(ds, prediction_id,
options.get('support_review_model', True), json_result, count_in_result, predicted_at,
model_path, model_id, output)
if filename:
self.ctx.log('Predictions stored in %s' % predicted)
return {'predicted': predicted}
@error_handler
@authenticated
def actuals(self, model_id, filename=None, actual_records=None, actuals_at=None, locally=False):
if locally:
model_path = self.ctx.config.get_model_path(model_id)
if not fsclient.is_folder_exists(model_path):
raise Exception('Model should be deployed first.')
return ModelReview({'model_path': model_path}).add_actuals(
actuals_path=filename, actual_records=actual_records, actual_date=actuals_at)
else:
raise Exception("Not Implemented")
@error_handler
@authenticated
def build_review_data(self, model_id, locally, output):
if locally:
model_path = self.ctx.config.get_model_path(model_id)
if not fsclient.is_folder_exists(model_path):
raise Exception('Model should be deployed first.')
return ModelReview({'model_path': os.path.join(model_path, "model")}).build_review_data(
data_path=self.ctx.config.get("source"), output=output)
else:
raise Exception("Not Implemented.")
@error_handler
@authenticated
def review(self, model_id):
pass
def _get_iteration(self, model_id):
iteration = None
run_id = model_id
parts = model_id.split('_')
if len(parts) > 2:
run_id = parts[0]+"_"+parts[1]
iteration = parts[2]
return iteration, run_id
def _aci_service_name(self, model_name):
# It must only consist of lowercase letters, numbers, or dashes, start
# with a letter, end with a letter or number, and be between 3 and 32
# characters long.
#TODO - service_name + suffix must satisfy requiremets
if model_name.endswith('-service'):
return model_name
return (model_name+'-service').lower()
def _get_experiment(self):
from azureml.core import Experiment
from .project import AzureProject
ws = AzureProject(self.ctx)._get_ws()
experiment_name = self.ctx.config.get('experiment/name', None)
if experiment_name is None:
raise AzureException('Please specify Experiment name...')
experiment = Experiment(ws, experiment_name)
return ws, experiment
def _get_remote_model_features(self, remote_run):
from a2ml.api.utils import fsclient, local_fsclient
import pandas as pd
model_features = None
target_categories = None
temp_dir = local_fsclient.LocalFSClient().get_temp_folder()
try:
file_name = 'scoring_file_v_1_0_0.py'
remote_run.download_file('outputs/%s'%file_name, os.path.join(temp_dir, file_name))
text = fsclient.read_text_file(os.path.join(temp_dir, file_name))
to_find = "input_sample ="
start = text.find(to_find)
if start > 0:
end = text.find("\n", start)
if end > start:
code_to_run = text[start+len(to_find):end]
input_sample = eval(code_to_run)
model_features = input_sample.columns.tolist()
except Exception as e:
self.ctx.log('Cannot get columns from remote model.Use original columns from predicted data: %s'%e)
if self.ctx.config.get("model_type") == "classification":
try:
file_name = 'confusion_matrix'
remote_run.download_file('%s'%file_name, os.path.join(temp_dir, file_name))
cm_data = fsclient.read_json_file(os.path.join(temp_dir, file_name))
target_categories = cm_data.get('data', {}).get('class_labels')
except Exception as e:
self.ctx.log('Cannot get categorical target class labels from remote model.Use class codes: %s'%e)
fsclient.remove_folder(temp_dir)
return model_features, target_categories
def _get_feature_importance(self, model_run):
from azureml.explain.model._internal.explanation_client import ExplanationClient
try:
client = ExplanationClient.from_run(model_run)
engineered_explanations = client.download_model_explanation(raw=True)
return engineered_explanations.get_feature_importance_dict()
except Exception as e:
self.ctx.log('Cannot get feature_importance from remote model: %s'%e)
return {}
def _predict_remotely(self, predict_data, model_id, predict_proba):
from azureml.core.webservice import AciWebservice
from azureml.train.automl.run import AutoMLRun
from azureml.core.run import Run
import numpy as np
ws, experiment = self._get_experiment()
model_features = None
target_categories = None
remote_run = AutoMLRun(experiment = experiment, run_id = model_id)
model_features, target_categories = self._get_remote_model_features(remote_run)
if model_id.startswith("AutoML_"):
model_name = remote_run.properties['model_name']
else:
model_name = model_id
if model_features:
predict_data = predict_data[model_features]
input_payload = predict_data.to_json(orient='split', index = False)
aci_service_name = self._aci_service_name(model_name)
aci_service = AciWebservice(ws, aci_service_name)
input_payload = json.loads(input_payload)
# If you have a classification model, you can get probabilities by changing this to 'predict_proba'.
method = 'predict'
if predict_proba:
method = 'predict_proba'
input_payload = {
'data': {'data': input_payload['data'], 'method': method}
}
input_payload = json.dumps(input_payload)
try:
response = aci_service.run(input_data = input_payload)
except Exception as e:
log_file = 'automl_errors.log'
fsclient.write_text_file(log_file, aci_service.get_logs(), mode="a")
raise AzureException("Prediction service error. Please redeploy the model. Log saved to file '%s'. Details: %s"%(log_file, str(e)))
response = json.loads(response)
if "error" in response or not 'result' in response:
raise AzureException('Prediction service return error: %s'%response.get('error'))
results_proba = None
proba_classes = None
results = response['result']
if predict_proba:
results_proba = results
proba_classes = response['proba_classes']
results_proba = np.array(results_proba)
return results, results_proba, proba_classes, target_categories
def verify_local_model(self, model_id):
model_path = os.path.join(self.ctx.config.get_model_path(model_id),
'model.pkl.gz')
return fsclient.is_file_exists(model_path), model_path
def _deploy_locally(self, model_id, model_run, ws, experiment):
from azureml.train.automl.run import AutoMLRun
self.ctx.log('Downloading model %s' % model_id)
iteration, run_id = self._get_iteration(model_id)
remote_run = AutoMLRun(experiment = experiment, run_id = run_id)
best_run, fitted_model = remote_run.get_output(iteration=iteration)
is_loaded, model_path = self.verify_local_model(model_id)
fsclient.save_object_to_file(fitted_model, model_path)
self.ctx.log('Downloaded model to %s' % model_path)
return {'model_id': model_id}
def _predict_locally(self, predict_data, model_id, threshold):
is_loaded, model_path = self.verify_local_model(model_id)
if not is_loaded:
raise Exception("Model should be deployed before predict.")
fitted_model = fsclient.load_object_from_file(model_path)
try:
options = fsclient.read_json_file(os.path.join(self.ctx.config.get_model_path(model_id), "options.json"))
model_features = options.get("originalFeatureColumns")
predict_data = predict_data[model_features]
predict_data.to_csv("test_options.csv", index=False, compression=None, encoding='utf-8')
except Exception as e:
self.ctx.log('Cannot get columns from model.Use original columns from predicted data: %s'%e)
results_proba = None
proba_classes = None
results = None
if threshold is not None:
results_proba = fitted_model.predict_proba(predict_data)
proba_classes = list(fitted_model.classes_)
else:
results = fitted_model.predict(predict_data)
target_categoricals = fsclient.read_json_file(os.path.join(
self.ctx.config.get_model_path(model_id), "target_categoricals.json"))
target_categories = target_categoricals.get(self.ctx.config.get('target'), {}).get("categories")
return results, results_proba, proba_classes, target_categories