yolo layers

This commit is contained in:
Francesco Gatti
2017-08-01 16:08:56 +02:00
parent 8e4b3c6c17
commit b94931f9f7
21 changed files with 522 additions and 84 deletions
+1 -1
View File
@@ -35,4 +35,4 @@ void activationELUForward(value_type* srcData, value_type* dstData, int size)
activation_elu<<<blocks, threads>>>(srcData, dstData, size);
checkCuda( cudaDeviceSynchronize() );
}
}
+29
View File
@@ -0,0 +1,29 @@
#include "kernels.h"
__global__
void activation_leaky(value_type *input, value_type *output, int size) {
int i = blockDim.x*blockIdx.x + threadIdx.x;
if(i<size) {
if (input[i]>0)
output[i] = input[i];
else
output[i] = 0.1f*input[i];
}
}
/**
ELU activation function
*/
void activationLEAKYForward(value_type* srcData, value_type* dstData, int size)
{
int blocks = (size+255)/256;
int threads = 256;
activation_leaky<<<blocks, threads>>>(srcData, dstData, size);
checkCuda( cudaDeviceSynchronize() );
}
+26
View File
@@ -0,0 +1,26 @@
#include "kernels.h"
__global__
void activation_logistic(value_type *input, value_type *output, int size) {
int i = blockDim.x*blockIdx.x + threadIdx.x;
if(i<size) {
output[i] = 1.0f/(1.0f + exp(-input[i]));;
}
}
/**
LOGISTIC activation function
*/
void activationLOGISTICForward(value_type* srcData, value_type* dstData, int size)
{
int blocks = (size+255)/256;
int threads = 256;
activation_logistic<<<blocks, threads>>>(srcData, dstData, size);
checkCuda( cudaDeviceSynchronize() );
}
+49
View File
@@ -0,0 +1,49 @@
#include "kernels.h"
__global__ void reorg_kernel(int N, float *x, int w, int h, int c, int batch, int stride, int forward, float *out)
{
int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
if(i >= N) return;
int in_index = i;
int in_w = i%w;
i = i/w;
int in_h = i%h;
i = i/h;
int in_c = i%c;
i = i/c;
int b = i%batch;
int out_c = c/(stride*stride);
int c2 = in_c % out_c;
int offset = in_c / out_c;
int w2 = in_w*stride + offset % stride;
int h2 = in_h*stride + offset / stride;
//printf("%d\n", offset);
int out_index = w2 + w*stride*(h2 + h*stride*(c2 + out_c*b));
// printf("%d %d %d\n", w2, h2, c2);
//printf("%d %d\n", in_index, out_index);
//if(out_index >= N || out_index < 0) printf("bad bad bad \n");
if(forward) out[out_index] = x[in_index];
else out[in_index] = x[out_index];
//if(forward) out[1] = x[1];
//else out[0] = x[0];
}
/**
reorg function function
*/
void reorgForward(value_type* srcData, value_type* dstData, tkDNN::dataDim_t dim, int stride)
{
int size = dim.tot();
int blocks = (size+255)/256;
int threads = 256;
reorg_kernel<<<blocks, threads>>>(size, srcData, dim.w, dim.h, dim.c, dim.n, stride, false, dstData);
checkCuda( cudaDeviceSynchronize() );
}
+43
View File
@@ -0,0 +1,43 @@
#include "kernels.h"
__device__ void softmax_device(float *input, int n, float temp, int stride, float *output)
{
int i;
float sum = 0;
float largest = -INFINITY;
for(i = 0; i < n; ++i){
int val = input[i*stride];
largest = (val>largest) ? val : largest;
}
for(i = 0; i < n; ++i){
float e = exp(input[i*stride]/temp - largest/temp);
sum += e;
output[i*stride] = e;
}
for(i = 0; i < n; ++i){
output[i*stride] /= sum;
}
}
__global__ void softmax_kernel(float *input, int n, int batch, int batch_offset, int groups, int group_offset, int stride, float temp, float *output)
{
int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
if (id >= batch*groups) return;
int b = id / groups;
int g = id % groups;
softmax_device(input + b*batch_offset + g*group_offset, n, temp, stride, output + b*batch_offset + g*group_offset);
}
/**
softmax function
*/
void softmaxForward(float *input, int n, int batch, int batch_offset,
int groups, int group_offset, int stride, float temp, float *output)
{
int size = groups*batch;
int blocks = (size+255)/256;
int threads = 256;
softmax_kernel<<<blocks, threads>>>(input, n, batch, batch_offset, groups, group_offset, stride, temp, output);
checkCuda( cudaDeviceSynchronize() );
}