-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpostprocess.cu
54 lines (42 loc) · 1.55 KB
/
postprocess.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
52
53
54
#include "cuda_utils.h"
using namespace std;
// postprocess (NCHW->NHWC, RGB->BGR, *255, ROUND, uint8)
__global__ void postprocess_kernel(uint8_t* output, float* 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 c_idx = index % channel;
int idx = index / channel;
const int w_idx = idx % width;
idx /= width;
const int h_idx = idx % height;
const int b_idx = idx / height;
int g_idx = b_idx * height * width * channel + (2 - c_idx)* height * width + h_idx * width + w_idx;
float tt = input[g_idx] * 255.f;
if (tt > 255)
tt = 255;
output[index] = tt;
}
void postprocess(uint8_t* output, float*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;
postprocess_kernel << <grid, block, 0, stream >> > (output, input, batchSize, height, width, channel, thread_count);
}
#include "postprocess.hpp"
namespace nvinfer1
{
int PostprocessPluginV2::enqueue(int batchSize, const void* const* inputs, void* const* outputs, void* workspace, cudaStream_t stream) noexcept
{
float* input = (float*)inputs[0];
uint8_t* output = (uint8_t*)outputs[0];
const int H = mPostprocess.H;
const int W = mPostprocess.W;
const int C = mPostprocess.C;
postprocess(output, input, batchSize, H, W, C, stream);
return 0;
}
}