参考文献依然是放前面:https://blog.csdn.net/caicaiatnbu/category_9096319.html
darknet版本: https://github.com/AlexeyAB/darknet,与原始的版本还是有一点区别的。
因为第一次读源码,我就直接按照参考文献的顺序来了,到时候再查漏补缺,加油!
今天看的是:cost_layer,主要完成损失函数的前向计算以及损失函数的求导,损失函数的误差反向传播。
直接放代码注解:比较简单
#include "cost_layer.h"
#include "utils.h"
#include "dark_cuda.h"
#include "blas.h"
#include
#include
#include
#include
/**
* 根据输入的损失函数名称,返回定义的枚举类型的损失函数类别
* @param s 损失函数的名称
* @return 损失函数类别: 枚举类型
* 说明: 如果不匹配,默认采用 SSE
*/
COST_TYPE get_cost_type(char *s)
{
if (strcmp(s, "sse")==0) return SSE;
if (strcmp(s, "masked")==0) return MASKED;
if (strcmp(s, "smooth")==0) return SMOOTH;
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 SSE:
return "sse";
case MASKED:
return "masked";
case SMOOTH:
return "smooth";
default:
return "sse";
}
}
/**
* 构建损失函数层
* @param batch 该层输入中一个batch所含有图片的张数,等于net.batch
* @param inputs 损失函数层每张输入图片的元素个数
* @param cost_type 损失函数类型
* @param scale
* @return 损失函数层 l
*/
/*parser.c文件中有一个关于cost layer的使用
可以看到scale和type都是外面传进来的参数
cost_layer parse_cost(list *options, size_params params)
{
char *type_s = option_find_str(options, "type", "sse");
COST_TYPE type = get_cost_type(type_s); //h获得cost layer对应的类型
float scale = option_find_float_quiet(options, "scale",1);
cost_layer layer = make_cost_layer(params.batch, params.inputs, type, scale);
layer.ratio = option_find_float_quiet(options, "ratio",0);
return layer;
}
*/
cost_layer make_cost_layer(int batch, int inputs, COST_TYPE cost_type, float scale)
{
fprintf(stderr, "cost %4d\n", inputs);
cost_layer l = { (LAYER_TYPE)0 };//初定义一个layer
l.type = COST;//给定layer的类型,为损失层
l.scale = scale;
l.batch = batch;//batch数
l.inputs = inputs;//该层的输入参数大小
l.outputs = inputs;//该层的输出参数大小
l.cost_type = cost_type;//损失函数类型
l.delta = (float*)xcalloc(inputs * batch, sizeof(float));//构建layer的误差项空间
l.output = (float*)xcalloc(inputs * batch, sizeof(float));//构建layer输出的特征空间
l.cost = (float*)xcalloc(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.delta, inputs*batch);
l.output_gpu = cuda_make_array(l.output, inputs*batch);
#endif
return l;
}
//重构cost layer中要用到的存储空间
void resize_cost_layer(cost_layer *l, int inputs)
{
l->inputs = inputs;
l->outputs = inputs;
l->delta = (float*)xrealloc(l->delta, inputs * l->batch * sizeof(float));//util.c定义的xrealloc,重新分配空间
l->output = (float*)xrealloc(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
}
/**
* 损失函数层的前向传播函数
* @param l 当前损失函数层
* @param net 整个网络
*/
void forward_cost_layer(cost_layer l, network_state state)
{
if (!state.truth) return;
if(l.cost_type == MASKED){// MASKED只发现在darknet9000.cfg中使用
int i;
for(i = 0; i < l.batch*l.inputs; ++i){
if(state.truth[i] == SECRET_NUM) state.input[i] = SECRET_NUM;
}
}
if(l.cost_type == SMOOTH){// 如果损失函数是 smooth l1,调用smooth_l1_cpu
smooth_l1_cpu(l.batch*l.inputs, state.input, state.truth, l.delta, l.output);
/*
在blas.c中:
明确:smooth(x) = 0.5* x^2 / beta |x| < 1,
= |x| - 0.5 * beta otherwise.
梯度:d(smooth_l1_loss(x))/d(x) = x |x| < 1
= +/- 1 otherwise.
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);//|x|
if(abs_val < 1) { //做判断
error[i] = diff * diff;
delta[i] = diff;
}
else {
error[i] = 2*abs_val - 1;
delta[i] = (diff > 0) ? 1 : -1;
}
}
}
*/
} else {//其它,调用l2_cpu
l2_cpu(l.batch*l.inputs, state.input, state.truth, l.delta, l.output);
/*
在blas.c中:
SSE, 即L2, 误差平方和,可以发现这里并没有乘以1/2.
明确:L2(x) = x**2
梯度:dL2(x) /dx = 2x
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];//x
error[i] = diff * diff;
delta[i] = diff;
}
}
*/
}
// 求loss总和,求cost layer的输出总和
l.cost[0] = sum_array(l.output, l.batch*l.inputs);
/*
在util.c中
float sum_array(float *a, int n)
{
int i;
float sum = 0;
for(i = 0; i < n; ++i) sum += a[i];
return sum;
}
*/
}
/**
* 损失函数层的反向传播函数
* @param l 当前损失函数层
* @param net 整个网络
*/
void backward_cost_layer(const cost_layer l, network_state state)
{
// net.data += l.scale * l.delta
//这里我没太懂,等我往后看
axpy_cpu(l.batch*l.inputs, l.scale, l.delta, 1, state.delta, 1);
/*
在blas.c中: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];
}
*/
}
#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_state state)
{
if (!state.truth) return;
if (l.cost_type == MASKED) {
mask_ongpu(l.batch*l.inputs, state.input, SECRET_NUM, state.truth);
}
if(l.cost_type == SMOOTH){
smooth_l1_gpu(l.batch*l.inputs, state.input, state.truth, l.delta_gpu, l.output_gpu);
} else {
l2_gpu(l.batch*l.inputs, state.input, state.truth, l.delta_gpu, l.output_gpu);
}
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_ongpu(l.batch*l.inputs, thresh, 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_state state)
{
axpy_ongpu(l.batch*l.inputs, l.scale, l.delta_gpu, 1, state.delta, 1);
}
#endif