darknet/src/cost_layer.c

88 lines
2.4 KiB
C
Raw Normal View History

2014-10-13 11:29:01 +04:00
#include "cost_layer.h"
2014-11-19 00:51:04 +03:00
#include "utils.h"
2015-01-23 03:38:24 +03:00
#include "cuda.h"
#include "blas.h"
2014-10-13 11:29:01 +04:00
#include <math.h>
2014-11-28 21:38:26 +03:00
#include <string.h>
2014-10-13 11:29:01 +04:00
#include <stdlib.h>
#include <stdio.h>
2014-11-28 21:38:26 +03:00
COST_TYPE get_cost_type(char *s)
{
if (strcmp(s, "sse")==0) return SSE;
if (strcmp(s, "detection")==0) return DETECTION;
fprintf(stderr, "Couldn't find activation function %s, going with SSE\n", s);
return SSE;
}
char *get_cost_string(COST_TYPE a)
{
switch(a){
case SSE:
return "sse";
case DETECTION:
return "detection";
}
return "sse";
}
cost_layer *make_cost_layer(int batch, int inputs, COST_TYPE type)
2014-10-13 11:29:01 +04:00
{
fprintf(stderr, "Cost Layer: %d inputs\n", inputs);
cost_layer *layer = calloc(1, sizeof(cost_layer));
layer->batch = batch;
layer->inputs = inputs;
2014-11-28 21:38:26 +03:00
layer->type = type;
2014-10-13 11:29:01 +04:00
layer->delta = calloc(inputs*batch, sizeof(float));
layer->output = calloc(1, sizeof(float));
#ifdef GPU
2015-01-23 03:38:24 +03:00
layer->delta_gpu = cuda_make_array(layer->delta, inputs*batch);
2014-10-13 11:29:01 +04:00
#endif
return layer;
}
void forward_cost_layer(cost_layer layer, float *input, float *truth)
{
if (!truth) return;
copy_cpu(layer.batch*layer.inputs, truth, 1, layer.delta, 1);
axpy_cpu(layer.batch*layer.inputs, -1, input, 1, layer.delta, 1);
2014-11-28 21:38:26 +03:00
if(layer.type == DETECTION){
int i;
for(i = 0; i < layer.batch*layer.inputs; ++i){
2015-02-24 05:52:05 +03:00
if((i%25) && !truth[(i/25)*25]) layer.delta[i] = 0;
2014-11-28 21:38:26 +03:00
}
}
2014-10-13 11:29:01 +04:00
*(layer.output) = dot_cpu(layer.batch*layer.inputs, layer.delta, 1, layer.delta, 1);
2014-12-04 10:20:29 +03:00
//printf("cost: %f\n", *layer.output);
2014-10-13 11:29:01 +04:00
}
void backward_cost_layer(const cost_layer layer, float *input, float *delta)
{
copy_cpu(layer.batch*layer.inputs, layer.delta, 1, delta, 1);
}
#ifdef GPU
2014-11-28 21:38:26 +03:00
2015-01-23 03:38:24 +03:00
void forward_cost_layer_gpu(cost_layer layer, float * input, float * truth)
2014-10-13 11:29:01 +04:00
{
if (!truth) return;
2014-11-06 01:49:58 +03:00
2015-01-23 03:38:24 +03:00
copy_ongpu(layer.batch*layer.inputs, truth, 1, layer.delta_gpu, 1);
axpy_ongpu(layer.batch*layer.inputs, -1, input, 1, layer.delta_gpu, 1);
2014-11-19 00:51:04 +03:00
2014-11-28 21:38:26 +03:00
if(layer.type==DETECTION){
2015-02-24 05:52:05 +03:00
mask_ongpu(layer.inputs*layer.batch, layer.delta_gpu, truth, 25);
2014-11-28 21:38:26 +03:00
}
2015-01-23 03:38:24 +03:00
cuda_pull_array(layer.delta_gpu, layer.delta, layer.batch*layer.inputs);
2014-10-13 11:29:01 +04:00
*(layer.output) = dot_cpu(layer.batch*layer.inputs, layer.delta, 1, layer.delta, 1);
2014-12-04 10:20:29 +03:00
//printf("cost: %f\n", *layer.output);
2014-10-13 11:29:01 +04:00
}
2015-01-23 03:38:24 +03:00
void backward_cost_layer_gpu(const cost_layer layer, float * input, float * delta)
2014-10-13 11:29:01 +04:00
{
2015-01-23 03:38:24 +03:00
copy_ongpu(layer.batch*layer.inputs, layer.delta_gpu, 1, delta, 1);
2014-10-13 11:29:01 +04:00
}
#endif