-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLUInference.py
More file actions
68 lines (59 loc) · 2.53 KB
/
LUInference.py
File metadata and controls
68 lines (59 loc) · 2.53 KB
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
#===- LUInference.py - ACPO Loop Unroll Inference ----------------------===//
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# Copyright (C) 2021-2023. Huawei Technologies Co., Ltd. All rights reserved.
#
#===----------------------------------------------------------------------===//
from MLInference import MLInference, ACPO_LOG
import numpy as np
import os
import pandas as pd
import pickle as pk
import tensorflow as tf
# Must be a full path
lu_path = os.path.join(os.path.dirname(__file__), './models/plu.pb')
frozen_model = lu_path
model_dir = lu_path
# Optional (not being used right now): inference from a csv containing features
test_features = lu_path + '/test_features.csv'
LU_Enum_Dic = {0: 'full', 1: 'partial', 2: 'runtime', 3: 'unmodified'}
class LUInference(MLInference):
def prepare_features(self):
"""
Process the features and convert them into an input to be used for inference.
"""
df = pd.DataFrame(self.features)
#print("Feature arrived at Python side:\n", features)
pkl_file = os.path.join(os.path.dirname(__file__), model_dir, "sc.pkl")
sc = pk.load(open(pkl_file, "rb"))
df = sc.transform(df.transpose())
input = np.array(df, dtype=np.float32)
input = input.reshape(1, len(self.features))
return input
def inference(self):
"""
Run an inference pass with an already loaded model and having features ready
"""
ACPO_LOG("ACPO Model successfully loaded.")
input = self.prepare_features()
output = self.infer(tf.constant(input))
output = output.get(self.output_key)
if (output is None):
return {}
output = output.numpy()
output_class_index = np.argmax(output)
# For now we assume that the order of outputs within the output class tuple is the same
# as the order specified by the OutputList field in model.acpo.
# A better way might be generating a dict of dicts or a list of dicts during load_model.
# Then use index to get the output dict directly. The dict will look like this:
# classes_dict = {0: {'LU-Count': 0, 'LU-Type': 3}, 1: {'LU-Count': 0, 'LU-Type': 2}, ...}
output_dict = {}
output_class = self.classes_dict.get(output_class_index)
for i in range(len(self.output_names)):
output_dict[self.output_names[i]] = output_class[i]
ACPO_LOG("Prediction is UP.Count=" + str(output_dict.get("LU-Count")) +
"\n")
return output_dict