1 //
2 //  CropAndResizeTest.cpp
3 //  MNNTests
4 //
5 //  Created by MNN on 2021/03/11.
6 //  Copyright © 2018, Alibaba Group Holding Limited
7 //
8 #include <cfenv>
9 #include <cmath>
10 #include <random>
11 
12 #include <MNN/expr/Expr.hpp>
13 #include <MNN/expr/ExprCreator.hpp>
14 
15 #define MNN_OPEN_TIME_TRACE
16 #include <MNN/AutoTime.hpp>
17 
18 #include "MNNTestSuite.h"
19 #include "TestUtils.h"
20 
21 using namespace MNN::Express;
22 
23 #define BATCH 8
24 #define DEPTH 4
25 #define WIDTH 720
26 #define HEIGHT 720
27 #define TIME 10
28 
29 class GridSampleSpeed : public MNNTestCase {
30 public:
31     virtual ~GridSampleSpeed() = default;
32 
run(int precision)33     virtual bool run(int precision) {
34         const int batch = BATCH;
35         const int inHeight = HEIGHT;
36         const int inWidth = WIDTH;
37         const int outHeight = HEIGHT;
38         const int outWidth = WIDTH;
39         const int depth = DEPTH;
40         auto input = _Input({batch, depth, inHeight, inWidth}, NCHW);
41         auto grid = _Input({batch, outHeight, outWidth, 2}, NHWC);
42 
43         std::vector<InterpolationMethod> modes({BILINEAR});
44         std::vector<GridSamplePaddingMode> paddingModes({GRID_SAMPLE_PADDING_ZEROS});
45         std::vector<bool> alignCornersVec({false});
46 
47         std::vector<float> expectedOutput(batch * outHeight * outWidth * depth);
48         for (auto mode : modes) {
49             std::string modeStr = mode == BILINEAR ? "bilinear" : "nearest";
50             for (auto paddingMode : paddingModes) {
51                 std::string paddingModeStr = paddingMode == GRID_SAMPLE_PADDING_ZEROS ?
52                                              "zeros" : (paddingMode == GRID_SAMPLE_PADDING_BORDER ? "border"
53                                                                                                   : "reflection");
54                 for (auto alignCorners : alignCornersVec) {
55                     std::string alignCornersStr = alignCorners ? "true" : "false";
56 
57 //                    grid->unMap();
58 //                    input->unMap();
59 //                    input = _Convert(input, NC4HW4);
60                     auto output = _GridSample(input, grid, mode, paddingMode, alignCorners);
61                     MNN_PRINT("Test GridSample for NCHW (%d, %d, %d, %d) x %d with setting %s %s %s \n",
62                               BATCH, DEPTH, HEIGHT, WIDTH, TIME,
63                               modeStr.c_str(), paddingModeStr.c_str(), alignCornersStr.c_str());
64                     {
65                         AUTOTIME;
66                         for (int i = 0; i < TIME; ++i) {
67                             auto inputPtr = input->writeMap<float>();
68                             auto gridPtr = grid->writeMap<float>();
69 
70                             output->readMap<float>();
71                         }
72                     }
73                 }
74             }
75         }
76         return true;
77     }
78 };
79 
80 MNNTestSuiteRegister(GridSampleSpeed, "speed/GridSample");
81