【问题标题】:What is the loss function of YOLOv3YOLOv3的损失函数是什么
【发布时间】:2019-08-19 01:59:13
【问题描述】:

我打算编写自己的 YOLOv3 实现,并提出损失函数的一些问题。原始论文提到他在类预测部分使用二元交叉熵,这就是我所做的。

我尝试通过原始暗网代码读取一些代码,但没有找到与 BCE 丢失相关的任何内容。我进一步阅读了一些使用 Keras、Pytorch 和 TensorFlow 的方法。每个人似乎对损失函数都有自己的看法。有些只使用 MSE 进行宽度和高度估计,其余使用 BCE,有些使用 x,y,w,h 使用 MSE,其余使用 BCE。

这是我的一些代码:

loss_x = self.mse_loss(x[mask], tx[mask])
loss_y = self.mse_loss(y[mask], ty[mask])
loss_w = self.mse_loss(w[mask], tw[mask])
loss_h = self.mse_loss(h[mask], th[mask])
loss_conf = self.bce_loss(pred_conf[conf_mask_false], tconf[conf_mask_false]) + self.bce_loss(pred_conf[conf_mask_true],tconf[conf_mask_true])
loss_cls = (1 / nB) * self.ce_loss(pred_cls[mask],torch.argmax(tcls[mask], 1))
loss = loss_x + loss_y + loss_w + loss_h + loss_conf + loss_cls

由于损失函数在训练中起着重要作用。我希望有人能帮我弄清楚。

【问题讨论】:

    标签: machine-learning deep-learning computer-vision object-detection yolo


    【解决方案1】:

    Yolo v3的损失函数,看src/yolo_layer.c

    盒子的增量,第 93 行

    float delta_yolo_box(box truth, float *x, float *biases, int n, int index, int i, int j, int lw, int lh, int w, int h, float *delta, float scale, int stride)
    {
        box pred = get_yolo_box(x, biases, n, index, i, j, lw, lh, w, h, stride);
        float iou = box_iou(pred, truth);
    
        float tx = (truth.x*lw - i);
        float ty = (truth.y*lh - j);
        float tw = log(truth.w*w / biases[2*n]);
        float th = log(truth.h*h / biases[2*n + 1]);
    
        delta[index + 0*stride] = scale * (tx - x[index + 0*stride]);
        delta[index + 1*stride] = scale * (ty - x[index + 1*stride]);
        delta[index + 2*stride] = scale * (tw - x[index + 2*stride]);
        delta[index + 3*stride] = scale * (th - x[index + 3*stride]);
        return iou;
    }
    

    类的增量,第 111 行

    void delta_yolo_class(float *output, float *delta, int index, int class, int classes, int stride, float *avg_cat)
    {
        int n;
        if (delta[index]){
            delta[index + stride*class] = 1 - output[index + stride*class];
            if(avg_cat) *avg_cat += output[index + stride*class];
            return;
        }
        for(n = 0; n < classes; ++n){
            delta[index + stride*n] = ((n == class)?1 : 0) - output[index + stride*n];
            if(n == class && avg_cat) *avg_cat += output[index + stride*n];
        }
    }
    

    对象性增量,第 178 行

    l.delta[obj_index] = 0 - l.output[obj_index];
                        if (best_iou > l.ignore_thresh) {
                            l.delta[obj_index] = 0;
    

    l.delta[obj_index] = 1 - l.output[obj_index];
    

    损失 = 平方和

    *(l.cost) = pow(mag_array(l.delta, l.outputs * l.batch), 2);
    

    无论如何,我只是让您了解一下 Yolo V3 中的损失函数。有关详细说明,您应该关注这个 github 讨论:
    https://github.com/AlexeyAB/darknet/issues/1695#issuecomment-426016524

    https://github.com/AlexeyAB/darknet/issues/1845#issuecomment-434079752

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2020-04-30
      • 2021-10-23
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多