using namespace cv;
Mat MatBGRImage = imread("wokspace/my_src.jpg");
auto t0 = iLogger::timestamp_now_float();
Mat RGBImg, ResizeImg;
cvtColor(MatBGRImage, RGBImg, COLOR_BGR2RGB);
cv::resize(RGBImg, ResizeImg, Size(224, 224));
// mean_rgb = [0.485, 0.456, 0.406]
// std_rgb = [0.229, 0.224, 0.225]
int channels = ResizeImg.channels(), height = ResizeImg.rows, width = ResizeImg.cols;
float* nchwMat = (float*)malloc(channels * height * width * sizeof(float));
memset(nchwMat, 0, channels * height * width * sizeof(float));
// Convert HWC to CHW and Normalize
float mean_rgb[3] = {0.485, 0.456, 0.406};
float std_rgb[3] = {0.229, 0.224, 0.225};
uint8_t* ptMat = ResizeImg.ptr<uint8_t>(0);
int area = height * width;
for (int c = 0; c < channels; ++c)
{
for (int h = 0; h < height; ++h)
{
for (int w = 0; w < width; ++w)
{
int srcIdx = c * area + h * width + w;
int divider = srcIdx / 3; // 0, 1, 2
for (int i = 0; i < 3; ++i)
{
nchwMat[divider + i * area] = static_cast<float>((ptMat[srcIdx] * 1.0f/255.0f - mean_rgb[i]) * 1.0f/std_rgb[i] );
}
}
}
}
// 复制到GPU
size_t Src_size = 3 * 224 * 224 * sizeof(float);
for (int i = 0; i < explicit_batch; ++i)
{
cudaMemcpy(static_cast<float*>(input_tensor_image->pValue + i * Src_size),
nchwMat, Src_size, cudaMemcpyHostToDevice);
}
free(nchwMat);
auto t1 = iLogger::timestamp_now_float();
auto ms0 = t1 - t0;
printf("preprocess time: %.3f ms\n", ms0);
网友评论