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
- def _proposal_layer(self, rpn_cls_prob, rpn_bbox_pred, name):
- with tf.variable_scope(name) as scope:
- rois, rpn_scores, inds= 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,tf.int64])
- # rois.set_shape([None, 5])
- # rpn_scores.set_shape([None, 1])
- rois.set_shape([1,None,None,self._num_anchors*5])
- rpn_scores.set_shape([1,None,None,self._num_anchors*1])
- return rois, rpn_scores,inds
When univariate
- def _draw_proposals_to_image(self,rois,scores,inds,keep_inds,stride,name):
- with tf.variable_scope(name) as scope:
- mask = tf.py_func(
- proposals_to_image,
- [rois, scores, inds, keep_inds,stride],
- tf.float32)
- mask = tf.stop_gradient(mask)
- mask.set_shape([1, None, None, cfg.TRAIN.BATCH_SIZE])
- return mask