コード例 #1
0
 def compute(self, y_true, y_pred):
     # y.shape (batches, priors, 4 x bbox_offset + 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] - 4
     eps = K.epsilon()
     
     # confidence loss
     conf_true = tf.reshape(y_true[:,:,4:], [-1, num_classes])
     conf_pred = tf.reshape(y_pred[:,:,4:], [-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=self.class_weights)
     conf_loss = tf.reduce_sum(conf_loss)
     
     conf_loss = conf_loss / (num_total + eps)
     
     # offset loss
     loc_true = tf.reshape(y_true[:,:,0:4], [-1, 4])
     loc_pred = tf.reshape(y_pred[:,:,0:4], [-1, 4])
     
     loc_loss = smooth_l1_loss(loc_true, loc_pred)
     pos_loc_loss = tf.reduce_sum(loc_loss * pos_mask_float) # only for positive ground truth
     
     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', 
                 ]:
         f = make_fcn(eval(name))
         f.__name__ = name
         self.metrics.append(f)
     
     return total_loss
コード例 #2
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])

        pos_seg_mask = seg_conf_true[:, 1]
        pos_seg_mask_float = tf.cast(pos_seg_mask, tf.float32)

        num_seg = tf.cast(tf.shape(seg_conf_true)[0], 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)
        pos_seg_loc_loss = pos_seg_loc_loss / (num_pos_seg + eps)
        seg_loc_loss = self.lambda_offsets * pos_seg_loc_loss

        # link confidence loss
        inter_link_conf_true = tf.reshape(y_true[:, :, 7:23], [-1, 2])
        inter_link_conf_pred = tf.reshape(y_pred[:, :, 7:23], [-1, 2])
        inter_link_conf_loss = focal_loss(inter_link_conf_true,
                                          inter_link_conf_pred,
                                          self.gamma_links)

        inter_link_conf_loss = tf.reduce_sum(inter_link_conf_loss)

        cross_link_conf_true = tf.reshape(
            y_true[:, self.first_map_offset:, 23:31], [-1, 2])
        cross_link_conf_pred = tf.reshape(
            y_pred[:, self.first_map_offset:, 23:31], [-1, 2])
        cross_link_conf_loss = focal_loss(cross_link_conf_true,
                                          cross_link_conf_pred,
                                          self.gamma_links)

        cross_link_conf_loss = tf.reduce_sum(cross_link_conf_loss)

        link_conf_loss = inter_link_conf_loss + cross_link_conf_loss
        num_link = tf.shape(inter_link_conf_true)[0] + tf.shape(
            cross_link_conf_true)[0]

        inter_link_conf_loss = inter_link_conf_loss / tf.cast(
            tf.shape(inter_link_conf_true)[0], tf.float32)
        cross_link_conf_loss = cross_link_conf_loss / tf.cast(
            tf.shape(cross_link_conf_true)[0], tf.float32)

        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)

        inter_link_conf = tf.reduce_max(inter_link_conf_pred, axis=1)
        inter_link_class_true = tf.argmax(inter_link_conf_true, axis=1)
        inter_link_class_pred = tf.argmax(inter_link_conf_pred, axis=1)
        inter_link_precision, inter_link_recall, inter_link_accuracy, inter_link_fmeasure = compute_metrics(
            inter_link_class_true,
            inter_link_class_pred,
            inter_link_conf,
            top_k=100 * batch_size)

        cross_link_conf = tf.reduce_max(cross_link_conf_pred, axis=1)
        cross_link_class_true = tf.argmax(cross_link_conf_true, axis=1)
        cross_link_class_pred = tf.argmax(cross_link_conf_pred, axis=1)
        cross_link_precision, cross_link_recall, cross_link_accuracy, cross_link_fmeasure = compute_metrics(
            cross_link_class_true,
            cross_link_class_pred,
            cross_link_conf,
            top_k=100 * batch_size)

        link_precision, link_recall, link_accuracy, link_fmeasure = compute_metrics(
            tf.concat([inter_link_class_true, cross_link_class_true], 0),
            tf.concat([inter_link_class_pred, cross_link_class_pred], 0),
            tf.concat([inter_link_conf, cross_link_conf], 0),
            top_k=100 * batch_size)

        seg_loc_loss = seg_loc_loss / (num_pos_seg + eps) * num_seg

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

        for name in [
                'seg_conf_loss',
                'seg_loc_loss',
                'link_conf_loss',
                'inter_link_conf_loss',
                'cross_link_conf_loss',
                'inter_link_precision',
                'inter_link_recall',
                'inter_link_accuracy',
                'inter_link_fmeasure',
                'cross_link_precision',
                'cross_link_recall',
                'cross_link_accuracy',
                'cross_link_fmeasure',
                '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
コード例 #3
0
ファイル: sl_training.py プロジェクト: weian312/ssd_detectors
    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)

        if self.reduced_focal_loss:
            from utils.training import reduced_focal_loss as focal_loss
        else:
            from utils.training import focal_loss

        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])

        pos_seg_mask = seg_conf_true[:, 1]
        pos_seg_mask_float = tf.cast(pos_seg_mask, tf.float32)

        num_seg = tf.cast(tf.shape(seg_conf_true)[0], 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)

        # 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)
        pos_seg_loc_loss = pos_seg_loc_loss / (num_pos_seg + eps)

        # link confidence loss
        inter_link_conf_true = tf.reshape(y_true[:, :, 7:23], [-1, 2])
        inter_link_conf_pred = tf.reshape(y_pred[:, :, 7:23], [-1, 2])
        inter_link_conf_loss = focal_loss(inter_link_conf_true,
                                          inter_link_conf_pred,
                                          self.gamma_links)
        inter_link_conf_loss = tf.reduce_sum(inter_link_conf_loss)
        num_inter_links = tf.cast(
            tf.shape(inter_link_conf_true)[0], tf.float32)
        inter_link_conf_loss = inter_link_conf_loss / (num_inter_links + eps)

        cross_link_conf_true = tf.reshape(
            y_true[:, self.first_map_offset:, 23:31], [-1, 2])
        cross_link_conf_pred = tf.reshape(
            y_pred[:, self.first_map_offset:, 23:31], [-1, 2])
        cross_link_conf_loss = focal_loss(cross_link_conf_true,
                                          cross_link_conf_pred,
                                          self.gamma_links)
        cross_link_conf_loss = tf.reduce_sum(cross_link_conf_loss)
        num_cross_links = tf.cast(
            tf.shape(cross_link_conf_true)[0], tf.float32)
        cross_link_conf_loss = cross_link_conf_loss / (num_cross_links + eps)

        link_conf_loss = inter_link_conf_loss + cross_link_conf_loss

        # total loss
        loss = self.lambda_segments * seg_conf_loss + self.lambda_offsets * seg_loc_loss + self.lambda_links * 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)

        inter_link_conf = tf.reduce_max(inter_link_conf_pred, axis=1)
        inter_link_class_true = tf.argmax(inter_link_conf_true, axis=1)
        inter_link_class_pred = tf.argmax(inter_link_conf_pred, axis=1)
        inter_link_precision, inter_link_recall, inter_link_accuracy, inter_link_fmeasure = compute_metrics(
            inter_link_class_true,
            inter_link_class_pred,
            inter_link_conf,
            top_k=100 * batch_size)

        cross_link_conf = tf.reduce_max(cross_link_conf_pred, axis=1)
        cross_link_class_true = tf.argmax(cross_link_conf_true, axis=1)
        cross_link_class_pred = tf.argmax(cross_link_conf_pred, axis=1)
        cross_link_precision, cross_link_recall, cross_link_accuracy, cross_link_fmeasure = compute_metrics(
            cross_link_class_true,
            cross_link_class_pred,
            cross_link_conf,
            top_k=100 * batch_size)

        link_precision, link_recall, link_accuracy, link_fmeasure = compute_metrics(
            tf.concat([inter_link_class_true, cross_link_class_true], 0),
            tf.concat([inter_link_class_pred, cross_link_class_pred], 0),
            tf.concat([inter_link_conf, cross_link_conf], 0),
            top_k=100 * batch_size)

        #seg_loc_loss = seg_loc_loss / (num_pos_seg + eps) * num_seg

        return eval(
            '{' +
            ' '.join(['"' + n + '": ' + n + ','
                      for n in self.metric_names]) + '}')