【darknet源码解析-08】cost_layer.h 和 cost_layer.c 解析

本系列为darknet源码解析,本次解析src/cost_layer.h 与 src/cost_layer.c 两个。在本文中,cost主要完成多种损失函数的前向计算以及损失损失函数反向传播。

COST_TYPE定义在include/darknet.h中,是枚举类型. 可以发现darknet提供了六种损失函数.

typedef enum{
    SSE, MASKED, L1, SEG, SMOOTH,WGAN
} COST_TYPE;

cost_layer.h 的解析如下: 

#ifndef COST_LAYER_H
#define COST_LAYER_H
#include "layer.h"
#include "network.h"

typedef layer cost_layer;

// 获取定义的枚举类型的损失函数类别
COST_TYPE get_cost_type(char *s);

// 获取损失函数对应的字符串描述
char *get_cost_string(COST_TYPE a);

// 构建损失函数层
cost_layer make_cost_layer(int batch, int inputs, COST_TYPE type, float scale);

// 损失函数层的前向传播计算
void forward_cost_layer(const cost_layer l, network net);

// 损失韩式层的反向传播计算
void backward_cost_layer(const cost_layer l, network net);
void resize_cost_layer(cost_layer *l, int inputs);

#ifdef GPU
void forward_cost_layer_gpu(cost_layer l, network net);
void backward_cost_layer_gpu(const cost_layer l, network net);
#endif

#endif

cost_layer.c 的解析如下:

#include "cost_layer.h"
#include "utils.h"
#include "cuda.h"
#include "blas.h"
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

/**
 * 根据输入的损失函数名称,返回定义的枚举类型的损失函数类别
 * @param s 损失函数的名称
 * @return 损失函数类别: 枚举类型
 * 说明: 如果不匹配,默认采用 SSE
 */
COST_TYPE get_cost_type(char *s)
{
    if (strcmp(s, "seg")==0) return SEG;
    if (strcmp(s, "sse")==0) return SSE;
    if (strcmp(s, "masked")==0) return MASKED;
    if (strcmp(s, "smooth")==0) return SMOOTH;
    if (strcmp(s, "L1")==0) return L1;
    if (strcmp(s, "wgan")==0) return WGAN;
    fprintf(stderr, "Couldn't find cost type %s, going with SSE\n", s);
    return SSE;
}

/**
 * 获得定义的枚举类型的损失函数字符串描述
 * @param a 损失函数类别: 枚举类型
 * @return 返回损失函数的字符串描述
 * 说明: 如果不匹配, 默认采用SSE
 */
char *get_cost_string(COST_TYPE a)
{
    switch(a){
        case SEG:
            return "seg";
        case SSE:
            return "sse";
        case MASKED:
            return "masked";
        case SMOOTH:
            return "smooth";
        case L1:
            return "L1";
        case WGAN:
            return "wgan";
    }
    return "sse";
}

/**
 * 构建损失函数层
 * @param batch 该层输入中一个batch所含有图片的张数,等于net.batch
 * @param inputs 损失函数层每张输入图片的元素个数
 * @param cost_type 损失函数类型
 * @param scale
 * @return 损失函数层 l
 */
cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float scale)
{
    fprintf(stderr, "cost                                           %4d\n",  inputs);
    cost_layer l = {0};
    l.type = COST;

    l.scale = scale;
    l.batch = batch; //一个batch中图片张数
    l.inputs = inputs; // 损失函数层一张输入图片的元素个数
    l.outputs = inputs; // 损失函数层对应一张输入图片的输出元素个数
    l.cost_type = cost_type; // 损失函数类型
    l.delta = calloc(inputs*batch, sizeof(float)); //损失函数层的误差项(包含整个batch的)
    l.output = calloc(inputs*batch, sizeof(float)); //损失函数层所有输出 (包含整个batch的)
    l.cost = calloc(1, sizeof(float)); // 损失函数值

    // 损失函数层前向, 反向
    l.forward = forward_cost_layer;
    l.backward = backward_cost_layer;
    #ifdef GPU
    l.forward_gpu = forward_cost_layer_gpu;
    l.backward_gpu = backward_cost_layer_gpu;

    l.delta_gpu = cuda_make_array(l.output, inputs*batch);
    l.output_gpu = cuda_make_array(l.delta, inputs*batch);
    #endif
    return l;
}

void resize_cost_layer(cost_layer *l, int inputs)
{
    l->inputs = inputs;
    l->outputs = inputs;
    l->delta = realloc(l->delta, inputs*l->batch*sizeof(float));
    l->output = realloc(l->output, inputs*l->batch*sizeof(float));
#ifdef GPU
    cuda_free(l->delta_gpu);
    cuda_free(l->output_gpu);
    l->delta_gpu = cuda_make_array(l->delta, inputs*l->batch);
    l->output_gpu = cuda_make_array(l->output, inputs*l->batch);
#endif
}

