Esempio n. 1
0
    def compute(self, y_true, y_pred):
        # y.shape (batches, priors, 4 x bbox_offset + 8 x quadrilaterals + 5 x rbbox_offsets + n x class_label)

        batch_size = tf.shape(y_true)[0]
        num_priors = tf.shape(y_true)[1]
        num_classes = tf.shape(y_true)[2] - 17
        eps = K.epsilon()

        # confidence loss
        conf_true = tf.reshape(y_true[:, :, 17:], [-1, num_classes])
        conf_pred = tf.reshape(y_pred[:, :, 17:], [-1, num_classes])

        class_true = tf.argmax(conf_true, axis=1)
        class_pred = tf.argmax(conf_pred, axis=1)
        conf = tf.reduce_max(conf_pred, axis=1)

        neg_mask_float = conf_true[:, 0]
        neg_mask = tf.cast(neg_mask_float, tf.bool)
        pos_mask = tf.logical_not(neg_mask)
        pos_mask_float = tf.cast(pos_mask, tf.float32)
        num_total = tf.cast(tf.shape(conf_true)[0], tf.float32)
        num_pos = tf.reduce_sum(pos_mask_float)
        num_neg = num_total - num_pos

        conf_loss = focal_loss(conf_true, conf_pred, alpha=[0.002, 0.998])
        conf_loss = tf.reduce_sum(conf_loss)
        conf_loss = conf_loss / (num_total + eps)

        # offset loss, bbox, quadrilaterals, rbbox
        loc_true = tf.reshape(y_true[:, :, 0:17], [-1, 17])
        loc_pred = tf.reshape(y_pred[:, :, 0:17], [-1, 17])

        loc_loss = smooth_l1_loss(loc_true, loc_pred)
        pos_loc_loss = tf.reduce_sum(loc_loss *
                                     pos_mask_float)  # only for positives
        loc_loss = pos_loc_loss / (num_pos + eps)

        # total loss
        total_loss = self.lambda_conf * conf_loss + self.lambda_offsets * loc_loss

        # metrics
        precision, recall, accuracy, fmeasure = compute_metrics(class_true,
                                                                class_pred,
                                                                conf,
                                                                top_k=100 *
                                                                batch_size)

        def make_fcn(t):
            return lambda y_true, y_pred: t

        for name in [
                'conf_loss', 'loc_loss', 'precision', 'recall', 'accuracy',
                'fmeasure', 'num_pos', 'num_neg'
        ]:
            f = make_fcn(eval(name))
            f.__name__ = name
            self.metrics.append(f)

        return total_loss
Esempio n. 2
0
    def compute(self, y_true, y_pred):
        # y.shape (batches, priors, 4 x bbox_offset + 8 x quadrilaterals + 5 x rbbox_offsets + n x class_label)

        batch_size = tf.shape(y_true)[0]
        num_priors = tf.shape(y_true)[1]
        num_classes = tf.shape(y_true)[2] - 17
        eps = K.epsilon()

        # confidence loss
        conf_true = tf.reshape(y_true[:, :, 17:], [-1, num_classes])
        conf_pred = tf.reshape(y_pred[:, :, 17:], [-1, num_classes])

        class_true = tf.argmax(conf_true, axis=1)
        class_pred = tf.argmax(conf_pred, axis=1)
        conf = tf.reduce_max(conf_pred, axis=1)

        neg_mask_float = conf_true[:, 0]
        neg_mask = tf.cast(neg_mask_float, tf.bool)
        pos_mask = tf.logical_not(neg_mask)
        pos_mask_float = tf.cast(pos_mask, tf.float32)
        num_total = tf.cast(tf.shape(conf_true)[0], tf.float32)
        num_pos = tf.reduce_sum(pos_mask_float)
        num_neg = num_total - num_pos

        conf_loss = focal_loss(conf_true, conf_pred, alpha=[0.002, 0.998])
        conf_loss = tf.reduce_sum(conf_loss)
        conf_loss = conf_loss / (num_total + eps)

        # offset loss, bbox, quadrilaterals, rbbox
        loc_true = tf.reshape(y_true[:, :, 0:17], [-1, 17])
        loc_pred = tf.reshape(y_pred[:, :, 0:17], [-1, 17])

        loc_loss = smooth_l1_loss(loc_true, loc_pred)
        pos_loc_loss = tf.reduce_sum(loc_loss *
                                     pos_mask_float)  # only for positives
        loc_loss = pos_loc_loss / (num_pos + eps)

        # total loss
        loss = self.lambda_conf * conf_loss + self.lambda_offsets * loc_loss

        precision, recall, accuracy, fmeasure = compute_metrics(class_true,
                                                                class_pred,
                                                                conf,
                                                                top_k=100 *
                                                                batch_size)

        return eval(
            '{' +
            ' '.join(['"' + n + '": ' + n + ','
                      for n in self.metric_names]) + '}')
