forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#51 from jiweibo/mask_demo
update mask demo with 2.0 api
- Loading branch information
Showing
1 changed file
with
39 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,42 @@ | ||
import numpy as np | ||
from paddle.fluid.core import AnalysisConfig | ||
from paddle.fluid.core import create_paddle_predictor | ||
from paddle.inference import Config | ||
from paddle.inference import create_predictor | ||
|
||
class Model: | ||
def __init__(self, model_file, params_file, use_mkldnn=True, use_gpu = False, device_id = 0): | ||
config = AnalysisConfig(model_file, params_file) | ||
config.switch_use_feed_fetch_ops(False) | ||
config.switch_specify_input_names(True) | ||
config.enable_memory_optim() | ||
|
||
if use_gpu: | ||
print ("ENABLE_GPU") | ||
config.enable_use_gpu(100, device_id) | ||
|
||
if use_mkldnn: | ||
config.enable_mkldnn() | ||
self.predictor = create_paddle_predictor(config) | ||
|
||
def run(self, img_list): | ||
|
||
input_names = self.predictor.get_input_names() | ||
for i, name in enumerate(input_names): | ||
input_tensor = self.predictor.get_input_tensor(input_names[i]) | ||
input_tensor.reshape(img_list[i].shape) | ||
input_tensor.copy_from_cpu(img_list[i].copy()) | ||
|
||
self.predictor.zero_copy_run() | ||
|
||
results = [] | ||
output_names = self.predictor.get_output_names() | ||
|
||
for i, name in enumerate(output_names): | ||
output_tensor = self.predictor.get_output_tensor(output_names[i]) | ||
output_data = output_tensor.copy_to_cpu() | ||
results.append(output_data) | ||
|
||
return results | ||
|
||
class Model: | ||
def __init__(self, | ||
model_file, | ||
params_file, | ||
use_mkldnn=True, | ||
use_gpu=False, | ||
device_id=0): | ||
config = Config(model_file, params_file) | ||
config.enable_memory_optim() | ||
|
||
if use_gpu: | ||
print("ENABLE_GPU") | ||
config.enable_use_gpu(100, device_id) | ||
|
||
if use_mkldnn: | ||
config.enable_mkldnn() | ||
self.predictor = create_predictor(config) | ||
|
||
def run(self, img_list): | ||
|
||
input_names = self.predictor.get_input_names() | ||
for i, name in enumerate(input_names): | ||
input_tensor = self.predictor.get_input_handle(input_names[i]) | ||
input_tensor.reshape(img_list[i].shape) | ||
input_tensor.copy_from_cpu(img_list[i].copy()) | ||
|
||
self.predictor.run() | ||
|
||
results = [] | ||
output_names = self.predictor.get_output_names() | ||
|
||
for i, name in enumerate(output_names): | ||
output_tensor = self.predictor.get_output_handle(output_names[i]) | ||
output_data = output_tensor.copy_to_cpu() | ||
results.append(output_data) | ||
|
||
return results |