Skip to content

Commit

Permalink
update pyramidbox_lite_server (#1978)
Browse files Browse the repository at this point in the history
* update pyramidbox_lite_server

* update

* add clean func

* update save inference model
  • Loading branch information
jm12138 authored Nov 4, 2022
1 parent c58c0ca commit 404ab2d
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 48 deletions.
16 changes: 7 additions & 9 deletions modules/image/face_detection/pyramidbox_lite_server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,13 @@


- ```python
def save_inference_model(dirname,
model_filename=None,
params_filename=None,
combined=True)
def save_inference_model(dirname)
```
- 将模型保存到指定路径。

- **参数**

- dirname: 存在模型的目录名称; <br/>
- model\_filename: 模型文件名称,默认为\_\_model\_\_<br/>
- params\_filename: 参数文件名称,默认为\_\_params\_\_(仅当`combined`为True时生效);<br/>
- combined: 是否将参数保存到统一的一个文件中。
- dirname: 模型保存路径 <br/>


## 四、服务部署
Expand Down Expand Up @@ -171,6 +165,10 @@

移除 fluid api

* 1.3.0

修复无法导出推理模型的问题

- ```shell
$ hub install pyramidbox_lite_server==1.2.1
$ hub install pyramidbox_lite_server==1.3.0
```
16 changes: 7 additions & 9 deletions modules/image/face_detection/pyramidbox_lite_server/README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,13 @@


- ```python
def save_inference_model(dirname,
model_filename=None,
params_filename=None,
combined=True)
def save_inference_model(dirname)
```
- Save model to specific path

- **Parameters**

- dirname: output dir for saving model
- model\_filename: filename for saving model
- params\_filename: filename for saving parameters
- combined: whether save parameters into one file
- dirname: model save path


## IV.Server Deployment
Expand Down Expand Up @@ -171,6 +165,10 @@

Remove fluid api

* 1.3.0

Fix a bug of save_inference_model

- ```shell
$ hub install pyramidbox_lite_server==1.2.1
$ hub install pyramidbox_lite_server==1.3.0
```
40 changes: 11 additions & 29 deletions modules/image/face_detection/pyramidbox_lite_server/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
import paddle
from paddle.inference import Config
from paddle.inference import create_predictor
from pyramidbox_lite_server.data_feed import reader
from pyramidbox_lite_server.processor import base64_to_cv2
from pyramidbox_lite_server.processor import postprocess
from .data_feed import reader
from .processor import base64_to_cv2
from .processor import postprocess

import paddlehub as hub
from paddlehub.module.module import moduleinfo
from paddlehub.module.module import runnable
from paddlehub.module.module import serving
Expand All @@ -25,19 +24,20 @@
author="baidu-vis",
author_email="",
summary="PyramidBox-Lite-Server is a high-performance face detection model.",
version="1.2.1")
class PyramidBoxLiteServer(hub.Module):

def _initialize(self):
self.default_pretrained_model_path = os.path.join(self.directory, "pyramidbox_lite_server_face_detection")
version="1.3.0")
class PyramidBoxLiteServer:
def __init__(self):
self.default_pretrained_model_path = os.path.join(self.directory, "pyramidbox_lite_server_face_detection", "model")
self._set_config()
self.processor = self

def _set_config(self):
"""
predictor config setting
"""
cpu_config = Config(self.default_pretrained_model_path)
model = self.default_pretrained_model_path+'.pdmodel'
params = self.default_pretrained_model_path+'.pdiparams'
cpu_config = Config(model, params)
cpu_config.disable_glog_info()
cpu_config.disable_gpu()
self.cpu_predictor = create_predictor(cpu_config)
Expand All @@ -49,7 +49,7 @@ def _set_config(self):
except:
use_gpu = False
if use_gpu:
gpu_config = Config(self.default_pretrained_model_path)
gpu_config = Config(model, params)
gpu_config.disable_glog_info()
gpu_config.enable_use_gpu(memory_pool_init_size_mb=1000, device_id=0)
self.gpu_predictor = create_predictor(gpu_config)
Expand Down Expand Up @@ -125,24 +125,6 @@ def face_detection(self,
res.append(out)
return res

def save_inference_model(self, dirname, model_filename=None, params_filename=None, combined=True):
if combined:
model_filename = "__model__" if not model_filename else model_filename
params_filename = "__params__" if not params_filename else params_filename
place = paddle.CPUPlace()
exe = paddle.Executor(place)

program, feeded_var_names, target_vars = paddle.static.load_inference_model(
dirname=self.default_pretrained_model_path, executor=exe)

paddle.static.save_inference_model(dirname=dirname,
main_program=program,
executor=exe,
feeded_var_names=feeded_var_names,
target_vars=target_vars,
model_filename=model_filename,
params_filename=params_filename)

@serving
def serving_method(self, images, **kwargs):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import os
import time
from collections import OrderedDict

import base64
import cv2
Expand Down
133 changes: 133 additions & 0 deletions modules/image/face_detection/pyramidbox_lite_server/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import os
import shutil
import unittest

import cv2
import requests
import paddlehub as hub


os.environ['CUDA_VISIBLE_DEVICES'] = '0'


class TestHubModule(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
img_url = 'https://unsplash.com/photos/iFgRcqHznqg/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8MXx8ZmFjZXxlbnwwfHx8fDE2NjE5ODAyMTc&force=true&w=640'
if not os.path.exists('tests'):
os.makedirs('tests')
response = requests.get(img_url)
assert response.status_code == 200, 'Network Error.'
with open('tests/test.jpg', 'wb') as f:
f.write(response.content)
cls.module = hub.Module(name="pyramidbox_lite_server")

@classmethod
def tearDownClass(cls) -> None:
shutil.rmtree('tests')
shutil.rmtree('inference')
shutil.rmtree('detection_result')

def test_face_detection1(self):
results = self.module.face_detection(
paths=['tests/test.jpg'],
use_gpu=False,
visualization=False
)
bbox = results[0]['data'][0]

confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']

self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)

def test_face_detection2(self):
results = self.module.face_detection(
images=[cv2.imread('tests/test.jpg')],
use_gpu=False,
visualization=False
)
bbox = results[0]['data'][0]

confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']

self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)

def test_face_detection3(self):
results = self.module.face_detection(
images=[cv2.imread('tests/test.jpg')],
use_gpu=False,
visualization=True
)
bbox = results[0]['data'][0]

confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']

self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)

def test_face_detection4(self):
results = self.module.face_detection(
images=[cv2.imread('tests/test.jpg')],
use_gpu=True,
visualization=False
)
bbox = results[0]['data'][0]

confidence = bbox['confidence']
left = bbox['left']
right = bbox['right']
top = bbox['top']
bottom = bbox['bottom']

self.assertTrue(confidence > 0.5)
self.assertTrue(0 < left < 2000)
self.assertTrue(0 < right < 2000)
self.assertTrue(0 < top < 2000)
self.assertTrue(0 < bottom < 2000)

def test_face_detection5(self):
self.assertRaises(
AssertionError,
self.module.face_detection,
paths=['no.jpg']
)

def test_face_detection6(self):
self.assertRaises(
AttributeError,
self.module.face_detection,
images=['test.jpg']
)

def test_save_inference_model(self):
self.module.save_inference_model('./inference/model')

self.assertTrue(os.path.exists('./inference/model.pdmodel'))
self.assertTrue(os.path.exists('./inference/model.pdiparams'))


if __name__ == "__main__":
unittest.main()

0 comments on commit 404ab2d

Please sign in to comment.