// L1计算
void l1_cpu(int n, float *pred, float *truth, float *delta, float *error) {
    int i;
    for(i = 0; i < n; i ++) {
        float diff = truth[i] - pred[i];
        error[i] = fabs(diff);
        delta[i] = diff > 0 ? 1 : -1;
    }
}

// SSE, 即L2, 误差平方和,可以发现这里并没有乘以1/2. 一般往 
void l2_cpu(int n, float *pred, float *truth, float *delta, float *error) {
    int i;
    for(i = 0; i < n; i ++) {
        float diff = truth[i] - pred[i];
        error[i] = diff * diff;
        delta[i] = diff;
    }
}


/**
 * 损失函数层的前向传播函数
 * @param l 当前损失函数层
 * @param net 整个网络
 */
void forward_cost_layer(cost_layer l, network net)
{
    if (!net.truth) return; //如果
    if(l.cost_type == MASKED){ // MASKED只发现在darknet9000.cfg中使用
        int i;
        for(i = 0; i < l.batch*l.inputs; ++i){
            if(net.truth[i] == SECRET_NUM) net.input[i] = SECRET_NUM;
        }
    }
    if(l.cost_type == SMOOTH){ // 如果损失函数是 smooth l1
        smooth_l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
    }else if(l.cost_type == L1){ // 如果损失函数是 l1
        l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
    } else { // 否则
        l2_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
    }
    // 求loss总和
    l.cost[0] = sum_array(l.output, l.batch*l.inputs);
}


// Y += alpha * X
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
{
    int i;
    for(i = 0; i < N; ++i) Y[i*INCY] += ALPHA*X[i*INCX];
}
/**
 * 损失函数层的反向传播函数
 * @param l 当前损失函数层
 * @param net 整个网络
 */
void backward_cost_layer(const cost_layer l, network net)
{
    // net.data += l.scale * l.delta
    axpy_cpu(l.batch*l.inputs, l.scale, l.delta, 1, net.delta, 1);
}

#ifdef GPU

void pull_cost_layer(cost_layer l)
{
    cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
}

void push_cost_layer(cost_layer l)
{
    cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
}

int float_abs_compare (const void * a, const void * b)
{
    float fa = *(const float*) a;
    if(fa < 0) fa = -fa;
    float fb = *(const float*) b;
    if(fb < 0) fb = -fb;
    return (fa > fb) - (fa < fb);
}

void forward_cost_layer_gpu(cost_layer l, network net)
{
    if (!net.truth) return;
    if(l.smooth){
        scal_gpu(l.batch*l.inputs, (1-l.smooth), net.truth_gpu, 1);
        add_gpu(l.batch*l.inputs, l.smooth * 1./l.inputs, net.truth_gpu, 1);
    }

    if(l.cost_type == SMOOTH){
        smooth_l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    } else if (l.cost_type == L1){
        l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    } else if (l.cost_type == WGAN){
        wgan_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    } else {
        l2_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    }

    if (l.cost_type == SEG && l.noobject_scale != 1) {
        scale_mask_gpu(l.batch*l.inputs, l.delta_gpu, 0, net.truth_gpu, l.noobject_scale);
        scale_mask_gpu(l.batch*l.inputs, l.output_gpu, 0, net.truth_gpu, l.noobject_scale);
    }
    if (l.cost_type == MASKED) {
        mask_gpu(l.batch*l.inputs, net.delta_gpu, SECRET_NUM, net.truth_gpu, 0);
    }

    if(l.ratio){
        cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
        qsort(l.delta, l.batch*l.inputs, sizeof(float), float_abs_compare);
        int n = (1-l.ratio) * l.batch*l.inputs;
        float thresh = l.delta[n];
        thresh = 0;
        printf("%f\n", thresh);
        supp_gpu(l.batch*l.inputs, thresh, l.delta_gpu, 1);
    }

    if(l.thresh){
        supp_gpu(l.batch*l.inputs, l.thresh*1./l.inputs, l.delta_gpu, 1);
    }

    cuda_pull_array(l.output_gpu, l.output, l.batch*l.inputs);
    l.cost[0] = sum_array(l.output, l.batch*l.inputs);
}

void backward_cost_layer_gpu(const cost_layer l, network net)
{
    axpy_gpu(l.batch*l.inputs, l.scale, l.delta_gpu, 1, net.delta_gpu, 1);
}
#endif

cost_layer.c 源码分析如下:

#include "cost_layer.h"
#include "utils.h"
#include "cuda.h"
#include "blas.h"
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

/**
 * 根据输入的损失函数名称,返回定义的枚举类型的损失函数类别
 * @param s 损失函数的名称
 * @return 损失函数类别: 枚举类型
 * 说明: 如果不匹配,默认采用 SSE
 */
