-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpreprocess.cu
51 lines (39 loc) · 1.51 KB
/
preprocess.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "cuda_utils.h"
using namespace std;
// preprocess (NHWC->NCHW, BGR->RGB, [0, 255]->[0, 1](Normalize))
__global__ void preprocess_kernel( float* output, uint8_t* input,
const int batchSize, const int height, const int width, const int channel,
const int thread_count)
{
int index = threadIdx.x + blockIdx.x * blockDim.x;
if (index >= thread_count) return;
const int w_idx = index % width;
int idx = index / width;
const int h_idx = idx % height;
idx /= height;
const int c_idx = idx % channel;
const int b_idx = idx / channel;
int g_idx = b_idx * height * width * channel + h_idx * width * channel + w_idx * channel + 2 - c_idx;
output[index] = input[g_idx] / 255.f;
}
void preprocess(float* output, uint8_t*input, int batchSize, int height, int width, int channel, cudaStream_t stream)
{
int thread_count = batchSize * height * width * channel;
int block = 512;
int grid = (thread_count - 1) / block + 1;
preprocess_kernel << <grid, block, 0, stream >> > (output, input, batchSize, height, width, channel, thread_count);
}
#include "preprocess.hpp"
namespace nvinfer1
{
int PreprocessPluginV2::enqueue(int batchSize, const void* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
{
uint8_t* input = (uint8_t*)inputs[0];
float* output = (float*)outputs[0];
const int H = mPreprocess.H;
const int W = mPreprocess.W;
const int C = mPreprocess.C;
preprocess(output, input, batchSize, H, W, C, stream);
return 0;
}
}