Skip to content

Commit 2e53f0c

Browse files
author
Huang ZhuangZhuang
authored
update infer preprocessing (wang-xinyu#623)
There are some problems with the preprocessing on LPRnet when infer, which will cause the predicted output results to be inconsistent with the pytorch version; as shown, the preprocessing has been updated here
1 parent b7a754e commit 2e53f0c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lprnet/LPRnet.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,19 @@ int main(int argc, char **argv) {
399399
cv::resize(img, pr_img, cv::Size(INPUT_W, INPUT_H), 0, 0, cv::INTER_CUBIC);
400400
// For multi-batch, I feed the same image multiple times.
401401
// If you want to process different images in a batch, you need adapt it.
402-
cv::Mat blob = cv::dnn::blobFromImage(pr_img, 0.0078125, pr_img.size(), cv::Scalar(127.5, 127.5, 127.5), true,
403-
false);
402+
//cv::Mat blob = cv::dnn::blobFromImage(pr_img, 0.0078125, pr_img.size(), cv::Scalar(127.5, 127.5, 127.5), true,
403+
//false);
404+
int i = 0;
405+
for (int row = 0; row < INPUT_H; ++row) {
406+
uchar* uc_pixel = pr_img.data + row * pr_img.step;
407+
for (int col = 0; col < INPUT_W; ++col) {
408+
data[i + 2 * INPUT_H * INPUT_W] = ((float)uc_pixel[2] - 127.5)*0.0078125;
409+
data[i + INPUT_H * INPUT_W] = ((float)uc_pixel[1]-127.5)*0.0078125;
410+
data[i] = ((float)uc_pixel[0]-127.5)*0.0078125;
411+
uc_pixel += 3;
412+
++i;
413+
}
414+
}
404415

405416
IRuntime *runtime = createInferRuntime(gLogger);
406417
assert(runtime != nullptr);
@@ -413,7 +424,7 @@ int main(int argc, char **argv) {
413424
// Run inference
414425
static float prob[BATCH_SIZE * OUTPUT_SIZE];
415426
auto start = std::chrono::system_clock::now();
416-
doInference(*context, blob.ptr<float>(0), prob, BATCH_SIZE);
427+
doInference(*context, data, prob, BATCH_SIZE);
417428
auto end = std::chrono::system_clock::now();
418429
std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "us" << std::endl;
419430
std::vector<int> preds;

0 commit comments

Comments
 (0)