COST_TYPE get_cost_type(char *s)
{
    if (strcmp(s, "seg")==0) return SEG;
    if (strcmp(s, "sse")==0) return SSE;
    if (strcmp(s, "masked")==0) return MASKED;
    if (strcmp(s, "smooth")==0) return SMOOTH;
    if (strcmp(s, "L1")==0) return L1;
    if (strcmp(s, "wgan")==0) return WGAN;
    fprintf(stderr, "Couldn't find cost type %s, going with SSE\n", s);
    return SSE;
}

/**
 * 获得定义的枚举类型的损失函数字符串描述
 * @param a 损失函数类别: 枚举类型
 * @return 返回损失函数的字符串描述
 * 说明: 如果不匹配, 默认采用SSE
 */
char *get_cost_string(COST_TYPE a)
{
    switch(a){
        case SEG:
            return "seg";
        case SSE:
            return "sse";
        case MASKED:
            return "masked";
        case SMOOTH:
            return "smooth";
        case L1:
            return "L1";
        case WGAN: //没有CPU版本
            return "wgan";
    }
    return "sse";
}

/**
 * 构建损失函数层
 * @param batch 该层输入中一个batch所含有图片的张数,等于net.batch
 * @param inputs 损失函数层每张输入图片的元素个数
 * @param cost_type 损失函数类型
 * @param scale
 * @return 损失函数层 l
 */
cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float scale)
{
    fprintf(stderr, "cost                                           %4d\n",  inputs);
    cost_layer l = {0};
    l.type = COST;

    l.scale = scale; // 用于误差反传的时候
    l.batch = batch; // 一个batch中图片张数
    l.inputs = inputs; // 损失函数层一张输入图片的元素个数
    l.outputs = inputs; // 损失函数层对应一张输入图片的输出元素个数
    l.cost_type = cost_type; // 损失函数类型
    l.delta = calloc(inputs*batch, sizeof(float)); //损失函数层的误差项(包含整个batch的)
    l.output = calloc(inputs*batch, sizeof(float)); //损失函数层所有输出 (包含整个batch的)
    l.cost = calloc(1, sizeof(float)); // 损失函数值

    // 损失函数层前向, 反向
    l.forward = forward_cost_layer;
    l.backward = backward_cost_layer;
    #ifdef GPU
    l.forward_gpu = forward_cost_layer_gpu;
    l.backward_gpu = backward_cost_layer_gpu;

    l.delta_gpu = cuda_make_array(l.output, inputs*batch);
    l.output_gpu = cuda_make_array(l.delta, inputs*batch);
    #endif
    return l;
}

void resize_cost_layer(cost_layer *l, int inputs)
{
    l->inputs = inputs;
    l->outputs = inputs;
    l->delta = realloc(l->delta, inputs*l->batch*sizeof(float));
    l->output = realloc(l->output, inputs*l->batch*sizeof(float));
#ifdef GPU
    cuda_free(l->delta_gpu);
    cuda_free(l->output_gpu);
    l->delta_gpu = cuda_make_array(l->delta, inputs*l->batch);
    l->output_gpu = cuda_make_array(l->output, inputs*l->batch);
#endif
}

// L1计算
// L1(x) = |x|
// dL1(x) = 1 if x > 0
//        = -1 otherwise
// L1对x的导数为常数.这就导致训练后期,预测值与GT的差异很小时,L1损失对预测值的导数的绝对值仍然为1, 
// 而learning rate如果不变,损失函数将在稳定值附近波动,难以继续收敛以达到更高精度.
void l1_cpu(int n, float *pred, float *truth, float *delta, float *error) {
    int i;
    for(i = 0; i < n; i ++) {
        float diff = truth[i] - pred[i];
        error[i] = fabs(diff);
        delta[i] = diff > 0 ? 1 : -1;
    }
}

// SSE, 即L2, 误差平方和,可以发现这里并没有乘以1/2. 一般往
//  L2(x) = x**2
// dL2(x) /dx = 2x 当x增大时,L2损失对x的导数也增大,这就导致训练初期,预测值与GT差异过大,损失对预测值的梯度十分大,训练不稳定
void l2_cpu(int n, float *pred, float *truth, float *delta, float *error) {
    int i;
    for(i = 0; i < n; i ++) {
        float diff = truth[i] - pred[i];
        error[i] = diff * diff;
        delta[i] = diff;
    }
}

