Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#51 from jiweibo/mask_demo
Browse files Browse the repository at this point in the history
update mask demo with 2.0 api
  • Loading branch information
jiweibo authored Dec 3, 2020
2 parents a0e45d3 + e252cad commit a03ce4d
Showing 1 changed file with 39 additions and 36 deletions.
75 changes: 39 additions & 36 deletions python/mask_detection/models/pd_model.py
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

0 comments on commit a03ce4d

Please sign in to comment.