-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
297 lines (255 loc) · 11.5 KB
/
classifier.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
from typing import Any
from sklearn.preprocessing import LabelEncoder
from sklearn.pipeline import Pipeline
import streamlit as st
import torch
import pickle
from sentence_transformers import SentenceTransformer
import logging
import warnings
from time import time
import onnx
import onnxruntime as ort
from normalizer import norm
import psutil
from common.download_pipeline import load_vectorizer
import numpy as np
import gc
warnings.filterwarnings("ignore")
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s %(message)s', level=logging.ERROR)
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
NUM_CPU = psutil.cpu_count(logical=False)
torch.set_num_threads(NUM_CPU)
torch.set_num_interop_threads(NUM_CPU * 4)
log.info(torch.__config__.parallel_info())
class BaseClassifier:
def __init__(self):
begin = time()
self.log = logging.getLogger(self.__class__.__name__)
self.log.setLevel(logging.INFO)
self.log.info(f'Initializing {self.__class__.__name__} >>>')
self.embedder = self._load_embedder()
self.label_encoder = self._load_label_encoder()
self.model = self._load_model()
self.log.info(f'Initialized {self.__class__.__name__} accomplished in {time() - begin} seconds')
def _load_embedder(self):
raise NotImplementedError("Implement in child class")
def _load_model(self):
raise NotImplementedError("Implement in child class")
def _load_label_encoder(self):
raise NotImplementedError("Implement in child class")
# def __del__(self):
# self.log.info(f'Deleting {self.__class__.__name__}')
# del self.embedder, self.model, self.label_encoder
# gc.collect()
class TorchClassifier(BaseClassifier):
def __init__(self):
super(TorchClassifier, self).__init__()
@st.cache(allow_output_mutation=True)
def _load_model(self) -> torch.nn.modules.container.Sequential:
"""
Load model with cache to prevent reloading model after predicting
:return: torch model
"""
self.log.info("Torch Initializing Neural Net>>>")
return torch.load('resource/classifier.pt', map_location=torch.device('cpu'))
@st.cache(allow_output_mutation=True)
def _load_embedder(self) -> SentenceTransformer:
"""
Load embedder with cache to prevent reloading embedder after predicting
:return: cached vietnamese-sbert SentenceTransformer
"""
self.log.info("Initializing embedder>>>")
return SentenceTransformer('keepitreal/vietnamese-sbert', device='cpu')
@st.cache(allow_output_mutation=True)
def _load_label_encoder(self) -> LabelEncoder:
"""
Load label encoder with cache to prevent reloading label encoder after predicting
:return: LabelEncoder that have been encoded with class label
"""
return pickle.load(open('resource/label_encoder.pkl', 'rb'))
def test_prediction(self):
"""
Test prediction
:return: Prediction result
"""
batch = ['áo choàng đông', ' iphone 13 promax']
self.log.info(f"Predicting batch sample : {batch}")
return self.predict(batch)
def predict(self, batch: list, batch_inference: bool = False) -> tuple[list[Any], float] | tuple[
dict[Any, Any], float]: # noqa
"""
Predict batch of sentences
:param batch_inference: check if predict on a file or not
:param batch: list of text that need to be predicted
:param batch_inference: check if predict on a file or not
:return: a dictionary of text and its predicted label
"""
assert type(batch) == list, "Batch must be a list"
begin_0 = time()
if not batch_inference:
batch_embedded = [norm(i) for i in batch]
else:
batch_embedded = batch
batch_embedded = self.embedder.encode(batch_embedded, convert_to_tensor=True, batch_size=min(len(batch), 2048))
self.log.info("Torch Embedding batch accomplished in {} seconds".format(time() - begin_0))
begin_1 = time()
self.model.eval()
with torch.no_grad():
out_data = self.model(batch_embedded)
ps = torch.exp(out_data)
pred = ps.max(1).indices.cpu().numpy()
res = [self.label_encoder.inverse_transform([i])[0] for i in pred]
self.log.info("Torch Predicting batch accomplished in {} seconds".format(time() - begin_1))
if not batch_inference:
self.log.info(res)
total_pred_time = time() - begin_0
if batch_inference:
return res, total_pred_time
return dict(zip(batch, res)), total_pred_time
class ONNXClassifier(BaseClassifier):
def __init__(self):
self.path = 'resource/classifier.onnx'
super().__init__()
onnx.checker.check_model(onnx.load(self.path))
self.model_label_name = self.model.get_outputs()[0].name
self.model_input_name = self.model.get_inputs()[0].name
@st.cache(allow_output_mutation=True)
def _load_model(self) -> ort.InferenceSession:
"""
Load ONNX session with cache to prevent reloading model after predicting
:return: Onnx session model
"""
self.log.info("Initializing ONNX session>>>")
so = ort.SessionOptions()
so.add_session_config_entry('session.load_model_format', 'ONNX')
so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
so.optimized_model_filepath = self.path
ort_sess = ort.InferenceSession(self.path, providers=['CPUExecutionProvider'], sess_options=so)
if ort_sess:
self.log.info(f"ONNX Neural Net Initialized in {ort_sess.get_profiling_start_time_ns()} ns")
else:
self.log.error("ONNX Neural Net Initialization Failed")
return ort_sess
@st.cache(allow_output_mutation=True)
def _load_embedder(self) -> SentenceTransformer:
"""
Load embedder with cache to prevent reloading embedder after predicting
:return: cached vietnamese-sbert SentenceTransformer
"""
self.log.info("Initializing embedder>>>")
return SentenceTransformer('keepitreal/vietnamese-sbert', device='cpu')
@st.cache(allow_output_mutation=True)
def _load_label_encoder(self) -> LabelEncoder:
"""
Load label encoder with cache to prevent reloading label encoder after predicting
:return: LabelEncoder that have been encoded with class label
"""
return pickle.load(open('resource/label_encoder.pkl', 'rb'))
def test_prediction(self) -> tuple[dict[str, str], float]:
"""
Test prediction
:return: Prediction result
"""
batch = ['áo choàng đông', ' iphone 13 promax']
self.log.info(f"Predicting batch sample : {batch}")
return self.predict(batch)
def predict(self, batch: list, batch_inference: bool = False) -> tuple[list[Any], float] | tuple[
dict[Any, Any], float]: # noqa
"""
Predict batch of sentences
:param batch: list of text that need to be predicted
:return: a dictionary of text and its predicted label
:param batch_inference: check if predict on a file or not
"""
assert type(batch) == list, "Batch must be a list"
begin_0 = time()
if not batch_inference:
batch_embedded = [norm(i) for i in batch]
else:
batch_embedded = batch
batch_embedded = self.embedder.encode(batch_embedded, batch_size=min(len(batch), 2048))
self.log.info("ONNX Embedding batch accomplished in {} seconds".format(time() - begin_0))
begin_1 = time()
pred = self.model.run([self.model_label_name], {'text_embedding': batch_embedded})
res = [self.label_encoder.inverse_transform([i])[0] for i in
[pred[0][index].argmax(0) for index, _ in enumerate(pred[0])]]
self.log.info("ONNX Model Predicting batch accomplished in {} seconds".format(time() - begin_1))
if not batch_inference:
self.log.info(res)
total_pred_time = time() - begin_0
if batch_inference:
return res, total_pred_time
return dict(zip(batch, res)), total_pred_time
class KerasClassifier(BaseClassifier):
def __init__(self):
self.path = 'resource/KerasClassifier.onnx'
super(KerasClassifier, self).__init__()
onnx.checker.check_model(onnx.load(self.path))
self.model_label_name = self.model.get_outputs()[0].name
self.model_input_name = self.model.get_inputs()[0].name
@st.cache(allow_output_mutation=True)
def _load_model(self) -> ort.InferenceSession:
"""
Load ONNX session with cache to prevent reloading model after predicting
:return: Onnx session model
"""
self.log.info("Initializing ONNX session>>>")
so = ort.SessionOptions()
so.add_session_config_entry('session.load_model_format', 'ONNX')
so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
so.optimized_model_filepath = self.path
ort_sess = ort.InferenceSession(self.path, providers=['CPUExecutionProvider'], sess_options=so)
if ort_sess:
self.log.info(f"ONNX Neural Net Initialized in {ort_sess.get_profiling_start_time_ns()} ns")
else:
self.log.error("ONNX Neural Net Initialization Failed")
return ort_sess
@st.cache(allow_output_mutation=True)
def _load_embedder(self) -> Pipeline:
"""
Load embedder with cache to prevent reloading embedder after predicting
:return: cached Sklearn Pipeline, see more in notebooks folder
"""
self.log.info("Initializing embedder>>>")
return load_vectorizer()
@st.cache(allow_output_mutation=True)
def _load_label_encoder(self) -> LabelEncoder:
"""
Load label encoder with cache to prevent reloading label encoder after predicting
:return: LabelEncoder that have been encoded with class label
"""
return pickle.load(open('resource/label_encoder.pkl', 'rb'))
def test_prediction(self) -> tuple[dict[str, str], float]:
"""
Test prediction
:return: Prediction result
"""
batch = ['áo choàng đông']
self.log.info(f"Predicting batch sample : {batch}")
return self.predict(batch)
def predict(self, sample: list[str]) -> tuple[list[Any], float] | tuple[dict[Any, Any], float]:
"""
Predict batch of sentences
:param sample: list of ONE text that need to be predicted.
:return: a dictionary of text and its predicted label, with prediction time.
"""
assert type(sample) == list, "Batch must be a list"
begin_0 = time()
transformed_sample = [norm(i) for i in sample]
transformed_sample = self.embedder.transform(transformed_sample)
self.log.info("Sklearn Pipeline Embedding accomplished in {} seconds".format(time() - begin_0))
begin_1 = time()
pred = self.model.run(None, {'input': transformed_sample})
pred = self.label_encoder.inverse_transform([np.argmax(pred[0], axis=1)])
self.log.info("Keras model predict accomplished in {} seconds".format(time() - begin_1))
total_pred_time = time() - begin_0
return dict(zip(sample, pred)), total_pred_time
if __name__ == '__main__':
# classifier = TorchClassifier()
# print(classifier.test_prediction())
classifier = KerasClassifier()
print(classifier.test_prediction())
# classifier = ONNXClassifier()
# print(classifier.test_prediction())