// smooth_l1_loss(x) = 0.5* x**2 / beta |x| < 1,
//                   = |x| - 0.5 * beta otherwise. 
// d(smooth_l1_loss(x))/d(x) = x if |x| < 1
//                           = +/- 1 otherwise.
// smooth_l1 在x较小的时候,对x的梯度也会变小,而x很大时,对x的梯度的绝对值达到1,
// 也不会太大以至于破坏网络, smooth_l1 完美的避开了L1和L2损失函数的缺陷.
// 主流smooth方式,会设定beta = 9. 这里不做过多探讨.
void smooth_l1_cpu(int n, float *pred, float *truth, float *delta, float *error)
{
    int i;
    for(i = 0; i < n; ++i){
        float diff = truth[i] - pred[i];
        float abs_val = fabs(diff);
        if(abs_val < 1) {
            error[i] = diff * diff;
            delta[i] = diff;
        }
        else {
            error[i] = 2*abs_val - 1;
            delta[i] = (diff < 0) ? 1 : -1;
        }
    }
}


/**
 * 损失函数层的前向传播函数
 * @param l 当前损失函数层
 * @param net 整个网络
 */
void forward_cost_layer(cost_layer l, network net)
{
    if (!net.truth) return; //如果
    if(l.cost_type == MASKED){ // MASKED只发现在darknet9000.cfg中使用
        int i;  // #define SECRET_NUM -1234 定义在include/darknet.h中
        for(i = 0; i < l.batch*l.inputs; ++i){
            if(net.truth[i] == SECRET_NUM) net.input[i] = SECRET_NUM;
        }
    }
    if(l.cost_type == SMOOTH){ // 如果损失函数是 smooth l1
        smooth_l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
    }else if(l.cost_type == L1){ // 如果损失函数是 l1
        l1_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
    } else { // 否则
        l2_cpu(l.batch*l.inputs, net.input, net.truth, l.delta, l.output);
    }
    // 求loss总和
    l.cost[0] = sum_array(l.output, l.batch*l.inputs);
}


// Y += alpha * X
void axpy_cpu(int N, float ALPHA, float *X, int INCX, float *Y, int INCY)
{
    int i;
    for(i = 0; i < N; ++i) Y[i*INCY] += ALPHA*X[i*INCX];
}
/**
 * 损失函数层的反向传播函数
 * @param l 当前损失函数层
 * @param net 整个网络
 */
void backward_cost_layer(const cost_layer l, network net)
{
    // net.data += l.scale * l.delta
    axpy_cpu(l.batch*l.inputs, l.scale, l.delta, 1, net.delta, 1);
}

#ifdef GPU

void pull_cost_layer(cost_layer l)
{
    cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
}

void push_cost_layer(cost_layer l)
{
    cuda_push_array(l.delta_gpu, l.delta, l.batch*l.inputs);
}

int float_abs_compare (const void * a, const void * b)
{
    float fa = *(const float*) a;
    if(fa < 0) fa = -fa;
    float fb = *(const float*) b;
    if(fb < 0) fb = -fb;
    return (fa > fb) - (fa < fb);
}

void forward_cost_layer_gpu(cost_layer l, network net)
{
    if (!net.truth) return;
    if(l.smooth){
        scal_gpu(l.batch*l.inputs, (1-l.smooth), net.truth_gpu, 1);
        add_gpu(l.batch*l.inputs, l.smooth * 1./l.inputs, net.truth_gpu, 1);
    }

    if(l.cost_type == SMOOTH){
        smooth_l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    } else if (l.cost_type == L1){
        l1_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    } else if (l.cost_type == WGAN){
        wgan_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    } else {
        l2_gpu(l.batch*l.inputs, net.input_gpu, net.truth_gpu, l.delta_gpu, l.output_gpu);
    }

    if (l.cost_type == SEG && l.noobject_scale != 1) {
        scale_mask_gpu(l.batch*l.inputs, l.delta_gpu, 0, net.truth_gpu, l.noobject_scale);
        scale_mask_gpu(l.batch*l.inputs, l.output_gpu, 0, net.truth_gpu, l.noobject_scale);
    }
    if (l.cost_type == MASKED) {
        mask_gpu(l.batch*l.inputs, net.delta_gpu, SECRET_NUM, net.truth_gpu, 0);
    }

    if(l.ratio){
        cuda_pull_array(l.delta_gpu, l.delta, l.batch*l.inputs);
        qsort(l.delta, l.batch*l.inputs, sizeof(float), float_abs_compare);
        int n = (1-l.ratio) * l.batch*l.inputs;
        float thresh = l.delta[n];
        thresh = 0;
        printf("%f\n", thresh);
        supp_gpu(l.batch*l.inputs, thresh, l.delta_gpu, 1);
    }

    if(l.thresh){
        supp_gpu(l.batch*l.inputs, l.thresh*1./l.inputs, l.delta_gpu, 1);
    }

    cuda_pull_array(l.output_gpu, l.output, l.batch*l.inputs);
    l.cost[0] = sum_array(l.output, l.batch*l.inputs);
}

void backward_cost_layer_gpu(const cost_layer l, network net)
{
    axpy_gpu(l.batch*l.inputs, l.scale, l.delta_gpu, 1, net.delta_gpu, 1);
}
#endif

完,

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值