Focal-Loss-implement-on-Tensorflow icon indicating copy to clipboard operation
Focal-Loss-implement-on-Tensorflow copied to clipboard

How to implemented it in faster-rcnn?

Open zqdeepbluesky opened this issue 6 years ago • 4 comments

@ailias Hi,thanks for your cool code. but I have little confused about how to use it. I want implemented it in faster rcnn,should I just change the softmax into focal loss or others?can you tell me how to use it? thanks so much.

zqdeepbluesky avatar Apr 24 '18 03:04 zqdeepbluesky

Yes, you just need replace your softmaxloss with focalloss function. Then everything is ok.

ailias avatar Jun 11 '18 02:06 ailias

hi,thanks so much, I will try it.

zqdeepbluesky avatar Jun 11 '18 09:06 zqdeepbluesky

@ailias hi,how are you . I want try it on faster rcnn ,just like you said change the softmax loss into focal_loss,but I find it's hard for me to change it .here the network.py function define the softmax_layer,the original code is https://github.com/endernewton/tf-faster-rcnn/blob/master/lib/nets/network.py I only intercepted the code for the softmax function:

` def _softmax_layer(self, bottom, name): if name.startswith('rpn_cls_prob_reshape'): input_shape = tf.shape(bottom) bottom_reshaped = tf.reshape(bottom, [-1, input_shape[-1]]) reshaped_score = tf.nn.softmax(bottom_reshaped, name=name) return tf.reshape(reshaped_score, input_shape) return tf.nn.softmax(bottom, name=name) .......... def add_losses(self, sigma_rpn=3.0): with tf.variable_scope('LOSS' + self._tag) as scope: # RPN, class loss rpn_cls_score = tf.reshape(self._predictions['rpn_cls_score_reshape'], [-1, 2]) rpn_label = tf.reshape(self._anchor_targets['rpn_labels'], [-1]) rpn_select = tf.where(tf.not_equal(rpn_label, -1)) rpn_cls_score = tf.reshape(tf.gather(rpn_cls_score, rpn_select), [-1, 2]) rpn_label = tf.reshape(tf.gather(rpn_label, rpn_select), [-1]) rpn_cross_entropy = tf.reduce_mean( tf.nn.sparse_softmax_cross_entropy_with_logits(logits=rpn_cls_score, labels=rpn_label)) .........

   # RCNN, class loss
  cls_score = self._predictions["cls_score"]
  label = tf.reshape(self._proposal_targets["labels"], [-1])
  cross_entropy = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=cls_score, labels=label))

......... def _region_proposal(self, net_conv, is_training, initializer): rpn = slim.conv2d(net_conv, cfg.RPN_CHANNELS, [3, 3], trainable=is_training, weights_initializer=initializer,scope="rpn_conv/3x3") self._act_summaries.append(rpn) rpn_cls_score = slim.conv2d(rpn, self._num_anchors * 2, [1, 1], trainable=is_training, weights_initializer=initializer, padding='VALID', activation_fn=None, scope='rpn_cls_score') # change it so that the score has 2 as its channel size rpn_cls_score_reshape = self._reshape_layer(rpn_cls_score, 2, 'rpn_cls_score_reshape') rpn_cls_prob_reshape = self._softmax_layer(rpn_cls_score_reshape, "rpn_cls_prob_reshape") rpn_cls_pred = tf.argmax(tf.reshape(rpn_cls_score_reshape, [-1, 2]), axis=1, name="rpn_cls_pred") rpn_cls_prob = self._reshape_layer(rpn_cls_prob_reshape, self._num_anchors * 2, "rpn_cls_prob") rpn_bbox_pred = slim.conv2d(rpn, self._num_anchors * 4, [1, 1], trainable=is_training, weights_initializer=initializer, padding='VALID', activation_fn=None, scope='rpn_bbox_pred') ............. def _region_classification(self, fc7, is_training, initializer, initializer_bbox): cls_score = slim.fully_connected(fc7, self._num_classes, weights_initializer=initializer, trainable=is_training, activation_fn=None, scope='cls_score') cls_prob = self._softmax_layer(cls_score, "cls_prob") cls_pred = tf.argmax(cls_score, axis=1, name="cls_pred") bbox_pred = slim.fully_connected(fc7, self._num_classes * 4, weights_initializer=initializer_bbox, trainable=is_training, activation_fn=None, scope='bbox_pred')

self._predictions["cls_score"] = cls_score
self._predictions["cls_pred"] = cls_pred
self._predictions["cls_prob"] = cls_prob
self._predictions["bbox_pred"] = bbox_pred

return cls_prob, bbox_pred

` when I change it I find the parameter between softmax and focal_loss are diffierent ,so I don't konw how to change it .can you tell me how to do it ?thanks so much.

zqdeepbluesky avatar Jun 12 '18 14:06 zqdeepbluesky

Maybe you should use tensorflow official models at https://github.com/tensorflow/models/tree/master/research/object_detection. Then you just replace the softmax_loss with focal_loss

ailias avatar Jun 15 '18 12:06 ailias