py_func使用问题
Created by: ResearchingDexter
1)PaddlePaddle版本:1.4.1 2)CPU/GPU:如果您使用GPU训练,请提供GPU驱动版本、CUDA和cuDNN版本号 3)系统环境:请您描述系统类型、版本,例如Mac OS 10.14 4)Python 3.6 5)显存信息 运行下面代码
import paddle.fluid as fluid
import numpy as np
def _bbox_overlaps(roi_boxes:np.ndarray, gt_boxes:np.ndarray):
lt = np.maximum(roi_boxes[:,np.newaxis, :2], gt_boxes[:, :2])
rb = np.minimum(roi_boxes[:,np.newaxis, 2:], gt_boxes[:, 2:])
wh=np.clip((rb-lt+1),a_min=0,a_max=None)
inter=wh[:,:,0]*wh[:,:,1]
w1 = np.maximum(roi_boxes[:, 2] - roi_boxes[:, 0] + 1, 0)
h1 = np.maximum(roi_boxes[:, 3] - roi_boxes[:, 1] + 1, 0)
w2 = np.maximum(gt_boxes[:, 2] - gt_boxes[:, 0] + 1, 0)
h2 = np.maximum(gt_boxes[:, 3] - gt_boxes[:, 1] + 1, 0)
area1 = w1 * h1
area2 = w2 * h2
overlap=inter/(area1[:,np.newaxis]+area2-inter)
return overlap
def create_tmp_var(name, dtype, shape):
return fluid.default_main_program().current_block().create_var(
name=name, dtype=dtype, shape=shape)
def py_func():
main=fluid.Program()
start=fluid.Program()
with fluid.program_guard(main,start):
roi=fluid.layers.data('roi',[3,4],append_batch_size=False)
gt=fluid.layers.data('gt',[2,4],append_batch_size=False)
overlap=create_tmp_var('overlap','float32',[3,2])
out=fluid.layers.py_func(_bbox_overlaps,[roi,gt],overlap)
exe=fluid.Executor(fluid.CPUPlace())
exe.run(start)
out=exe.run(main,feed={'roi':np.ones([3,4]),'gt':np.ones([2,4])},fetch_list=[out])
print(out)
py_func()
报错信息如下
---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-2-b318fada9f02> in <module>
29 out=exe.run(main,feed={'roi':np.ones([3,4]),'gt':np.ones([2,4])},fetch_list=[out])
30 print(out)
---> 31 py_func()
<ipython-input-2-b318fada9f02> in py_func()
24 gt=fluid.layers.data('gt',[2,4],append_batch_size=False)
25 overlap=create_tmp_var('overlap','float32',[3,2])
---> 26 out=fluid.layers.py_func(_bbox_overlaps,[roi,gt],overlap)
27 exe=fluid.Executor(fluid.CPUPlace())
28 exe.run(start)
/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/paddle/fluid/layers/nn.py in py_func(func, x, out, backward_func, skip_vars_in_backward_input)
10634 'Output must be Variable/list(Variable)/tuple(Variable)')
10635
> 10636 fwd_func_id = PyFuncRegistry(func).id
10637 bwd_func_id = PyFuncRegistry(
10638 backward_func).id if backward_func is not None else -1
/opt/conda/envs/python35-paddle120-env/lib/python3.5/site-packages/paddle/fluid/layers/nn.py in __init__(self, func)
10468 self._func = func
10469 # find named args using reflection
> 10470 args = inspect.getargspec(self._func)
10471 if len(args[0]) == 0 and args[1] is None and args[2] is None:
10472 # Function with no inputs
/opt/conda/envs/python35-paddle120-env/lib/python3.5/inspect.py in getargspec(func)
1048 getfullargspec(func)
1049 if kwonlyargs or ann:
-> 1050 raise ValueError("Function has keyword-only arguments or annotations"
1051 ", use getfullargspec() API which can support them")
1052 return ArgSpec(args, varargs, varkw, defaults)
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them