Joseph Redmon
2015-09-16 c53e03348c65462bcba33f6352087dd6b9268e8f
src/network_kernels.cu
@@ -1,6 +1,7 @@
extern "C" {
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include "network.h"
#include "image.h"
@@ -12,13 +13,17 @@
#include "crop_layer.h"
#include "connected_layer.h"
#include "detection_layer.h"
#include "region_layer.h"
#include "convolutional_layer.h"
#include "deconvolutional_layer.h"
#include "maxpool_layer.h"
#include "avgpool_layer.h"
#include "normalization_layer.h"
#include "cost_layer.h"
#include "softmax_layer.h"
#include "dropout_layer.h"
#include "route_layer.h"
#include "blas.h"
}
float * get_network_output_gpu_layer(network net, int i);
@@ -30,12 +35,17 @@
    int i;
    for(i = 0; i < net.n; ++i){
        layer l = net.layers[i];
        if(l.delta_gpu){
            scal_ongpu(l.outputs * l.batch, 0, l.delta_gpu, 1);
        }
        if(l.type == CONVOLUTIONAL){
            forward_convolutional_layer_gpu(l, state);
        } else if(l.type == DECONVOLUTIONAL){
            forward_deconvolutional_layer_gpu(l, state);
        } else if(l.type == DETECTION){
            forward_detection_layer_gpu(l, state);
        } else if(l.type == REGION){
            forward_region_layer_gpu(l, state);
        } else if(l.type == CONNECTED){
            forward_connected_layer_gpu(l, state);
        } else if(l.type == CROP){
@@ -44,8 +54,12 @@
            forward_cost_layer_gpu(l, state);
        } else if(l.type == SOFTMAX){
            forward_softmax_layer_gpu(l, state);
        } else if(l.type == NORMALIZATION){
            forward_normalization_layer_gpu(l, state);
        } else if(l.type == MAXPOOL){
            forward_maxpool_layer_gpu(l, state);
        } else if(l.type == AVGPOOL){
            forward_avgpool_layer_gpu(l, state);
        } else if(l.type == DROPOUT){
            forward_dropout_layer_gpu(l, state);
        } else if(l.type == ROUTE){
@@ -59,11 +73,12 @@
{
    int i;
    float * original_input = state.input;
    float * original_delta = state.delta;
    for(i = net.n-1; i >= 0; --i){
        layer l = net.layers[i];
        if(i == 0){
            state.input = original_input;
            state.delta = 0;
            state.delta = original_delta;
        }else{
            layer prev = net.layers[i-1];
            state.input = prev.output_gpu;
@@ -75,10 +90,16 @@
            backward_deconvolutional_layer_gpu(l, state);
        } else if(l.type == MAXPOOL){
            if(i != 0) backward_maxpool_layer_gpu(l, state);
        } else if(l.type == AVGPOOL){
            if(i != 0) backward_avgpool_layer_gpu(l, state);
        } else if(l.type == DROPOUT){
            backward_dropout_layer_gpu(l, state);
        } else if(l.type == DETECTION){
            backward_detection_layer_gpu(l, state);
        } else if(l.type == REGION){
            backward_region_layer_gpu(l, state);
        } else if(l.type == NORMALIZATION){
            backward_normalization_layer_gpu(l, state);
        } else if(l.type == SOFTMAX){
            if(i != 0) backward_softmax_layer_gpu(l, state);
        } else if(l.type == CONNECTED){
@@ -95,14 +116,15 @@
{
    int i;
    int update_batch = net.batch*net.subdivisions;
    float rate = get_current_rate(net);
    for(i = 0; i < net.n; ++i){
        layer l = net.layers[i];
        if(l.type == CONVOLUTIONAL){
            update_convolutional_layer_gpu(l, update_batch, net.learning_rate, net.momentum, net.decay);
            update_convolutional_layer_gpu(l, update_batch, rate, net.momentum, net.decay);
        } else if(l.type == DECONVOLUTIONAL){
            update_deconvolutional_layer_gpu(l, net.learning_rate, net.momentum, net.decay);
            update_deconvolutional_layer_gpu(l, rate, net.momentum, net.decay);
        } else if(l.type == CONNECTED){
            update_connected_layer_gpu(l, update_batch, net.learning_rate, net.momentum, net.decay);
            update_connected_layer_gpu(l, update_batch, rate, net.momentum, net.decay);
        }
    }
}
@@ -112,6 +134,7 @@
    network_state state;
    int x_size = get_network_input_size(net)*net.batch;
    int y_size = get_network_output_size(net)*net.batch;
    if(net.layers[net.n-1].type == REGION) y_size = net.layers[net.n-1].truths*net.batch;
    if(!*net.input_gpu){
        *net.input_gpu = cuda_make_array(x, x_size);
        *net.truth_gpu = cuda_make_array(y, y_size);
@@ -120,12 +143,13 @@
        cuda_push_array(*net.truth_gpu, y, y_size);
    }
    state.input = *net.input_gpu;
    state.delta = 0;
    state.truth = *net.truth_gpu;
    state.train = 1;
    forward_network_gpu(net, state);
    backward_network_gpu(net, state);
    float error = get_network_cost(net);
    if ((net.seen / net.batch) % net.subdivisions == 0) update_network_gpu(net);
    if (((*net.seen) / net.batch) % net.subdivisions == 0) update_network_gpu(net);
    return error;
}
@@ -133,23 +157,8 @@
float *get_network_output_layer_gpu(network net, int i)
{
    layer l = net.layers[i];
    if(l.type == CONVOLUTIONAL){
        return l.output;
    } else if(l.type == DECONVOLUTIONAL){
        return l.output;
    } else if(l.type == CONNECTED){
        cuda_pull_array(l.output_gpu, l.output, l.outputs*l.batch);
        return l.output;
    } else if(l.type == DETECTION){
        cuda_pull_array(l.output_gpu, l.output, l.outputs*l.batch);
        return l.output;
    } else if(l.type == MAXPOOL){
        return l.output;
    } else if(l.type == SOFTMAX){
        pull_softmax_layer_output(l);
        return l.output;
    }
    return 0;
    cuda_pull_array(l.output_gpu, l.output, l.outputs*l.batch);
    return l.output;
}
float *get_network_output_gpu(network net)