Esempio n. 3
0
    def compute(self, y_true, y_pred):
        # y.shape (batches, segments, 2 x segment_label + 5 x segment_offset + 16 x inter_layer_links_label + 8 x cross_layer_links_label)

        batch_size = tf.shape(y_true)[0]
        eps = K.epsilon()

        # segment confidence loss
        seg_conf_true = tf.reshape(y_true[:, :, 0:2], [-1, 2])
        seg_conf_pred = tf.reshape(y_pred[:, :, 0:2], [-1, 2])
        num_seg = tf.cast(tf.shape(seg_conf_true)[0], tf.float32)

        pos_seg_mask = seg_conf_true[:, 1]
        pos_seg_mask_float = tf.cast(pos_seg_mask, tf.float32)
        num_pos_seg = tf.reduce_sum(pos_seg_mask_float)
        num_neg_seg = num_seg - num_pos_seg

        seg_conf_loss = focal_loss(seg_conf_true, seg_conf_pred,
                                   self.gamma_segments)
        seg_conf_loss = tf.reduce_sum(seg_conf_loss)
        seg_conf_loss = seg_conf_loss / (tf.cast(num_seg, tf.float32) + eps)
        seg_conf_loss = self.lambda_segments * seg_conf_loss

        # segment offset loss
        seg_loc_true = tf.reshape(y_true[:, :, 2:7], [-1, 5])
        seg_loc_pred = tf.reshape(y_pred[:, :, 2:7], [-1, 5])

        seg_loc_loss = smooth_l1_loss(seg_loc_true, seg_loc_pred)
        pos_seg_loc_loss = tf.reduce_sum(seg_loc_loss * pos_seg_mask_float)

        seg_loc_loss = pos_seg_loc_loss / (num_pos_seg + eps)
        seg_loc_loss = self.lambda_offsets * seg_loc_loss

        # link confidence loss
        inter_link_conf_true = y_true[:, :, 7:23]
        cross_link_conf_true = y_true[:, self.first_map_offset:, 23:31]
        link_conf_true = tf.concat([
            tf.reshape(inter_link_conf_true, [-1, 2]),
            tf.reshape(cross_link_conf_true, [-1, 2])
        ], 0)
        inter_link_conf_pred = y_pred[:, :, 7:23]
        cross_link_conf_pred = y_pred[:, self.first_map_offset:, 23:31]
        link_conf_pred = tf.concat([
            tf.reshape(inter_link_conf_pred, [-1, 2]),
            tf.reshape(cross_link_conf_pred, [-1, 2])
        ], 0)
        num_link = tf.shape(link_conf_true)[0]

        link_conf_loss = focal_loss(link_conf_true, link_conf_pred,
                                    self.gamma_links)
        link_conf_loss = tf.reduce_sum(link_conf_loss)
        link_conf_loss = link_conf_loss / (tf.cast(num_link, tf.float32) + eps)
        link_conf_loss = self.lambda_links * link_conf_loss

        # total loss
        total_loss = seg_conf_loss + seg_loc_loss + link_conf_loss

        seg_conf = tf.reduce_max(seg_conf_pred, axis=1)
        seg_class_true = tf.argmax(seg_conf_true, axis=1)
        seg_class_pred = tf.argmax(seg_conf_pred, axis=1)
        seg_precision, seg_recall, seg_accuracy, seg_fmeasure = compute_metrics(
            seg_class_true, seg_class_pred, seg_conf, top_k=100 * batch_size)

        link_conf = tf.reduce_max(link_conf_pred, axis=1)
        link_class_true = tf.argmax(link_conf_true, axis=1)
        link_class_pred = tf.argmax(link_conf_pred, axis=1)
        link_precision, link_recall, link_accuracy, link_fmeasure = compute_metrics(
            link_class_true,
            link_class_pred,
            link_conf,
            top_k=100 * batch_size)

        # metrics
        def make_fcn(t):
            return lambda y_true, y_pred: t

        for name in [
                'seg_conf_loss',
                'seg_loc_loss',
                'link_conf_loss',
                'num_pos_seg',
                'num_neg_seg',
                'seg_precision',
                'seg_recall',
                'seg_accuracy',
                'seg_fmeasure',
                'link_precision',
                'link_recall',
                'link_accuracy',
                'link_fmeasure',
        ]:
            f = make_fcn(eval(name))
            f.__name__ = name
            self.metrics.append(f)

        return total_loss