argman / EAST

A tensorflow implementation of EAST text detector
GNU General Public License v3.0
3.02k stars 1.05k forks source link

is focal loss help to imporve performance? how to decide score threshold when in test? #157

Open zjupx opened 6 years ago

zjupx commented 6 years ago

Hi, argman, I have some doubt about EAST. I need your help here. 1、Do you have train with focal loss, Does it work for improve the performance?

I have write a focal loss code, can you help me to check this implement is right or wrong?

def focal_loss(y_true_cls, y_pred_cls, training_mask, normalizer, gamma = 2, alpha =0.25): epsilon = 10e-8 y_pred_cls = tf.clip_by_value(y_pred_cls, epsilon, 1 - epsilon) logits = tf.log(y_pred_cls / (1. - y_pred_cls)) per_entry_cross_ent = (tf.nn.sigmoid_cross_entropy_with_logits( labels=y_true_cls, logits=logits)) prediction_probabilities = y_pred_cls p_t = ((y_true_cls prediction_probabilities) + ((1 - y_true_cls) (1 - prediction_probabilities))) modulating_factor = 1.0 if gamma: modulating_factor = tf.pow(1.0 - p_t, gamma) alpha_weight_factor = 1.0 if alpha is not None: alpha_weight_factor = (y_true_cls alpha + (1 - y_true_cls) (1 - alpha)) focal_cross_entropy_loss = (modulating_factor alpha_weight_factor per_entry_cross_ent) total_loss = tf.reduce_sum(focal_cross_entropy_loss * training_mask) total_loss /= normalizer tf.summary.scalar('classification_focal_loss', total_loss) return total_loss

def loss(y_true_cls, y_pred_cls, y_true_geo, y_pred_geo, training_mask): normalizer = tf.reduce_sum(tf.cast(tf.equal(y_true_cls * training_mask, 1), tf.int32)) + 1 normalizer = tf.cast(normalizer, tf.float32) tf.summary.scalar('normalizer', normalizer) classification_loss = focal_loss(y_true_cls, y_pred_cls, training_mask, normalizer)

d1 -> top, d2->right, d3->bottom, d4->left

d1_gt, d2_gt, d3_gt, d4_gt, theta_gt = tf.split(value=y_true_geo, num_or_size_splits=5, axis=3)
d1_pred, d2_pred, d3_pred, d4_pred, theta_pred = tf.split(value=y_pred_geo, num_or_size_splits=5, axis=3)
area_gt = (d1_gt + d3_gt) * (d2_gt + d4_gt)
area_pred = (d1_pred + d3_pred) * (d2_pred + d4_pred)
w_union = tf.minimum(d2_gt, d2_pred) + tf.minimum(d4_gt, d4_pred)
h_union = tf.minimum(d1_gt, d1_pred) + tf.minimum(d3_gt, d3_pred)
area_intersect = w_union * h_union
area_union = area_gt + area_pred - area_intersect
L_AABB = -tf.log((area_intersect + 1.0)/(area_union + 1.0))
L_theta = 1 - tf.cos(theta_pred - theta_gt)
tf.summary.scalar('geometry_AABB', tf.reduce_sum(L_AABB * y_true_cls * training_mask) / normalizer)
tf.summary.scalar('geometry_theta', tf.reduce_sum(L_theta * y_true_cls * training_mask) / normalizer)
L_g = L_AABB + 20 * L_theta
return tf.reduce_sum(L_g * y_true_cls * training_mask) / normalizer + classification_loss

2、how to choose score threshold and nms threshold ? Which score threshold and nms threshold will lead the Hmean best?

argman commented 6 years ago
  1. hard to read the code..i have tried focal loss, no gains found.
  2. myself trial and search