Skip to content

Commit 9f00755

Browse files
authored
fix python deploy (#2148)
* fix python deploy * probs and logits * update * revert
1 parent d58a221 commit 9f00755

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

model_zoo/ernie-3.0/deploy/python/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,11 @@ python infer_cpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/
153153
```
154154
input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
155155
seq cls result:
156-
label: news_car confidence: 0.9929346442222595
156+
label: news_car confidence: 0.5543532371520996
157157
-----------------------------
158158
input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
159159
seq cls result:
160-
label: news_entertainment confidence: 0.998711347579956
160+
label: news_entertainment confidence: 0.9495906829833984
161161
-----------------------------
162162
```
163163
和命名实体识别模型推理类似,开启动态量化的命令如下:
@@ -168,11 +168,11 @@ python infer_cpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/
168168
```
169169
input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
170170
seq cls result:
171-
label: news_car confidence: 0.9910931587219238
171+
label: news_car confidence: 0.5778735876083374
172172
-----------------------------
173173
input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
174174
seq cls result:
175-
label: news_entertainment confidence: 0.9977021813392639
175+
label: news_entertainment confidence: 0.9206441044807434
176176
-----------------------------
177177
```
178178
### 3.3 GPU端推理样例
@@ -184,11 +184,11 @@ python infer_gpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/
184184
```
185185
input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
186186
seq cls result:
187-
label: news_car confidence: 0.9929346442222595
187+
label: news_car confidence: 0.5543532371520996
188188
-----------------------------
189189
input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
190190
seq cls result:
191-
label: news_entertainment confidence: 0.998711347579956
191+
label: news_entertainment confidence: 0.9495906829833984
192192
-----------------------------
193193
```
194194
如果需要FP16进行加速,可以开启use_fp16开关,具体命令为
@@ -202,11 +202,11 @@ python infer_gpu.py --task_name seq_cls --model_path ./tnews_pruned_infer_model/
202202
```
203203
input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
204204
seq cls result:
205-
label: news_car confidence: 0.9929342269897461
205+
label: news_car confidence: 0.5536671876907349
206206
-----------------------------
207207
input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
208208
seq cls result:
209-
label: news_entertainment confidence: 0.99870365858078
209+
label: news_entertainment confidence: 0.9494127035140991
210210
-----------------------------
211211
```
212212
如果需要进行INT8量化加速,还需要使用量化脚本对训练好的FP32模型进行量化,然后使用量化后的模型进行部署,模型的量化请参考:[模型量化脚本使用说明](./../../README.md#模型压缩),也可下载我们量化后的INT8模型进行部署,请执行如下命令获取模型:
@@ -226,10 +226,10 @@ python infer_gpu.py --task_name seq_cls --model_path ./tnews_quant_infer_model/i
226226
```
227227
input data: 未来自动驾驶真的会让酒驾和疲劳驾驶成历史吗?
228228
seq cls result:
229-
label: news_car confidence: 0.9922153353691101
229+
label: news_car confidence: 0.5510320067405701
230230
-----------------------------
231231
input data: 黄磊接受华少快问快答,不光智商逆天,情商也不逊黄渤
232232
seq cls result:
233-
label: news_entertainment confidence: 0.9986827373504639
233+
label: news_entertainment confidence: 0.9432708024978638
234234
-----------------------------
235235
```

model_zoo/ernie-3.0/deploy/python/ernie_predictor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,13 @@ def seq_cls_preprocess(self, input_data: list):
242242
}
243243

244244
def seq_cls_postprocess(self, infer_data, input_data):
245-
infer_data = np.array(infer_data)
246-
exp_data = np.exp(infer_data)
247-
softmax_data = np.exp(infer_data) / np.sum(exp_data, axis=1)
245+
logits = np.array(infer_data[0])
246+
max_value = np.max(logits, axis=1, keepdims=True)
247+
exp_data = np.exp(logits - max_value)
248+
probs = exp_data / np.sum(exp_data, axis=1, keepdims=True)
248249
out_dict = {
249-
"label": softmax_data.argmax(axis=-1),
250-
"confidence": softmax_data.max(axis=-1)
250+
"label": probs.argmax(axis=-1),
251+
"confidence": probs.max(axis=-1)
251252
}
252253
return out_dict
253254

0 commit comments

Comments
 (0)