opencv viz
This commit is contained in:
+4
-4
@@ -9,7 +9,7 @@ Activation::Activation(Network *net, int act_mode) :
|
||||
Layer(net) {
|
||||
|
||||
this->act_mode = act_mode;
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(dnnType)) );
|
||||
|
||||
if(int(act_mode) < 100) {
|
||||
|
||||
@@ -43,14 +43,14 @@ Activation::~Activation() {
|
||||
checkCUDNN( cudnnDestroyActivationDescriptor(activDesc) );
|
||||
}
|
||||
|
||||
value_type* Activation::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Activation::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
if(act_mode == ACTIVATION_LEAKY) {
|
||||
activationLEAKYForward(srcData, dstData, dim.tot());
|
||||
|
||||
} else {
|
||||
value_type alpha = value_type(1);
|
||||
value_type beta = value_type(0);
|
||||
dnnType alpha = dnnType(1);
|
||||
dnnType beta = dnnType(0);
|
||||
checkCUDNN( cudnnActivationForward(net->cudnnHandle,
|
||||
activDesc,
|
||||
&alpha,
|
||||
|
||||
+6
-6
@@ -76,7 +76,7 @@ Conv2d::Conv2d( Network *net, int out_ch, int kernelH, int kernelW,
|
||||
output_dim.l = 1;
|
||||
|
||||
//allocate data for infer result
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
Conv2d::~Conv2d() {
|
||||
@@ -91,12 +91,12 @@ Conv2d::~Conv2d() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* Conv2d::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Conv2d::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
|
||||
// convolution
|
||||
value_type alpha = value_type(1);
|
||||
value_type beta = value_type(0);
|
||||
dnnType alpha = dnnType(1);
|
||||
dnnType beta = dnnType(0);
|
||||
checkCUDNN( cudnnConvolutionForward(net->cudnnHandle,
|
||||
&alpha, srcTensorDesc, srcData, filterDesc,
|
||||
data_d, convDesc, algo, workSpace, ws_sizeInBytes,
|
||||
@@ -104,8 +104,8 @@ value_type* Conv2d::infer(dataDim_t &dim, value_type* srcData) {
|
||||
|
||||
if(!batchnorm) {
|
||||
// bias
|
||||
alpha = value_type(1);
|
||||
beta = value_type(1);
|
||||
alpha = dnnType(1);
|
||||
beta = dnnType(1);
|
||||
checkCUDNN( cudnnAddTensor(net->cudnnHandle,
|
||||
&alpha, biasTensorDesc, bias_d,
|
||||
&beta, dstTensorDesc, dstData) );
|
||||
|
||||
+4
-4
@@ -14,7 +14,7 @@ Dense::Dense(Network *net, int out_ch, const char* fname_weights) :
|
||||
output_dim.l = 1;
|
||||
|
||||
//allocate data for infer result
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
Dense::~Dense() {
|
||||
@@ -22,7 +22,7 @@ Dense::~Dense() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* Dense::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Dense::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
if (dim.n != 1)
|
||||
FatalError("Not Implemented");
|
||||
@@ -33,9 +33,9 @@ value_type* Dense::infer(dataDim_t &dim, value_type* srcData) {
|
||||
if (dim_x != input_dim.tot())
|
||||
FatalError("Input mismatch");
|
||||
|
||||
value_type alpha = value_type(1), beta = value_type(1);
|
||||
dnnType alpha = dnnType(1), beta = dnnType(1);
|
||||
// place bias into dstData
|
||||
checkCuda( cudaMemcpy(dstData, bias_d, dim_y*sizeof(value_type), cudaMemcpyDeviceToDevice) );
|
||||
checkCuda( cudaMemcpy(dstData, bias_d, dim_y*sizeof(dnnType), cudaMemcpyDeviceToDevice) );
|
||||
|
||||
//do matrix moltiplication
|
||||
checkERROR( cublasSgemv(net->cublasHandle, CUBLAS_OP_T,
|
||||
|
||||
+2
-2
@@ -7,7 +7,7 @@ namespace tkDNN {
|
||||
|
||||
Flatten::Flatten(Network *net) : Layer(net) {
|
||||
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(dnnType)) );
|
||||
|
||||
output_dim.n = 1;
|
||||
output_dim.c = input_dim.tot();
|
||||
@@ -22,7 +22,7 @@ Flatten::~Flatten() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* Flatten::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Flatten::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
//transpose per channel
|
||||
matrixTranspose(net->cublasHandle, srcData, dstData, dim.c, dim.h*dim.w*dim.l);
|
||||
|
||||
+6
-6
@@ -5,7 +5,7 @@
|
||||
|
||||
namespace tkDNN {
|
||||
|
||||
MulAdd::MulAdd(Network *net, value_type mul, value_type add) : Layer(net) {
|
||||
MulAdd::MulAdd(Network *net, dnnType mul, dnnType add) : Layer(net) {
|
||||
|
||||
this->mul = mul;
|
||||
this->add = add;
|
||||
@@ -13,16 +13,16 @@ MulAdd::MulAdd(Network *net, value_type mul, value_type add) : Layer(net) {
|
||||
int size = input_dim.tot();
|
||||
|
||||
// create a vector with all value setted to add
|
||||
value_type *add_vector_h = new value_type[size];
|
||||
dnnType *add_vector_h = new dnnType[size];
|
||||
for(int i=0; i<size; i++)
|
||||
add_vector_h[i] = add;
|
||||
|
||||
checkCuda( cudaMalloc(&add_vector, size*sizeof(value_type)));
|
||||
checkCuda( cudaMemcpy(add_vector, add_vector_h, size*sizeof(value_type), cudaMemcpyHostToDevice));
|
||||
checkCuda( cudaMalloc(&add_vector, size*sizeof(dnnType)));
|
||||
checkCuda( cudaMemcpy(add_vector, add_vector_h, size*sizeof(dnnType), cudaMemcpyHostToDevice));
|
||||
delete [] add_vector_h;
|
||||
|
||||
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
MulAdd::~MulAdd() {
|
||||
@@ -31,7 +31,7 @@ MulAdd::~MulAdd() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* MulAdd::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* MulAdd::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
matrixMulAdd(net->cublasHandle, srcData, dstData, add_vector, input_dim.tot(), mul);
|
||||
|
||||
|
||||
+1
-1
@@ -30,7 +30,7 @@ Network::~Network() {
|
||||
checkERROR( cublasDestroy(cublasHandle) );
|
||||
}
|
||||
|
||||
value_type* Network::infer(dataDim_t &dim, value_type* data) {
|
||||
dnnType* Network::infer(dataDim_t &dim, dnnType* data) {
|
||||
|
||||
//do infer for every layer
|
||||
for(int i=0; i<num_layers; i++) {
|
||||
|
||||
+5
-5
@@ -81,9 +81,9 @@ NetworkRT::NetworkRT(Network *net) {
|
||||
std::cout<<"input idex = "<<buf_input_idx<<" -> output index = "<<buf_output_idx<<"\n";
|
||||
|
||||
// create GPU buffers and a stream
|
||||
checkCuda(cudaMalloc(&buffersRT[buf_input_idx], dim.tot()*sizeof(value_type)));
|
||||
checkCuda(cudaMalloc(&buffersRT[buf_output_idx], output_dim.tot()*sizeof(value_type)));
|
||||
checkCuda(cudaMalloc(&output, output_dim.tot()*sizeof(value_type)));
|
||||
checkCuda(cudaMalloc(&buffersRT[buf_input_idx], dim.tot()*sizeof(dnnType)));
|
||||
checkCuda(cudaMalloc(&buffersRT[buf_output_idx], output_dim.tot()*sizeof(dnnType)));
|
||||
checkCuda(cudaMalloc(&output, output_dim.tot()*sizeof(dnnType)));
|
||||
checkCuda(cudaStreamCreate(&stream));
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ NetworkRT::~NetworkRT() {
|
||||
|
||||
}
|
||||
|
||||
value_type* NetworkRT::infer(dataDim_t &dim, value_type* data) {
|
||||
dnnType* NetworkRT::infer(dataDim_t &dim, dnnType* data) {
|
||||
|
||||
checkCuda(cudaMemcpyAsync(buffersRT[buf_input_idx], data, dim.tot()*sizeof(float), cudaMemcpyDeviceToDevice, stream));
|
||||
contextRT->enqueue(1, buffersRT, stream, nullptr);
|
||||
@@ -161,7 +161,7 @@ ITensor* NetworkRT::convert_layer(ITensor *input, Conv2d *l) {
|
||||
float eps = CUDNN_BN_MIN_EPSILON;
|
||||
|
||||
//make power array of ones
|
||||
value_type *power_h = new value_type[l->outputs];
|
||||
dnnType *power_h = new dnnType[l->outputs];
|
||||
for(int i=0; i<l->outputs; i++) power_h[i] = 1.0f;
|
||||
|
||||
//convert mean
|
||||
|
||||
+8
-8
@@ -57,15 +57,15 @@ Pooling::Pooling( Network *net, int winH, int winW, int strideH, int strideW,
|
||||
output_dim.w = w;
|
||||
output_dim.l = l;
|
||||
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(dnnType)) );
|
||||
|
||||
//pool on 3d data need transposition at the enter and on the exit
|
||||
//allocate for initial and final transposition
|
||||
if(poolOn3d) {
|
||||
output_dim.n = 1;
|
||||
|
||||
checkCuda( cudaMalloc(&tmpInputData, input_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&tmpOutputData, output_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&tmpInputData, input_dim.tot()*sizeof(dnnType)) );
|
||||
checkCuda( cudaMalloc(&tmpOutputData, output_dim.tot()*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -81,10 +81,10 @@ Pooling::~Pooling() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* Pooling::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Pooling::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
value_type *poolSrc = srcData;
|
||||
value_type *poolDst = dstData;
|
||||
dnnType *poolSrc = srcData;
|
||||
dnnType *poolDst = dstData;
|
||||
|
||||
if(poolOn3d) {
|
||||
matrixTranspose(net->cublasHandle, srcData, tmpInputData, dim.h*dim.w*dim.c, dim.l);
|
||||
@@ -92,8 +92,8 @@ value_type* Pooling::infer(dataDim_t &dim, value_type* srcData) {
|
||||
poolDst = tmpOutputData;
|
||||
}
|
||||
|
||||
value_type alpha = value_type(1);
|
||||
value_type beta = value_type(0);
|
||||
dnnType alpha = dnnType(1);
|
||||
dnnType beta = dnnType(0);
|
||||
checkCUDNN( cudnnPoolingForward(net->cudnnHandle, poolingDesc,
|
||||
&alpha, srcTensorDesc, poolSrc,
|
||||
&beta, dstTensorDesc, poolDst) );
|
||||
|
||||
+55
-8
@@ -1,5 +1,10 @@
|
||||
#include <iostream>
|
||||
|
||||
#ifdef OPENCV
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
#endif
|
||||
|
||||
#include "Layer.h"
|
||||
#include "kernels.h"
|
||||
|
||||
@@ -12,6 +17,7 @@ Region::Region(Network *net, int classes, int coords, int num, float thresh, con
|
||||
this->coords = coords;
|
||||
this->num = num;
|
||||
this->thresh = thresh;
|
||||
this->res_boxes_n = 0;
|
||||
|
||||
// same
|
||||
output_dim.n = input_dim.n;
|
||||
@@ -23,7 +29,7 @@ Region::Region(Network *net, int classes, int coords, int num, float thresh, con
|
||||
//load anchors
|
||||
readBinaryFile(fname_weights, 2*num, &bias_h, &bias_d);
|
||||
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
Region::~Region() {
|
||||
@@ -36,9 +42,9 @@ int Region::entry_index(int batch, int location, int entry) {
|
||||
return batch*output_dim.tot() + n*input_dim.w*input_dim.h*(coords+classes+1) + entry*input_dim.w*input_dim.h + loc;
|
||||
}
|
||||
|
||||
value_type* Region::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Region::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
checkCuda( cudaMemcpy(dstData, srcData, dim.tot()*sizeof(value_type), cudaMemcpyDeviceToDevice));
|
||||
checkCuda( cudaMemcpy(dstData, srcData, dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToDevice));
|
||||
|
||||
for (int b = 0; b < dim.n; ++b){
|
||||
for(int n = 0; n < num; ++n){
|
||||
@@ -192,11 +198,11 @@ int max_index(float *a, int n) {
|
||||
|
||||
void Region::interpretData() {
|
||||
|
||||
int imW = 768, imH = 576;
|
||||
int imW = net->input_dim.w, imH = net->input_dim.h;
|
||||
|
||||
int tot = output_dim.w*output_dim.h*num;
|
||||
float *lel = new value_type[output_dim.tot()];
|
||||
cudaMemcpy(lel, dstData, output_dim.tot()*sizeof(value_type), cudaMemcpyDeviceToHost);
|
||||
float *lel = new dnnType[output_dim.tot()];
|
||||
cudaMemcpy(lel, dstData, output_dim.tot()*sizeof(dnnType), cudaMemcpyDeviceToHost);
|
||||
box *boxes = (box*) calloc(tot, sizeof(box));
|
||||
float **probs = (float**) calloc(tot, sizeof(float *));
|
||||
for(int j = 0; j < tot; ++j) probs[j] = (float*)calloc(classes + 1, sizeof(float *));
|
||||
@@ -232,10 +238,51 @@ void Region::interpretData() {
|
||||
int cl = max_index(probs[i], classes);
|
||||
float prob = probs[i][cl];
|
||||
if(prob > thresh) {
|
||||
//printf("%d %s: %.0f%%\n", i, names[class], prob*100);
|
||||
printf("%d: %.0f%%\n", cl, prob*100);
|
||||
box b = boxes[i];
|
||||
int x = (b.x-b.w/2.)*imW;
|
||||
int w = (b.x+b.w/2.)*imW - b.x;
|
||||
int y = (b.y-b.h/2.)*imH;
|
||||
int h = (b.y+b.h/2.)*imH - b.y;
|
||||
|
||||
printf("%d: %.0f%% box(x1, y1, x2, y2): %d %d %d %d\n", cl, prob*100, x, y, w, h);
|
||||
b.x = x;
|
||||
b.y = y;
|
||||
b.h = h;
|
||||
b.w = w;
|
||||
res_boxes[res_boxes_n] = b;
|
||||
res_boxes_n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Region::showImageResult(dnnType *input_h) {
|
||||
|
||||
#ifdef OPENCV
|
||||
dataDim_t dim = net->input_dim;
|
||||
// read an image
|
||||
cv::Mat r(dim.h, dim.w, CV_32F, input_h);
|
||||
cv::Mat g(dim.h, dim.w, CV_32F, input_h + dim.h*dim.w);
|
||||
cv::Mat b(dim.h, dim.w, CV_32F, input_h + dim.h*dim.w*2);
|
||||
std::vector<cv::Mat> array_to_merge;
|
||||
array_to_merge.push_back(b);
|
||||
array_to_merge.push_back(g);
|
||||
array_to_merge.push_back(r);
|
||||
cv::Mat color;
|
||||
cv::merge(array_to_merge, color);
|
||||
|
||||
for(int i=0; i<res_boxes_n; i++) {
|
||||
box bx = res_boxes[i];
|
||||
cv::rectangle(color, cv::Point(bx.x, bx.y), cv::Point(bx.w, bx.h),
|
||||
cv::Scalar( 0, 0, 255), 2);
|
||||
}
|
||||
cv::namedWindow("result");
|
||||
// show the image on window
|
||||
cv::imshow("result", color);
|
||||
// wait key for 5000 ms
|
||||
cv::waitKey(5000);
|
||||
#else
|
||||
std::cout<<"Visualization not supported, please recompile with OpenCV\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -15,7 +15,7 @@ Reorg::Reorg(Network *net, int stride) : Layer(net) {
|
||||
output_dim.w = input_dim.w/stride;
|
||||
output_dim.l = input_dim.l;
|
||||
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
Reorg::~Reorg() {
|
||||
@@ -23,7 +23,7 @@ Reorg::~Reorg() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* Reorg::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Reorg::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
reorgForward(srcData, dstData, dim.n, dim.c, dim.h, dim.w, stride);
|
||||
|
||||
|
||||
+4
-4
@@ -28,7 +28,7 @@ Route::Route(Network *net, Layer **layers, int layers_n) : Layer(net) {
|
||||
|
||||
input_dim = output_dim;
|
||||
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
Route::~Route() {
|
||||
@@ -36,14 +36,14 @@ Route::~Route() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* Route::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Route::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
|
||||
int offset = 0;
|
||||
for(int i=0; i<layers_n; i++) {
|
||||
value_type *input = layers[i]->dstData;
|
||||
dnnType *input = layers[i]->dstData;
|
||||
int in_dim = layers[i]->input_dim.tot();
|
||||
checkCuda( cudaMemcpy(dstData + offset, input, in_dim*sizeof(value_type), cudaMemcpyDeviceToDevice));
|
||||
checkCuda( cudaMemcpy(dstData + offset, input, in_dim*sizeof(dnnType), cudaMemcpyDeviceToDevice));
|
||||
offset += in_dim;
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -7,7 +7,7 @@ namespace tkDNN {
|
||||
|
||||
Softmax::Softmax(Network *net) : Layer(net) {
|
||||
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(dnnType)) );
|
||||
|
||||
checkCUDNN( cudnnSetTensor4dDescriptor(srcTensorDesc,
|
||||
net->tensorFormat,
|
||||
@@ -28,10 +28,10 @@ Softmax::~Softmax() {
|
||||
checkCuda( cudaFree(dstData) );
|
||||
}
|
||||
|
||||
value_type* Softmax::infer(dataDim_t &dim, value_type* srcData) {
|
||||
dnnType* Softmax::infer(dataDim_t &dim, dnnType* srcData) {
|
||||
|
||||
value_type alpha = value_type(1);
|
||||
value_type beta = value_type(0);
|
||||
dnnType alpha = dnnType(1);
|
||||
dnnType beta = dnnType(0);
|
||||
checkCUDNN( cudnnSoftmaxForward(net->cudnnHandle,
|
||||
CUDNN_SOFTMAX_ACCURATE ,
|
||||
CUDNN_SOFTMAX_MODE_CHANNEL,
|
||||
|
||||
@@ -7,12 +7,12 @@
|
||||
x > 0 : y = x
|
||||
*/
|
||||
__global__
|
||||
void activation_elu(value_type *input, value_type *output, int size) {
|
||||
void activation_elu(dnnType *input, dnnType *output, int size) {
|
||||
|
||||
int i = blockDim.x*blockIdx.x + threadIdx.x;
|
||||
|
||||
if(i<size) {
|
||||
value_type k0, k1;
|
||||
dnnType k0, k1;
|
||||
|
||||
if (input[i]>0)
|
||||
k0 = 1.0f;
|
||||
@@ -28,7 +28,7 @@ void activation_elu(value_type *input, value_type *output, int size) {
|
||||
/**
|
||||
ELU activation function
|
||||
*/
|
||||
void activationELUForward(value_type* srcData, value_type* dstData, int size)
|
||||
void activationELUForward(dnnType* srcData, dnnType* dstData, int size)
|
||||
{
|
||||
int blocks = (size+255)/256;
|
||||
int threads = 256;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "kernels.h"
|
||||
|
||||
__global__
|
||||
void activation_leaky(value_type *input, value_type *output, int size) {
|
||||
void activation_leaky(dnnType *input, dnnType *output, int size) {
|
||||
|
||||
int i = blockDim.x*blockIdx.x + threadIdx.x;
|
||||
|
||||
@@ -17,7 +17,7 @@ void activation_leaky(value_type *input, value_type *output, int size) {
|
||||
/**
|
||||
ELU activation function
|
||||
*/
|
||||
void activationLEAKYForward(value_type* srcData, value_type* dstData, int size)
|
||||
void activationLEAKYForward(dnnType* srcData, dnnType* dstData, int size)
|
||||
{
|
||||
int blocks = (size+255)/256;
|
||||
int threads = 256;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "kernels.h"
|
||||
|
||||
__global__
|
||||
void activation_logistic(value_type *input, value_type *output, int size) {
|
||||
void activation_logistic(dnnType *input, dnnType *output, int size) {
|
||||
|
||||
int i = blockDim.x*blockIdx.x + threadIdx.x;
|
||||
|
||||
@@ -14,7 +14,7 @@ void activation_logistic(value_type *input, value_type *output, int size) {
|
||||
/**
|
||||
LOGISTIC activation function
|
||||
*/
|
||||
void activationLOGISTICForward(value_type* srcData, value_type* dstData, int size)
|
||||
void activationLOGISTICForward(dnnType* srcData, dnnType* dstData, int size)
|
||||
{
|
||||
int blocks = (size+255)/256;
|
||||
int threads = 256;
|
||||
|
||||
@@ -35,7 +35,7 @@ __global__ void reorg_kernel(int N, float *x, int w, int h, int c, int batch, in
|
||||
/**
|
||||
reorg function function
|
||||
*/
|
||||
void reorgForward(value_type* srcData, value_type* dstData,
|
||||
void reorgForward(dnnType* srcData, dnnType* dstData,
|
||||
int n, int c, int h, int w, int stride) {
|
||||
|
||||
int size = n*c*h*w;
|
||||
|
||||
@@ -41,8 +41,8 @@ public:
|
||||
|
||||
virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override {
|
||||
|
||||
activationLEAKYForward((value_type*)reinterpret_cast<const value_type*>(inputs[0]),
|
||||
reinterpret_cast<value_type*>(outputs[0]), size);
|
||||
activationLEAKYForward((dnnType*)reinterpret_cast<const dnnType*>(inputs[0]),
|
||||
reinterpret_cast<dnnType*>(outputs[0]), size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,10 +44,10 @@ public:
|
||||
|
||||
virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override {
|
||||
|
||||
value_type *srcData = (value_type*)reinterpret_cast<const value_type*>(inputs[0]);
|
||||
value_type *dstData = reinterpret_cast<value_type*>(outputs[0]);
|
||||
dnnType *srcData = (dnnType*)reinterpret_cast<const dnnType*>(inputs[0]);
|
||||
dnnType *dstData = reinterpret_cast<dnnType*>(outputs[0]);
|
||||
|
||||
checkCuda( cudaMemcpy(dstData, srcData, batchSize*c*h*w*sizeof(value_type), cudaMemcpyDeviceToDevice));
|
||||
checkCuda( cudaMemcpy(dstData, srcData, batchSize*c*h*w*sizeof(dnnType), cudaMemcpyDeviceToDevice));
|
||||
|
||||
for (int b = 0; b < batchSize; ++b){
|
||||
for(int n = 0; n < num; ++n){
|
||||
|
||||
@@ -40,8 +40,8 @@ public:
|
||||
|
||||
virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override {
|
||||
|
||||
reorgForward((value_type*)reinterpret_cast<const value_type*>(inputs[0]),
|
||||
reinterpret_cast<value_type*>(outputs[0]),
|
||||
reorgForward((dnnType*)reinterpret_cast<const dnnType*>(inputs[0]),
|
||||
reinterpret_cast<dnnType*>(outputs[0]),
|
||||
batchSize, c, h, w, stride);
|
||||
return 0;
|
||||
}
|
||||
|
||||
+23
-23
@@ -14,7 +14,7 @@ void printCenteredTitle(const char *title, char fill, int dim) {
|
||||
}
|
||||
|
||||
|
||||
void readBinaryFile(const char* fname, int size, value_type** data_h, value_type** data_d, int seek)
|
||||
void readBinaryFile(const char* fname, int size, dnnType** data_h, dnnType** data_d, int seek)
|
||||
{
|
||||
std::ifstream dataFile (fname, std::ios::in | std::ios::binary);
|
||||
std::stringstream error_s;
|
||||
@@ -25,11 +25,11 @@ void readBinaryFile(const char* fname, int size, value_type** data_h, value_type
|
||||
}
|
||||
|
||||
if(seek != 0) {
|
||||
dataFile.seekg(seek*sizeof(value_type), dataFile.cur);
|
||||
dataFile.seekg(seek*sizeof(dnnType), dataFile.cur);
|
||||
}
|
||||
|
||||
int size_b = size*sizeof(value_type);
|
||||
*data_h = new value_type[size];
|
||||
int size_b = size*sizeof(dnnType);
|
||||
*data_h = new dnnType[size];
|
||||
if (!dataFile.read ((char*) *data_h, size_b))
|
||||
{
|
||||
error_s << "Error reading file " << fname;
|
||||
@@ -40,13 +40,13 @@ void readBinaryFile(const char* fname, int size, value_type** data_h, value_type
|
||||
checkCuda( cudaMemcpy(*data_d, *data_h, size_b, cudaMemcpyHostToDevice) );
|
||||
}
|
||||
|
||||
void printDeviceVector(int size, value_type* vec_d, bool device)
|
||||
void printDeviceVector(int size, dnnType* vec_d, bool device)
|
||||
{
|
||||
value_type *vec;
|
||||
dnnType *vec;
|
||||
if(device) {
|
||||
vec = new value_type[size];
|
||||
vec = new dnnType[size];
|
||||
cudaDeviceSynchronize();
|
||||
cudaMemcpy(vec, vec_d, size*sizeof(value_type), cudaMemcpyDeviceToHost);
|
||||
cudaMemcpy(vec, vec_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost);
|
||||
} else {
|
||||
vec = vec_d;
|
||||
}
|
||||
@@ -60,17 +60,17 @@ void printDeviceVector(int size, value_type* vec_d, bool device)
|
||||
delete [] vec;
|
||||
}
|
||||
|
||||
int checkResult(int size, value_type *data_d, value_type *correct_d, bool device) {
|
||||
int checkResult(int size, dnnType *data_d, dnnType *correct_d, bool device) {
|
||||
|
||||
value_type *data_h, *correct_h;
|
||||
dnnType *data_h, *correct_h;
|
||||
const float eps = 0.0001f;
|
||||
|
||||
if(device) {
|
||||
data_h = new value_type[size];
|
||||
correct_h = new value_type[size];
|
||||
data_h = new dnnType[size];
|
||||
correct_h = new dnnType[size];
|
||||
cudaDeviceSynchronize();
|
||||
cudaMemcpy(data_h, data_d, size*sizeof(value_type), cudaMemcpyDeviceToHost);
|
||||
cudaMemcpy(correct_h, correct_d, size*sizeof(value_type), cudaMemcpyDeviceToHost);
|
||||
cudaMemcpy(data_h, data_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost);
|
||||
cudaMemcpy(correct_h, correct_d, size*sizeof(dnnType), cudaMemcpyDeviceToHost);
|
||||
|
||||
} else {
|
||||
data_h = data_d;
|
||||
@@ -103,30 +103,30 @@ int checkResult(int size, value_type *data_d, value_type *correct_d, bool device
|
||||
return diffs;
|
||||
}
|
||||
|
||||
void resize(int size, value_type **data)
|
||||
void resize(int size, dnnType **data)
|
||||
{
|
||||
if (*data != NULL)
|
||||
checkCuda( cudaFree(*data) );
|
||||
checkCuda( cudaMalloc(data, size*sizeof(value_type)) );
|
||||
checkCuda( cudaMalloc(data, size*sizeof(dnnType)) );
|
||||
}
|
||||
|
||||
void matrixTranspose(cublasHandle_t handle, value_type* srcData, value_type* dstData, int rows, int cols) {
|
||||
void matrixTranspose(cublasHandle_t handle, dnnType* srcData, dnnType* dstData, int rows, int cols) {
|
||||
|
||||
value_type *A = srcData, *clone = dstData;
|
||||
dnnType *A = srcData, *clone = dstData;
|
||||
int m = rows, n= cols;
|
||||
checkCuda( cudaMemcpy(clone, A, m*n*sizeof(value_type), cudaMemcpyDeviceToDevice));
|
||||
checkCuda( cudaMemcpy(clone, A, m*n*sizeof(dnnType), cudaMemcpyDeviceToDevice));
|
||||
|
||||
float const alpha(1.0);
|
||||
float const beta(0.0);
|
||||
checkERROR( cublasSgeam( handle, CUBLAS_OP_T, CUBLAS_OP_N, m, n, &alpha, A, n, &beta, A, m, clone, m ));
|
||||
}
|
||||
|
||||
void matrixMulAdd( cublasHandle_t handle, value_type* srcData, value_type* dstData,
|
||||
value_type* add_vector, int dim, value_type mul) {
|
||||
void matrixMulAdd( cublasHandle_t handle, dnnType* srcData, dnnType* dstData,
|
||||
dnnType* add_vector, int dim, dnnType mul) {
|
||||
|
||||
checkCuda( cudaMemcpy(dstData, add_vector, dim*sizeof(value_type), cudaMemcpyDeviceToDevice));
|
||||
checkCuda( cudaMemcpy(dstData, add_vector, dim*sizeof(dnnType), cudaMemcpyDeviceToDevice));
|
||||
|
||||
value_type alpha = mul;
|
||||
dnnType alpha = mul;
|
||||
checkERROR( cublasSaxpy(handle, dim, &alpha, srcData, 1, dstData, 1));
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user