tensorflow tf.py_func

tf.py_func

See this function in the tensorflow implementation of faster rcnn

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" )

  Explanation on the tensorflow official website

py_func(
    func,     inp,     Tout,     stateful=True,     name=None )
Wrap a python function as a tensorflow operator
The python function proposal_layer takes numpy matrices as input and output, making the function an operator in the tensorflow graph
Define a simple sinh function in the tensorflow graph:

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)



When tf.py_func defines a multi-output function, the output variable type needs to be framed with [ ];
When tf.py_func defines a single output function, the output variable type can no longer be framed with [ ];
This needs everyone's attention!
E.g:
multivariate
[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  
When univariate
[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 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325858273&siteId=291194637