Exemplo n.º 1
0
    def run(self, y_true, y_pred, anchors=[90, 95, 92, 154, 139, 281]):

        # 1. setup
        y_pred = tf.reshape(y_pred, y_true.shape)
        object_mask = tf.expand_dims(y_true[..., 4], 4)

        # 2. Adjust prediction (bxy, twh)
        preds = adjust_pred_tensor(y_pred)

        # 3. Adjust ground truth (bxy, twh)
        trues = adjust_true_tensor(y_true)

        # 4. conf_delta tensor
        conf_delta = conf_delta_tensor(y_true, preds, anchors,
                                       self.ignore_thresh)

        # 5. loss tensor
        wh_scale = wh_scale_tensor(trues[..., 2:4], anchors, self.image_size)

        loss_box = loss_coord_tensor(object_mask, preds[..., :4],
                                     trues[..., :4], wh_scale, self.xywh_scale)
        loss_conf = loss_conf_tensor(object_mask, preds[..., 4], trues[..., 4],
                                     self.obj_scale, self.noobj_scale,
                                     conf_delta)
        loss_class = loss_class_tensor(object_mask, preds[..., 5:],
                                       trues[..., 5], self.class_scale)
        loss = loss_box + loss_conf + loss_class
        return loss * self.grid_scale
Exemplo n.º 2
0
    def run_loss_component(self, y_true, y_pred, anchors=[66,303, 81,318, 104,337]):
        # 1. setup
        y_pred = tf.reshape(y_pred, y_true.shape)
        # print(y_true[...,4].shape) # (1, grid, grid, 3, 15)
        object_mask = tf.expand_dims(y_true[..., 4], 4) # contain 0, 1 value, indicates which bbox has object in it
        # print(object_mask.shape) # (1, grid, grid, 3, 1)

        # 2. Adjust prediction (bxy, twh)
        preds = adjust_pred_tensor(y_pred)

        # 3. Adjust ground truth (bxy, twh)
        trues = adjust_true_tensor(y_true)

        # 4. conf_delta tensor
        conf_delta = conf_delta_tensor(y_true, preds, anchors, self.ignore_thresh)

        # 5. loss tensor
        wh_scale =  wh_scale_tensor(trues[..., 2:4], anchors, self.image_size)
        
        loss_box = loss_coord_tensor(object_mask, preds[..., :4], trues[..., :4], wh_scale, self.xywh_scale)
        loss_conf = loss_conf_tensor(object_mask, preds[..., 4], trues[..., 4], self.obj_scale, self.noobj_scale, conf_delta)
        loss_class = loss_class_tensor(object_mask, preds[..., 5:], trues[..., 5], self.class_scale)
        loss = loss_box + loss_conf + loss_class
        return loss * self.grid_scale, \
               [loss_box * self.grid_scale, loss_conf * self.grid_scale, loss_class * self.grid_scale]