|
| 1 | +import tensorflow as tf |
| 2 | +from keras.losses import categorical_crossentropy |
| 3 | + |
| 4 | + |
| 5 | +class MultiBoxLoss: |
| 6 | + """ |
| 7 | + """ |
| 8 | + def __init__(self, n_classes, alpha=1.0, neg_pos_ratio=3.0, |
| 9 | + negatives_for_hard=100): |
| 10 | + self.n_classes = n_classes |
| 11 | + self.alpha = alpha |
| 12 | + self.neg_pos_ratio = neg_pos_ratio |
| 13 | + self.negatives_for_hard = negatives_for_hard |
| 14 | + |
| 15 | + def _softmax_loss(self, y_true, y_pred): |
| 16 | + """ |
| 17 | + """ |
| 18 | + softmax_loss = categorical_crossentropy(y_true, y_pred) |
| 19 | + # y_pred = tf.maximum(tf.minimum(y_pred, 1 - 1e-15), 1e-15) |
| 20 | + # softmax_loss = -tf.reduce_sum(y_true * tf.log(y_pred), axis=-1) |
| 21 | + return softmax_loss |
| 22 | + |
| 23 | + def _l1_smooth_loss(self, y_true, y_pred): |
| 24 | + """ |
| 25 | + """ |
| 26 | + abs_loss = tf.abs(y_true - y_pred) |
| 27 | + sq_loss = 0.5 * (y_true - y_pred)**2 |
| 28 | + l1_loss = tf.where(tf.less(abs_loss, 1.0), sq_loss, abs_loss - 0.5) |
| 29 | + return tf.reduce_sum(l1_loss, -1) |
| 30 | + |
| 31 | + def compute_loss_old(self, y_true, y_pred): |
| 32 | + """ compute loss |
| 33 | + """ |
| 34 | + batch_size = tf.shape(y_true)[0] |
| 35 | + num_boxes = tf.to_float(tf.shape(y_true)[1]) |
| 36 | + |
| 37 | + # loss for all default boxes |
| 38 | + conf_loss = self._softmax_loss(y_true[:, :, 4:], |
| 39 | + y_pred[:, :, 4:]) |
| 40 | + loc_loss = self._l1_smooth_loss(y_true[:, :, :4], |
| 41 | + y_pred[:, :, :4]) |
| 42 | + |
| 43 | + # positives loss |
| 44 | + num_pos = num_boxes - tf.reduce_sum(y_true[:, :, 4], axis=-1) |
| 45 | + fpmask = 1 - y_true[:, :, 4] |
| 46 | + pos_loc_loss = tf.reduce_sum(loc_loss * fpmask, axis=1) |
| 47 | + pos_conf_loss = tf.reduce_sum(conf_loss * fpmask, axis=1) |
| 48 | + |
| 49 | + # negatives loss |
| 50 | + num_neg = tf.minimum(self.neg_pos_ratio * num_pos, |
| 51 | + num_boxes - num_pos) |
| 52 | + pos_num_neg_mask = tf.greater(num_neg, 0) |
| 53 | + has_min = tf.to_float(tf.reduce_any(pos_num_neg_mask)) |
| 54 | + num_neg = tf.concat(axis=0, |
| 55 | + values=[num_neg, |
| 56 | + [(1 - has_min) * self.negatives_for_hard]]) |
| 57 | + num_neg_batch = tf.reduce_min(tf.boolean_mask(num_neg, |
| 58 | + tf.greater(num_neg, 0))) |
| 59 | + num_neg_batch = tf.to_int32(num_neg_batch) |
| 60 | + confs_start = 4 + 1 |
| 61 | + confs_end = confs_start + self.n_classes - 1 |
| 62 | + max_confs = tf.reduce_max(y_pred[:, :, confs_start:confs_end], |
| 63 | + axis=2) |
| 64 | + |
| 65 | + nvalues, indices = tf.nn.top_k(max_confs * y_true[:, :, 4], |
| 66 | + k=num_neg_batch) |
| 67 | + |
| 68 | + batch_idx = tf.expand_dims(tf.range(0, batch_size), 1) |
| 69 | + batch_idx = tf.tile(batch_idx, (1, num_neg_batch)) |
| 70 | + full_indices = (tf.reshape(batch_idx, [-1]) * tf.to_int32(num_boxes) + |
| 71 | + tf.reshape(indices, [-1])) |
| 72 | + |
| 73 | + neg_conf_loss = tf.gather(tf.reshape(conf_loss, [-1]), |
| 74 | + full_indices) |
| 75 | + neg_conf_loss = tf.reshape(neg_conf_loss, |
| 76 | + [batch_size, num_neg_batch]) |
| 77 | + neg_conf_loss = tf.reduce_sum(neg_conf_loss, axis=1) |
| 78 | + |
| 79 | + # loss is sum of positives and negatives |
| 80 | + total_loss = pos_conf_loss + neg_conf_loss |
| 81 | + total_loss /= (num_pos + tf.to_float(num_neg_batch)) |
| 82 | + num_pos = tf.where(tf.not_equal(num_pos, 0), num_pos, |
| 83 | + tf.ones_like(num_pos)) |
| 84 | + total_loss += (self.alpha * pos_loc_loss) / num_pos |
| 85 | + return total_loss |
| 86 | + |
| 87 | + def compute_loss(self, y_true, y_pred): |
| 88 | + """ compute loss |
| 89 | + """ |
| 90 | + batch_size = tf.shape(y_true)[0] |
| 91 | + num_boxes = tf.to_float(tf.shape(y_true)[1]) |
| 92 | + |
| 93 | + # loss for all default boxes |
| 94 | + conf_loss = self._softmax_loss(y_true[:, :, 4:], |
| 95 | + y_pred[:, :, 4:]) |
| 96 | + loc_loss = self._l1_smooth_loss(y_true[:, :, :4], |
| 97 | + y_pred[:, :, :4]) |
| 98 | + |
| 99 | + # positives loss |
| 100 | + num_pos = num_boxes - tf.reduce_sum(y_true[:, :, 4], axis=-1) |
| 101 | + fpmask = 1 - y_true[:, :, 4] |
| 102 | + pos_loc_loss = tf.reduce_sum(loc_loss * fpmask, axis=1) |
| 103 | + pos_conf_loss = tf.reduce_sum(conf_loss * fpmask, axis=1) |
| 104 | + |
| 105 | + # negatives loss |
| 106 | + num_neg = tf.minimum(self.neg_pos_ratio * num_pos, |
| 107 | + num_boxes - num_pos) |
| 108 | + pos_num_neg_mask = tf.greater(num_neg, 0) |
| 109 | + has_min = tf.to_float(tf.reduce_any(pos_num_neg_mask)) |
| 110 | + num_neg = tf.concat(axis=0, |
| 111 | + values=[num_neg, |
| 112 | + [(1 - has_min) * self.negatives_for_hard]]) |
| 113 | + num_neg_batch = tf.reduce_min(tf.boolean_mask(num_neg, |
| 114 | + tf.greater(num_neg, 0))) |
| 115 | + num_neg_batch = tf.to_int32(num_neg_batch) |
| 116 | + confs_start = 4 + 1 |
| 117 | + confs_end = confs_start + self.n_classes - 1 |
| 118 | + max_confs = tf.reduce_max(y_pred[:, :, confs_start:confs_end], |
| 119 | + axis=2) |
| 120 | + |
| 121 | + nvalues, indices = tf.nn.top_k(max_confs * y_true[:, :, 4], |
| 122 | + k=num_neg_batch) |
| 123 | + min_nvalues = nvalues[:, -1] |
| 124 | + min_nvalues = tf.expand_dims(min_nvalues, 1) |
| 125 | + min_nvalues = tf.tile(min_nvalues, (1, tf.shape(max_confs)[1])) |
| 126 | + nmask = tf.logical_not(tf.cast(fpmask, tf.bool)) |
| 127 | + nmask = tf.logical_and(nmask, |
| 128 | + tf.greater_equal(max_confs, min_nvalues)) |
| 129 | + fnmask = tf.to_float(nmask) |
| 130 | + |
| 131 | + neg_conf_loss = tf.reduce_sum(conf_loss * fnmask, axis=1) |
| 132 | + |
| 133 | + # loss is sum of positives and negatives |
| 134 | + total_loss = pos_conf_loss + neg_conf_loss |
| 135 | + total_loss /= (num_pos + tf.to_float(num_neg_batch)) |
| 136 | + num_pos = tf.where(tf.not_equal(num_pos, 0), num_pos, |
| 137 | + tf.ones_like(num_pos)) |
| 138 | + total_loss += (self.alpha * pos_loc_loss) / num_pos |
| 139 | + return total_loss |
0 commit comments