DetectionTeamUCAS / RetinaNet_Tensorflow_Rotation

Focal Loss for Dense Rotation Object Detection
MIT License
312 stars 93 forks source link

iou smooth #54

Closed lijain closed 4 years ago

lijain commented 4 years ago

What's the reason why I want to add IOU in your r2cnn + + and then add tf.py_function() function without execution? def iou_smooth_l1_loss_rcnn_r(bbox_pred, bbox_targets, label, num_classes, sigma=1.0): '''

:param bbox_pred: [-1, (cfgs.CLS_NUM +1) * 5]
:param bbox_targets:[-1, (cfgs.CLS_NUM +1) * 5]
:param label:[-1]
:param num_classes:
:param sigma:
:return:
'''

outside_mask = tf.stop_gradient(tf.to_float(tf.greater(label, 0)))

bbox_pred = tf.reshape(bbox_pred, [-1, num_classes, 5])
bbox_targets = tf.reshape(bbox_targets, [-1, num_classes, 5])

regression_loss= _smooth_l1_loss_base(bbox_pred,
                             bbox_targets,
                             sigma=sigma)
print("######regression_loss:",regression_loss,regression_loss.shape)
print("######bbox_pred,:", tf.reshape(bbox_pred, [-1, 5]), tf.reshape(bbox_pred, [-1, 5]).shape)
print("######bbox_targets:", tf.reshape(bbox_targets[:, :-1], [-1, 5]), tf.reshape(bbox_targets[:, :-1], [-1, 5]).shape)
# 计算预测值与真实目标框的iou
overlaps = tf.py_func(iou_rotate_calculate2,[tf.reshape(bbox_pred, [-1, 5]),tf.reshape(bbox_targets[:, :-1], [-1, 5])],[tf.float32])
print("######overlaps:", overlaps)
regression_loss = tf.reduce_sum(regression_loss, 2)
print("######regression_loss:", regression_loss, regression_loss.shape)
regression_loss = tf.reshape(regression_loss, [-1, num_classes])
print("######regression_loss1:", regression_loss, regression_loss.shape)
# 计算方向因子
iou_factor = tf.stop_gradient(-1 * tf.log(tf.reduce_mean(overlaps))) / (tf.stop_gradient(tf.reduce_mean(regression_loss)) + 1e-6)
print("######iou_factor:", iou_factor, iou_factor.shape)
normalizer = tf.stop_gradient(tf.where(tf.equal(outside_mask, 1)))
normalizer = tf.cast(tf.shape(normalizer)[0], tf.float32)
normalizer = tf.maximum(1.0, normalizer)
print("######normalizer:", normalizer, normalizer.shape)
bbox_loss = tf.reduce_sum(
    tf.reduce_sum(regression_loss * iou_factor)) / normalizer
print("######bbox_loss:", bbox_loss, bbox_loss.shape)
os._exit()
return bbox_loss

No function called in the line tf.py_func

yangxue0827 commented 4 years ago

tf.pyfunc only will be executed when the data feed into the whole graph. So you may not see the print information when you are building graph.

yangxue0827 commented 4 years ago

tensorflow is a static graph framework.

lijain commented 4 years ago

result:: restore from pretrained_weighs in IMAGE_NET W1219 14:01:40.316207 139632994981632 deprecation.py:323] From multi_gpu_train.py:295: start_queue_runners (from tensorflow.python.training.queue_runner_impl) is deprecated and will be removed in a future version. Instructions for updating: To construct input pipelines, use the tf.data module. W1219 14:01:40.322418 139632994981632 deprecation_wrapper.py:119] From multi_gpu_train.py:299: The name tf.summary.FileWriter is deprecated. Please use tf.compat.v1.summary.FileWriter instead. W1219 14:01:44.381587 139632994981632 deprecation.py:323] From /data/maq/anaconda3/envs/R2CNN++/lib/python3.6/site-packages/tensorflow/python/training/saver.py:1276: checkpoint_exists (from tensorflow.python.training.checkpoint_management) is deprecated and will be removed in a future version. Instructions for updating: Use standard file APIs to check for files with this prefix. restore model

boxes1: [[754.0592   409.5029   126.65204   25.286615 -90.361   ]

 [762.2895   409.4651   127.14709   25.38398  -90.36403 ]  [753.849    417.4832   126.77838   25.319916 -90.5137  ]  [762.0659   417.50424  127.09755   25.344255 -90.5188  ]

------------------ 原始邮件 ------------------ 发件人: "yangxue"<notifications@github.com>; 发送时间: 2019年12月20日(星期五) 中午11:47 收件人: "DetectionTeamUCAS/RetinaNet_Tensorflow_Rotation"<RetinaNet_Tensorflow_Rotation@noreply.github.com>; 抄送: "利剑"<1632401541@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [DetectionTeamUCAS/RetinaNet_Tensorflow_Rotation] iou smooth (#54)

tensorflow is a static graph framework.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

lijain commented 4 years ago

I wrote the printout boxes1 in the transfer function, which can be printed out in your retennet rotation box. I just wrote it in r2cnn + +

------------------ 原始邮件 ------------------ 发件人: "yangxue"<notifications@github.com>; 发送时间: 2019年12月20日(星期五) 中午11:44 收件人: "DetectionTeamUCAS/RetinaNet_Tensorflow_Rotation"<RetinaNet_Tensorflow_Rotation@noreply.github.com>; 抄送: "利剑"<1632401541@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [DetectionTeamUCAS/RetinaNet_Tensorflow_Rotation] iou smooth (#54)

tf.pyfunc only will be executed when the data feed into the whole graph. So you may not see the print information when you are building graph.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

a5372935 commented 4 years ago

What's the reason why I want to add IOU in your r2cnn + + and then add tf.py_function() function without execution? def iou_smooth_l1_loss_rcnn_r(bbox_pred, bbox_targets, label, num_classes, sigma=1.0): '''

