mirror of
https://github.com/PaddlePaddle/PaddleClas.git
synced 2025-06-03 21:55:06 +08:00
parent
8dd6418ae5
commit
11ef05ca32
@ -18,7 +18,7 @@ sys.path.insert(0, ".")
|
|||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from paddlehub.common.logger import logger
|
from paddlehub.utils.log import logger
|
||||||
from paddlehub.module.module import moduleinfo, serving
|
from paddlehub.module.module import moduleinfo, serving
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -103,16 +103,16 @@ class ClasSystem(hub.Module):
|
|||||||
logger.info("error in loading image")
|
logger.info("error in loading image")
|
||||||
all_results.append([])
|
all_results.append([])
|
||||||
continue
|
continue
|
||||||
starttime = time.time()
|
|
||||||
|
|
||||||
self.args.image_file = img
|
self.args.image_file = img
|
||||||
self.args.top_k = top_k
|
self.args.top_k = top_k
|
||||||
|
|
||||||
|
starttime = time.time()
|
||||||
classes, scores = paddle_predict.predict(self.args, self.predictor)
|
classes, scores = paddle_predict.predict(self.args, self.predictor)
|
||||||
|
|
||||||
elapse = time.time() - starttime
|
elapse = time.time() - starttime
|
||||||
logger.info("Predict time: {}".format(elapse))
|
|
||||||
all_results.append([classes.tolist(), scores.tolist()])
|
|
||||||
|
|
||||||
|
logger.info("Predict time: {}".format(elapse))
|
||||||
|
all_results.append([classes.tolist(), scores.tolist(), elapse])
|
||||||
return all_results
|
return all_results
|
||||||
|
|
||||||
@serving
|
@serving
|
||||||
@ -124,10 +124,3 @@ class ClasSystem(hub.Module):
|
|||||||
images_decode = [to_cv2(image) for image in images]
|
images_decode = [to_cv2(image) for image in images]
|
||||||
results = self.predict(images_decode, **kwargs)
|
results = self.predict(images_decode, **kwargs)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
clas = ClasSystem()
|
|
||||||
image_path = ['./deploy/hubserving/ILSVRC2012_val_00006666.JPEG', ]
|
|
||||||
res = clas.predict(paths=image_path, top_k=5)
|
|
||||||
print(res)
|
|
||||||
|
22
deploy/hubserving/clas/test.py
Normal file
22
deploy/hubserving/clas/test.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import paddlehub as hub
|
||||||
|
|
||||||
|
image_path = ["./deploy/hubserving/ILSVRC2012_val_00006666.JPEG", ]
|
||||||
|
top_k = 5
|
||||||
|
module = hub.Module(name="clas_system")
|
||||||
|
res = module.predict(paths=image_path, top_k=top_k)
|
||||||
|
for i, image in enumerate(image_path):
|
||||||
|
print("The returned result of {}: {}".format(image, res[i]))
|
@ -120,8 +120,15 @@ hub serving start -c deploy/hubserving/clas/config.json
|
|||||||
访问示例:
|
访问示例:
|
||||||
```python tools/test_hubserving.py http://127.0.0.1:8866/predict/clas_system ./deploy/hubserving/ILSVRC2012_val_00006666.JPEG 5```
|
```python tools/test_hubserving.py http://127.0.0.1:8866/predict/clas_system ./deploy/hubserving/ILSVRC2012_val_00006666.JPEG 5```
|
||||||
|
|
||||||
## 返回结果格式说明
|
### 返回结果格式说明
|
||||||
返回结果为列表(list),包含 `clas`,以及所有得分组成的 `scores` (list类型), `scores` 包含前 `top_k` 个 `score` 。
|
返回结果为列表(list),包含top-k个分类结果,以及对应的得分,还有此图片预测耗时,具体如下:
|
||||||
|
```
|
||||||
|
list: 返回结果
|
||||||
|
└─ list: 第一张图片结果
|
||||||
|
└─ list: 前k个分类结果,依score递减排序
|
||||||
|
└─ list: 前k个分类结果对应的score,依score递减排序
|
||||||
|
└─ float: 该图分类耗时,单位秒
|
||||||
|
```
|
||||||
|
|
||||||
**说明:** 如果需要增加、删除、修改返回字段,可在相应模块的`module.py`文件中进行修改,完整流程参考下一节自定义修改服务模块。
|
**说明:** 如果需要增加、删除、修改返回字段,可在相应模块的`module.py`文件中进行修改,完整流程参考下一节自定义修改服务模块。
|
||||||
|
|
||||||
@ -132,7 +139,9 @@ hub serving start -c deploy/hubserving/clas/config.json
|
|||||||
```hub serving stop --port/-p XXXX```
|
```hub serving stop --port/-p XXXX```
|
||||||
|
|
||||||
- 2、 到相应的`module.py`和`params.py`等文件中根据实际需求修改代码。
|
- 2、 到相应的`module.py`和`params.py`等文件中根据实际需求修改代码。
|
||||||
例如,例如需要替换部署服务所用模型,则需要到`params.py`中修改模型路径参数`cfg.model_file`和`cfg.params_file`。 **强烈建议修改后先直接运行`module.py`调试,能正确运行预测后再启动服务测试。**
|
例如,例如需要替换部署服务所用模型,则需要到`params.py`中修改模型路径参数`cfg.model_file`和`cfg.params_file`。
|
||||||
|
|
||||||
|
修改并安装(`hub install deploy/hubserving/clas/`)完成后,在进行部署前,可通过`python deploy/hubserving/clas/test.py`测试已安装服务模块。
|
||||||
|
|
||||||
- 3、 卸载旧服务包
|
- 3、 卸载旧服务包
|
||||||
```hub uninstall clas_system```
|
```hub uninstall clas_system```
|
||||||
|
@ -124,29 +124,40 @@ Two parameters need to be passed to the script:
|
|||||||
python tools/test_hubserving.py http://127.0.0.1:8866/predict/clas_system ./deploy/hubserving/ILSVRC2012_val_00006666.JPEG 5
|
python tools/test_hubserving.py http://127.0.0.1:8866/predict/clas_system ./deploy/hubserving/ILSVRC2012_val_00006666.JPEG 5
|
||||||
```
|
```
|
||||||
|
|
||||||
## Returned result format
|
### Returned result format
|
||||||
The returned result is a list, including classification results(`clas`), and the `top_k`'s scores(`socres`). And `scores` is a list, consist of `score`.
|
The returned result is a list, including the `top_k`'s classification results, corresponding scores and the time cost of prediction, details as follows.
|
||||||
|
|
||||||
|
```
|
||||||
|
list: The returned results
|
||||||
|
└─ list: The result of first picture
|
||||||
|
└─ list: The top-k classification results, sorted in descending order of score
|
||||||
|
└─ list: The scores corresponding to the top-k classification results, sorted in descending order of score
|
||||||
|
└─ float: The time cost of predicting the picture, unit second
|
||||||
|
```
|
||||||
|
|
||||||
**Note:** If you need to add, delete or modify the returned fields, you can modify the file `module.py` of the corresponding module. For the complete process, refer to the user-defined modification service module in the next section.
|
**Note:** If you need to add, delete or modify the returned fields, you can modify the file `module.py` of the corresponding module. For the complete process, refer to the user-defined modification service module in the next section.
|
||||||
|
|
||||||
## User defined service module modification
|
## User defined service module modification
|
||||||
If you need to modify the service logic, the following steps are generally required:
|
If you need to modify the service logic, the following steps are generally required:
|
||||||
|
|
||||||
- 1. Stop service
|
1. Stop service
|
||||||
```shell
|
```shell
|
||||||
hub serving stop --port/-p XXXX
|
hub serving stop --port/-p XXXX
|
||||||
```
|
```
|
||||||
- 2. Modify the code in the corresponding files, like `module.py` and `params.py`, according to the actual needs.
|
2. Modify the code in the corresponding files, like `module.py` and `params.py`, according to the actual needs.
|
||||||
For example, if you need to replace the model used by the deployed service, you need to modify model path parameters `cfg.model_file` and `cfg.params_file` in `params.py`. Of course, other related parameters may need to be modified at the same time. Please modify and debug according to the actual situation. It is suggested to run `module.py` directly for debugging after modification before starting the service test.
|
For example, if you need to replace the model used by the deployed service, you need to modify model path parameters `cfg.model_file` and `cfg.params_file` in `params.py`. Of course, other related parameters may need to be modified at the same time. Please modify and debug according to the actual situation.
|
||||||
- 3. Uninstall old service module
|
|
||||||
|
After modifying and installing (`hub install deploy/hubserving/clas/`) and before deploying, you can use `python deploy/hubserving/clas/test.py` to test the installed service module.
|
||||||
|
|
||||||
|
1. Uninstall old service module
|
||||||
```shell
|
```shell
|
||||||
hub uninstall clas_system
|
hub uninstall clas_system
|
||||||
```
|
```
|
||||||
- 4. Install modified service module
|
4. Install modified service module
|
||||||
```shell
|
```shell
|
||||||
hub install deploy/hubserving/clas/
|
hub install deploy/hubserving/clas/
|
||||||
```
|
```
|
||||||
- 5. Restart service
|
5. Restart service
|
||||||
```shell
|
```shell
|
||||||
hub serving start -m clas_system
|
hub serving start -m clas_system
|
||||||
```
|
```
|
||||||
|
@ -64,24 +64,21 @@ def main(url, image_path, top_k=1):
|
|||||||
continue
|
continue
|
||||||
data = {'images': [cv2_to_base64(img)], 'top_k': top_k}
|
data = {'images': [cv2_to_base64(img)], 'top_k': top_k}
|
||||||
|
|
||||||
starttime = time.time()
|
|
||||||
try:
|
try:
|
||||||
r = requests.post(url=url, headers=headers, data=json.dumps(data))
|
r = requests.post(url=url, headers=headers, data=json.dumps(data))
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("File:{}, {}".format(file_str, e))
|
logger.error("File:{}, {}".format(file_str, e))
|
||||||
continue
|
continue
|
||||||
elapse = time.time() - starttime
|
|
||||||
total_time += elapse
|
|
||||||
if r.json()['status'] != '000':
|
if r.json()['status'] != '000':
|
||||||
logger.error(
|
logger.error(
|
||||||
"File:{}, The parameters returned by the server are: {}".
|
"File:{}, The parameters returned by the server are: {}".
|
||||||
format(file_str, r.json()['msg']))
|
format(file_str, r.json()['msg']))
|
||||||
continue
|
continue
|
||||||
res = r.json()["results"][0]
|
res = r.json()["results"][0]
|
||||||
classes = res[0]
|
classes, scores, elapse = res
|
||||||
scores = res[1]
|
|
||||||
all_acc += scores[0]
|
all_acc += scores[0]
|
||||||
|
total_time += elapse
|
||||||
cnt += 1
|
cnt += 1
|
||||||
|
|
||||||
scores = map(lambda x: round(x, 5), scores)
|
scores = map(lambda x: round(x, 5), scores)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user