darknet/src/network_kernels.cu

245 lines
8.0 KiB
Plaintext
Raw Normal View History

2015-01-23 03:38:24 +03:00
extern "C" {
2014-11-06 01:49:58 +03:00
#include <stdio.h>
#include <time.h>
#include "network.h"
#include "image.h"
#include "data.h"
#include "utils.h"
#include "crop_layer.h"
#include "connected_layer.h"
#include "convolutional_layer.h"
#include "maxpool_layer.h"
#include "cost_layer.h"
#include "normalization_layer.h"
#include "freeweight_layer.h"
#include "softmax_layer.h"
#include "dropout_layer.h"
2015-01-23 03:38:24 +03:00
}
2014-11-06 01:49:58 +03:00
2015-01-23 03:38:24 +03:00
extern "C" float * get_network_output_gpu_layer(network net, int i);
extern "C" float * get_network_delta_gpu_layer(network net, int i);
2014-11-06 01:49:58 +03:00
2015-01-23 03:38:24 +03:00
void forward_network_gpu(network net, float * input, float * truth, int train)
2014-11-06 01:49:58 +03:00
{
int i;
for(i = 0; i < net.n; ++i){
2015-01-14 23:18:57 +03:00
//clock_t time = clock();
2014-11-06 01:49:58 +03:00
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
forward_convolutional_layer_gpu(layer, input);
2015-01-23 03:38:24 +03:00
input = layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == COST){
cost_layer layer = *(cost_layer *)net.layers[i];
forward_cost_layer_gpu(layer, input, truth);
}
else if(net.types[i] == CONNECTED){
connected_layer layer = *(connected_layer *)net.layers[i];
forward_connected_layer_gpu(layer, input);
2015-01-23 03:38:24 +03:00
input = layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == MAXPOOL){
maxpool_layer layer = *(maxpool_layer *)net.layers[i];
forward_maxpool_layer_gpu(layer, input);
2015-01-23 03:38:24 +03:00
input = layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == SOFTMAX){
softmax_layer layer = *(softmax_layer *)net.layers[i];
forward_softmax_layer_gpu(layer, input);
2015-01-23 03:38:24 +03:00
input = layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
2014-11-19 00:51:04 +03:00
else if(net.types[i] == DROPOUT){
if(!train) continue;
dropout_layer layer = *(dropout_layer *)net.layers[i];
forward_dropout_layer_gpu(layer, input);
2015-01-23 03:38:24 +03:00
input = layer.output_gpu;
2014-11-19 00:51:04 +03:00
}
2014-12-16 22:40:05 +03:00
else if(net.types[i] == CROP){
crop_layer layer = *(crop_layer *)net.layers[i];
2015-01-31 09:05:23 +03:00
forward_crop_layer_gpu(layer, train, input);
2015-01-23 03:38:24 +03:00
input = layer.output_gpu;
2014-12-16 22:40:05 +03:00
}
2015-01-14 23:18:57 +03:00
//printf("Forward %d %s %f\n", i, get_layer_string(net.types[i]), sec(clock() - time));
2014-11-06 01:49:58 +03:00
}
}
2015-01-23 03:38:24 +03:00
void backward_network_gpu(network net, float * input)
2014-11-06 01:49:58 +03:00
{
int i;
2015-01-23 03:38:24 +03:00
float * prev_input;
float * prev_delta;
2014-11-06 01:49:58 +03:00
for(i = net.n-1; i >= 0; --i){
2015-01-14 23:18:57 +03:00
//clock_t time = clock();
2014-11-06 01:49:58 +03:00
if(i == 0){
prev_input = input;
prev_delta = 0;
}else{
2015-01-23 03:38:24 +03:00
prev_input = get_network_output_gpu_layer(net, i-1);
prev_delta = get_network_delta_gpu_layer(net, i-1);
2014-11-06 01:49:58 +03:00
}
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
2014-12-04 10:20:29 +03:00
backward_convolutional_layer_gpu(layer, prev_input, prev_delta);
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == COST){
cost_layer layer = *(cost_layer *)net.layers[i];
backward_cost_layer_gpu(layer, prev_input, prev_delta);
}
else if(net.types[i] == CONNECTED){
connected_layer layer = *(connected_layer *)net.layers[i];
backward_connected_layer_gpu(layer, prev_input, prev_delta);
}
else if(net.types[i] == MAXPOOL){
maxpool_layer layer = *(maxpool_layer *)net.layers[i];
backward_maxpool_layer_gpu(layer, prev_delta);
}
2014-12-13 23:01:21 +03:00
else if(net.types[i] == DROPOUT){
dropout_layer layer = *(dropout_layer *)net.layers[i];
backward_dropout_layer_gpu(layer, prev_delta);
}
2014-11-06 01:49:58 +03:00
else if(net.types[i] == SOFTMAX){
softmax_layer layer = *(softmax_layer *)net.layers[i];
backward_softmax_layer_gpu(layer, prev_delta);
}
2015-01-14 23:18:57 +03:00
//printf("Backward %d %s %f\n", i, get_layer_string(net.types[i]), sec(clock() - time));
2014-11-06 01:49:58 +03:00
}
}
void update_network_gpu(network net)
{
int i;
for(i = 0; i < net.n; ++i){
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
update_convolutional_layer_gpu(layer);
}
else if(net.types[i] == CONNECTED){
connected_layer layer = *(connected_layer *)net.layers[i];
update_connected_layer_gpu(layer);
}
}
}
2015-01-23 03:38:24 +03:00
float * get_network_output_gpu_layer(network net, int i)
2014-11-06 01:49:58 +03:00
{
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == CONNECTED){
connected_layer layer = *(connected_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == MAXPOOL){
maxpool_layer layer = *(maxpool_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
2014-12-16 22:40:05 +03:00
else if(net.types[i] == CROP){
crop_layer layer = *(crop_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.output_gpu;
2014-12-16 22:40:05 +03:00
}
2014-11-06 01:49:58 +03:00
else if(net.types[i] == SOFTMAX){
softmax_layer layer = *(softmax_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.output_gpu;
2014-11-19 00:51:04 +03:00
} else if(net.types[i] == DROPOUT){
2014-12-19 02:46:45 +03:00
dropout_layer layer = *(dropout_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.output_gpu;
2014-11-06 01:49:58 +03:00
}
return 0;
}
2015-01-23 03:38:24 +03:00
float * get_network_delta_gpu_layer(network net, int i)
2014-11-06 01:49:58 +03:00
{
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.delta_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == CONNECTED){
connected_layer layer = *(connected_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.delta_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == MAXPOOL){
maxpool_layer layer = *(maxpool_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.delta_gpu;
2014-11-06 01:49:58 +03:00
}
else if(net.types[i] == SOFTMAX){
softmax_layer layer = *(softmax_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
return layer.delta_gpu;
2014-11-19 00:51:04 +03:00
} else if(net.types[i] == DROPOUT){
2014-12-19 02:46:45 +03:00
if(i == 0) return 0;
2015-01-23 03:38:24 +03:00
return get_network_delta_gpu_layer(net, i-1);
2014-11-06 01:49:58 +03:00
}
return 0;
}
float train_network_datum_gpu(network net, float *x, float *y)
{
2015-01-28 00:31:06 +03:00
//clock_t time = clock();
2014-11-06 01:49:58 +03:00
int x_size = get_network_input_size(net)*net.batch;
int y_size = get_network_output_size(net)*net.batch;
2015-01-23 03:38:24 +03:00
if(!*net.input_gpu){
*net.input_gpu = cuda_make_array(x, x_size);
*net.truth_gpu = cuda_make_array(y, y_size);
2014-11-06 01:49:58 +03:00
}else{
2015-01-23 03:38:24 +03:00
cuda_push_array(*net.input_gpu, x, x_size);
cuda_push_array(*net.truth_gpu, y, y_size);
2014-11-06 01:49:58 +03:00
}
2015-01-28 00:31:06 +03:00
//printf("trans %f\n", sec(clock() - time));
//time = clock();
2015-01-23 03:38:24 +03:00
forward_network_gpu(net, *net.input_gpu, *net.truth_gpu, 1);
2015-01-28 00:31:06 +03:00
//printf("forw %f\n", sec(clock() - time));
//time = clock();
2015-01-23 03:38:24 +03:00
backward_network_gpu(net, *net.input_gpu);
2015-01-28 00:31:06 +03:00
//printf("back %f\n", sec(clock() - time));
//time = clock();
2014-11-06 01:49:58 +03:00
update_network_gpu(net);
float error = get_network_cost(net);
2015-01-28 00:31:06 +03:00
//printf("updt %f\n", sec(clock() - time));
//time = clock();
2014-11-06 01:49:58 +03:00
return error;
}
float *get_network_output_layer_gpu(network net, int i)
{
if(net.types[i] == CONVOLUTIONAL){
convolutional_layer layer = *(convolutional_layer *)net.layers[i];
return layer.output;
}
else if(net.types[i] == CONNECTED){
connected_layer layer = *(connected_layer *)net.layers[i];
2015-01-23 03:38:24 +03:00
cuda_pull_array(layer.output_gpu, layer.output, layer.outputs*layer.batch);
2014-11-06 01:49:58 +03:00
return layer.output;
}
else if(net.types[i] == MAXPOOL){
maxpool_layer layer = *(maxpool_layer *)net.layers[i];
return layer.output;
}
else if(net.types[i] == SOFTMAX){
softmax_layer layer = *(softmax_layer *)net.layers[i];
pull_softmax_layer_output(layer);
return layer.output;
}
return 0;
}
float *get_network_output_gpu(network net)
{
int i;
for(i = net.n-1; i > 0; --i) if(net.types[i] != COST) break;
return get_network_output_layer_gpu(net, i);
}
float *network_predict_gpu(network net, float *input)
{
2014-12-16 22:40:05 +03:00
2014-11-06 01:49:58 +03:00
int size = get_network_input_size(net) * net.batch;
2015-01-23 03:38:24 +03:00
float * input_gpu = cuda_make_array(input, size);
forward_network_gpu(net, input_gpu, 0, 0);
2014-11-06 01:49:58 +03:00
float *out = get_network_output_gpu(net);
2015-01-23 03:38:24 +03:00
cuda_free(input_gpu);
2014-11-06 01:49:58 +03:00
return out;
}