-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update ultra_light_fast_generic_face_detector_1mb_320 (#1965)
* update ultra_light_fast_generic_face_detector_1mb * add clean func * update save inference model
- Loading branch information
Showing
4 changed files
with
158 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,10 @@ | |
import paddle | ||
from paddle.inference import Config | ||
from paddle.inference import create_predictor | ||
from ultra_light_fast_generic_face_detector_1mb_320.data_feed import reader | ||
from ultra_light_fast_generic_face_detector_1mb_320.processor import base64_to_cv2 | ||
from ultra_light_fast_generic_face_detector_1mb_320.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 | ||
|
@@ -27,19 +26,20 @@ | |
author_email="[email protected]", | ||
summary= | ||
"Ultra-Light-Fast-Generic-Face-Detector-1MB is a high-performance object detection model release on https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB.", | ||
version="1.1.3") | ||
class FaceDetector320(hub.Module): | ||
|
||
def _initialize(self): | ||
version="1.2.0") | ||
class FaceDetector320: | ||
def __init__(self): | ||
self.default_pretrained_model_path = os.path.join(self.directory, | ||
"ultra_light_fast_generic_face_detector_1mb_320") | ||
"ultra_light_fast_generic_face_detector_1mb_320", "model") | ||
self._set_config() | ||
|
||
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) | ||
|
@@ -51,29 +51,11 @@ 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) | ||
|
||
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) | ||
|
||
def face_detection(self, | ||
images=None, | ||
paths=None, | ||
|
133 changes: 133 additions & 0 deletions
133
modules/image/face_detection/ultra_light_fast_generic_face_detector_1mb_320/test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://ai-studio-static-online.cdn.bcebos.com/7799a8ccc5f6471b9d56fb6eff94f82a08b70ca2c7594d3f99877e366c0a2619' | ||
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="ultra_light_fast_generic_face_detector_1mb_320") | ||
|
||
@classmethod | ||
def tearDownClass(cls) -> None: | ||
shutil.rmtree('tests') | ||
shutil.rmtree('inference') | ||
shutil.rmtree('face_detector_320_predict_output') | ||
|
||
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(1000 < left < 4000) | ||
self.assertTrue(1000 < right < 4000) | ||
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(1000 < left < 4000) | ||
self.assertTrue(1000 < right < 4000) | ||
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(1000 < left < 4000) | ||
self.assertTrue(1000 < right < 4000) | ||
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(1000 < left < 4000) | ||
self.assertTrue(1000 < right < 4000) | ||
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() |