darknet/src/deconvolutional_kernels.cu

105 lines
3.7 KiB
Plaintext
Raw Normal View History

2015-02-11 06:41:03 +03:00
extern "C" {
#include "convolutional_layer.h"
#include "deconvolutional_layer.h"
#include "gemm.h"
#include "blas.h"
#include "im2col.h"
#include "col2im.h"
#include "utils.h"
#include "cuda.h"
}
2015-03-12 08:20:15 +03:00
extern "C" void forward_deconvolutional_layer_gpu(deconvolutional_layer layer, network_state state)
2015-02-11 06:41:03 +03:00
{
int i;
int out_h = deconvolutional_out_height(layer);
int out_w = deconvolutional_out_width(layer);
int size = out_h*out_w;
int m = layer.size*layer.size*layer.n;
int n = layer.h*layer.w;
int k = layer.c;
bias_output_gpu(layer.output_gpu, layer.biases_gpu, layer.batch, layer.n, size);
for(i = 0; i < layer.batch; ++i){
float *a = layer.filters_gpu;
2015-03-12 08:20:15 +03:00
float *b = state.input + i*layer.c*layer.h*layer.w;
2015-02-11 06:41:03 +03:00
float *c = layer.col_image_gpu;
gemm_ongpu(1,0,m,n,k,1,a,m,b,n,0,c,n);
col2im_ongpu(c, layer.n, out_h, out_w, layer.size, layer.stride, 0, layer.output_gpu+i*layer.n*size);
}
activate_array(layer.output_gpu, layer.batch*layer.n*size, layer.activation);
}
2015-03-12 08:20:15 +03:00
extern "C" void backward_deconvolutional_layer_gpu(deconvolutional_layer layer, network_state state)
2015-02-11 06:41:03 +03:00
{
float alpha = 1./layer.batch;
int out_h = deconvolutional_out_height(layer);
int out_w = deconvolutional_out_width(layer);
int size = out_h*out_w;
int i;
gradient_array(layer.output_gpu, size*layer.n*layer.batch, layer.activation, layer.delta_gpu);
backward_bias(layer.bias_updates_gpu, layer.delta, layer.batch, layer.n, size);
2015-03-12 08:20:15 +03:00
if(state.delta) memset(state.delta, 0, layer.batch*layer.h*layer.w*layer.c*sizeof(float));
2015-02-11 06:41:03 +03:00
for(i = 0; i < layer.batch; ++i){
int m = layer.c;
int n = layer.size*layer.size*layer.n;
int k = layer.h*layer.w;
2015-03-12 08:20:15 +03:00
float *a = state.input + i*m*n;
2015-02-11 06:41:03 +03:00
float *b = layer.col_image_gpu;
float *c = layer.filter_updates_gpu;
im2col_ongpu(layer.delta_gpu + i*layer.n*size, layer.n, out_h, out_w,
layer.size, layer.stride, 0, b);
gemm_ongpu(0,1,m,n,k,alpha,a,k,b,k,1,c,n);
2015-03-12 08:20:15 +03:00
if(state.delta){
2015-02-11 06:41:03 +03:00
int m = layer.c;
int n = layer.h*layer.w;
int k = layer.size*layer.size*layer.n;
float *a = layer.filters_gpu;
float *b = layer.col_image_gpu;
2015-03-12 08:20:15 +03:00
float *c = state.delta + i*n*m;
2015-02-11 06:41:03 +03:00
gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
}
}
}
extern "C" void pull_deconvolutional_layer(deconvolutional_layer layer)
{
cuda_pull_array(layer.filters_gpu, layer.filters, layer.c*layer.n*layer.size*layer.size);
cuda_pull_array(layer.biases_gpu, layer.biases, layer.n);
cuda_pull_array(layer.filter_updates_gpu, layer.filter_updates, layer.c*layer.n*layer.size*layer.size);
cuda_pull_array(layer.bias_updates_gpu, layer.bias_updates, layer.n);
}
extern "C" void push_deconvolutional_layer(deconvolutional_layer layer)
{
cuda_push_array(layer.filters_gpu, layer.filters, layer.c*layer.n*layer.size*layer.size);
cuda_push_array(layer.biases_gpu, layer.biases, layer.n);
cuda_push_array(layer.filter_updates_gpu, layer.filter_updates, layer.c*layer.n*layer.size*layer.size);
cuda_push_array(layer.bias_updates_gpu, layer.bias_updates, layer.n);
}
2015-03-12 08:20:15 +03:00
extern "C" void update_deconvolutional_layer_gpu(deconvolutional_layer layer, float learning_rate, float momentum, float decay)
2015-02-11 06:41:03 +03:00
{
int size = layer.size*layer.size*layer.c*layer.n;
2015-03-12 08:20:15 +03:00
axpy_ongpu(layer.n, learning_rate, layer.bias_updates_gpu, 1, layer.biases_gpu, 1);
scal_ongpu(layer.n, momentum, layer.bias_updates_gpu, 1);
2015-02-11 06:41:03 +03:00
2015-03-12 08:20:15 +03:00
axpy_ongpu(size, -decay, layer.filters_gpu, 1, layer.filter_updates_gpu, 1);
axpy_ongpu(size, learning_rate, layer.filter_updates_gpu, 1, layer.filters_gpu, 1);
scal_ongpu(size, momentum, layer.filter_updates_gpu, 1);
2015-02-11 06:41:03 +03:00
}