图片预处理的高性能实现
int input_batch = 1;
int input_channel = 3;
int input_height = 224;
int input_width = 224;
int input_numel = input_batch * input_channel * input_height * input_width;
float* input_data_host = nullptr;
float* input_data_device = nullptr;
checkRuntime(cudaMallocHost(&input_data_host, input_numel * sizeof(float)));
checkRuntime(cudaMalloc(&input_data_device, input_numel * sizeof(float)));
///////////////////////////////////////////////////
// image to float
auto image = cv::imread("dog.jpg");
float mean[] = {0.406, 0.456, 0.485};
float std[] = {0.225, 0.224, 0.229};
// 对应于pytorch的代码部分
cv::resize(image, image, cv::Size(input_width, input_height));
// opencv读取的图片数据为BRG,模型的输入是RGB,因此需要转换一次
// BRG的图片排列格式是:BGRBGRBGR... -> BBBB....GGGG....RRRR....
// 这里采用指针的方式,把数据转换同时对其进行归一化
int image_area = image.cols * image.rows;
unsigned char* pimage = image.d