Hello Mat

 找回密码
 立即注册
查看: 424|回复: 0

Halide模板匹配

[复制链接]

1306

主题

1532

帖子

114

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22685
发表于 2024-5-9 23:18:02 | 显示全部楼层 |阅读模式
Halide模板匹配
  1. // windows-20240425
  2. // WITIAITemplateMatching_generate -o . -g TemplateWitiai -f TemplateWitiai -e static_library,h,schedule target=host
  3. // WITIAITemplateMatching_generate -o . -g TemplateWitiai -f TemplateWitiai -e static_library,h,schedule -p autoschedule_mullapudi2016.dll target=host autoscheduler=Mullapudi2016 autoscheduler.parallelism=32 autoscheduler.last_level_cache_size=16777216 autoscheduler.balance=40
  4. #include "Halide.h"
  5. #include <stdio.h>
  6. using namespace Halide;
  7. // We will define a generator to auto-schedule.
  8. class TemplateWitiaiMain : public Halide::Generator<TemplateWitiaiMain> {
  9. public:
  10.         Input<Buffer<uint8_t, 3>> input1{ "input1" };
  11.         Input<Buffer<uint8_t, 3>> templ{ "templ" };
  12.         Output<Buffer<float, 2>> output1{ "output1" };
  13.         Output<Buffer<float, 2>> output2{ "output2" };
  14.         void generate()
  15.         {
  16.                 //input1.dim(0).set_stride(3);  // stride in dimension 0 (x) is three
  17.                 //input1.dim(1).set_stride(3);  // stride in dimension 1 (y) is three
  18.                 input1.dim(0).set_bounds(0, input1.width());
  19.                 input1.dim(1).set_bounds(0, input1.height());
  20.                 //input1_1(x, y) = input1(x, y, 0);
  21.                 //input_16(x, y) = cast<uint16_t>(input1_1(x, y));
  22.                 //limit = BoundaryConditions::constant_exterior(input_16, 0, 0, input1.width(), 0, input1.height());
  23.                 limit = BoundaryConditions::repeat_edge(input1);
  24.                 input1_1(x, y) = limit(x, y, 0);
  25.                 input_16(x, y) = cast<float>(input1_1(x, y));
  26.                 templ_1(x, y) = templ(x, y, 0);
  27.                 temp_16(x, y) = cast<float>(templ_1(x, y));
  28.                 RDom matchDom(0, templ.width(), 0, templ.height());
  29.                 //matchDom.where(matchDom.x + matchDom.y > 25);  // 增大步长
  30.                 //Expr strided_x = x * 2;
  31.                 //Expr strided_y = x * 2;
  32.                 score(x, y) = sum(matchDom, pow(temp_16(matchDom.x, matchDom.y) - input_16(x * 3 + matchDom.x, y * 3 + matchDom.y), 2));
  33.                 //
  34.                 RDom searchDom(0, input1.width() / 3 - templ.width(), 0, input1.height() / 3 - templ.height());
  35.                 Tuple searchBest = argmin(searchDom, score(searchDom.x, searchDom.y), "argmin");
  36.                 //searchBest = argmin(searchDom, score(searchDom.x, searchDom.y), "argmin");
  37.                 //Func bestX;
  38.                 bestX(x, y) = searchBest[0];
  39.                 //Func bestY;
  40.                 bestY(x, y) = searchBest[1];
  41.                 //Realization re = best.realize();
  42.                 //Buffer<int> x_coordinate(re[0]);
  43.                 //Buffer<int> y_coordinate(re[1]);
  44.                 //int bestX0 = bestX(0);
  45.                 //int bestY0 = bestY(0);

  46.                 //output1(x, y) = cast<uint8_t>(best(x, y));
  47.                 output1(x, y) = cast<float>(bestX(x, y));
  48.                 output2(x, y) = cast<float>(bestY(x, y));
  49.         }

  50.         void schedule()
  51.         {
  52.                 if (using_autoscheduler())
  53.                 {
  54.                         //
  55.                 }
  56.                 else
  57.                 {
  58.                         limit.compute_root();
  59.                         input1_1.compute_root();
  60.                         input_16.compute_root();
  61.                         templ_1.compute_root();
  62.                         temp_16.compute_root();
  63.                         score.vectorize(x, 8).parallel(y).compute_root();
  64.                         //score.compute_root();
  65.                         /*bestX.compute_root();
  66.                         bestY.compute_root();*/
  67.                         /*                bestX.parallel(x).parallel(y).compute_root();
  68.                                         bestY.parallel(x).parallel(y).compute_root();*/
  69.                                         /*bestX.vectorize(x, 16).parallel(y).compute_root();
  70.                                         bestY.vectorize(x, 16).parallel(y).compute_root();*/
  71.                         bestX.vectorize(x, 16).parallel(y).compute_root();
  72.                         bestY.vectorize(x, 16).parallel(y).compute_root();
  73.                         //bestX.tile(x, y, x_outer, y_outer, x_inner, y_inner, 64, 64)
  74.                         //        .fuse(x_outer, y_outer, tile_index)
  75.                         //        .vectorize(x_inner, 4)
  76.                         //        .parallel(tile_index)
  77.                         //        .compute_root();
  78.                         //bestY.tile(x, y, x_outer, y_outer, x_inner, y_inner, 64, 64)
  79.                         //        .fuse(x_outer, y_outer, tile_index)
  80.                         //        .vectorize(x_inner, 4)
  81.                         //        .parallel(tile_index)
  82.                         //        .compute_root();
  83.         /*                output1.vectorize(x, 16).parallel(y).compute_root();
  84.                         output2.vectorize(x, 16).parallel(y).compute_root();*/
  85.                         output1.compute_root();
  86.                         output2.compute_root();
  87.                 }
  88.         }

  89. private:
  90.         Var x{ "x" }, y{ "y" }, c{ "c" }, x_outer{ "x_outer" }, y_outer{ "y_outer" }, x_inner{ "x_inner" }, y_inner{ "y_inner" }, tile_index{ "tile_index" };
  91.         Func limit, input_16, input1_1, templ_1, temp_16, score, bestX, bestY;
  92.         //Tuple searchBest;
  93.         //RDom matchDom, searchDom;
  94. };

  95. // file along with tools/GenGen.cpp.
  96. HALIDE_REGISTER_GENERATOR(TemplateWitiaiMain, TemplateWitiai)
复制代码




算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-7-27 14:30 , Processed in 0.199734 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表