tensorflow tf.py_func

tf.py_func

在 faster  rcnn的tensorflow 实现中看到这个函数

1
rois,rpn_scores = tf.py_func(proposal_layer,[rpn_cls_prob,rpn_bbox_pred, self ._im_info, self .mode, self ._feat_stride, self ._anchors, self ._num_anchors],[tf.float32,tf.float32],name = "proposal" )

  tensorflow 官网上的解释

py_func(
    func,     inp,     Tout,     stateful=True,     name=None )
将python 函数包装为一个tensorflow操作符
python 函数proposal_layer 以numpy 矩阵作为输入输出,使函数变为tensorflow图中的操作符
定义一个简单的sinh函数在tensorflow图中:

def my_func(x):

  
# x will be a numpy array with the contents of the placeholder below

  
  return np.sinh(x)

inp =tf.placeholder(tf.float32)

y =tf.py_func(my_func, [inp], tf.float32)



tf.py_func在定义多输出函数时,输出变量类型需要用[ ]框起来;
tf.py_func在定义单输出函数时,输出变量 类型不能再用[ ]框起来;
这个需要各外注意!
例如:
多变量时
[python] view plain copy
  1. def _proposal_layer(self, rpn_cls_prob, rpn_bbox_pred, name):  
  2.   with tf.variable_scope(name) as scope:  
  3.     rois, rpn_scores, inds= tf.py_func(proposal_layer,  
  4.                                   [rpn_cls_prob, rpn_bbox_pred, self._im_info, self._mode,  
  5.                                    self._feat_stride, self._anchors, self._num_anchors],  
  6.                                   [tf.float32, tf.float32,tf.int64])  
  7.     # rois.set_shape([None, 5])  
  8.     # rpn_scores.set_shape([None, 1])  
  9.   rois.set_shape([1,None,None,self._num_anchors*5])  
  10.   rpn_scores.set_shape([1,None,None,self._num_anchors*1])  
  11.   return rois, rpn_scores,inds  
单变量时
[python] view plain copy
  1. def _draw_proposals_to_image(self,rois,scores,inds,keep_inds,stride,name):  
  2.   with tf.variable_scope(name) as scope:  
  3.     mask = tf.py_func(  
  4.       proposals_to_image,  
  5.       [rois, scores, inds, keep_inds,stride],  
  6.       tf.float32)  
  7.   
  8.     mask = tf.stop_gradient(mask)  
  9.     mask.set_shape([1NoneNone, cfg.TRAIN.BATCH_SIZE])  
  10.   
  11.     return mask 

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/80220068