:param bbox_pred: [-1, (cfgs.CLS_NUM +1) * 5]
:param bbox_targets:[-1, (cfgs.CLS_NUM +1) * 5]
:param label:[-1]
:param num_classes:
:param sigma:
:return:
'''

outside_mask = tf.stop_gradient(tf.to_float(tf.greater(label, 0)))

bbox_pred = tf.reshape(bbox_pred, [-1, num_classes, 5])
bbox_targets = tf.reshape(bbox_targets, [-1, num_classes, 5])

regression_loss= _smooth_l1_loss_base(bbox_pred,
                             bbox_targets,
                             sigma=sigma)
print("######regression_loss:",regression_loss,regression_loss.shape)
print("######bbox_pred,:", tf.reshape(bbox_pred, [-1, 5]), tf.reshape(bbox_pred, [-1, 5]).shape)
print("######bbox_targets:", tf.reshape(bbox_targets[:, :-1], [-1, 5]), tf.reshape(bbox_targets[:, :-1], [-1, 5]).shape)
# 计算预测值与真实目标框的iou
overlaps = tf.py_func(iou_rotate_calculate2,[tf.reshape(bbox_pred, [-1, 5]),tf.reshape(bbox_targets[:, :-1], [-1, 5])],[tf.float32])
print("######overlaps:", overlaps)
regression_loss = tf.reduce_sum(regression_loss, 2)
print("######regression_loss:", regression_loss, regression_loss.shape)
regression_loss = tf.reshape(regression_loss, [-1, num_classes])
print("######regression_loss1:", regression_loss, regression_loss.shape)
# 计算方向因子
iou_factor = tf.stop_gradient(-1 * tf.log(tf.reduce_mean(overlaps))) / (tf.stop_gradient(tf.reduce_mean(regression_loss)) + 1e-6)
print("######iou_factor:", iou_factor, iou_factor.shape)
normalizer = tf.stop_gradient(tf.where(tf.equal(outside_mask, 1)))
normalizer = tf.cast(tf.shape(normalizer)[0], tf.float32)
normalizer = tf.maximum(1.0, normalizer)
print("######normalizer:", normalizer, normalizer.shape)
bbox_loss = tf.reduce_sum(
    tf.reduce_sum(regression_loss * iou_factor)) / normalizer
print("######bbox_loss:", bbox_loss, bbox_loss.shape)
os._exit()
return bbox_loss

No function called in the line tf.py_func

@lijain 請問你這代碼 在overlaps = tf.py_func(iou_rotate_calculate2,[tf.reshape(bbox_pred, [-1, 5]),tf.reshape(bbox_targets[:, :-1], [-1, 5])],[tf.float32]) 這裡會報錯

tf.reshape(bbox_pred, [-1, 5]) 與 tf.reshape(bbox_targets[:, :-1], [-1, 5]) size 不吻合,請問你解決了嗎