warp_perspective.cpp 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**
 * \file dnn/test/aarch64/warp_perspective.cpp
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
 * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#include <string>
#include <vector>
#include "test/aarch64/fixture.h"

#include "test/common/benchmarker.h"
#include "test/common/checker.h"
#include "test/common/random_state.h"
#include "test/common/rng.h"

#include "test/common/warp_perspective.h"

namespace megdnn {
namespace test {

TEST_F(AARCH64, WARP_PERSPECTIVE_CV) {
    //! Just for the format NHWC
28
    Checker<WarpPerspective, WarpPerspectiveMatIdxProxy> checker(handle());
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    param::WarpPerspective param;
    class ResizeMatRNG : public RNG {
        void gen(const TensorND& tensor_) override {
            auto& gen = RandomState::generator();
            std::uniform_real_distribution<dt_float32> pdist3(1.9f, 3.1f);
            std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
            std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
            std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
            std::uniform_real_distribution<dt_float32> ndist3(-3.1f, -1.9f);
            std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
            std::uniform_int_distribution<int> dice(0, 5);
            float* ptr = tensor_.ptr<dt_float32>();
            auto N = tensor_.layout.shape[0];
            for (size_t n = 0; n < N; ++n) {
                for (size_t i = 0; i < 9; ++i) {
                    switch (dice(gen)) {
                        case 0:
                            ptr[i] = pdist3(gen);
                            break;
                        case 1:
                            ptr[i] = pdist(gen);
                            break;
                        case 2:
                            ptr[i] = pdisth(gen);
                            break;
                        case 3:
                            ptr[i] = ndist(gen);
                            break;
                        case 4:
                            ptr[i] = ndist3(gen);
                            break;
                        case 5:
                            ptr[i] = ndisth(gen);
                            break;
                    }
                }
                // is resize?
                if (n & 1) {
                    ptr[1] = 0;
                    ptr[3] = 0;
                    ptr[6] = ptr[7] = 0;
                }
                ptr += 9;
            }
        }
    } rng;

    using BMode = param::WarpPerspective::BorderMode;
    param.format = param::WarpPerspective::Format::NHWC;
    // add for nearest test
    param.imode = param::WarpPerspective::InterpolationMode::NEAREST;
    for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
                      BMode::WRAP, BMode::CONSTANT}) {
        param.bmode = mode;
        param.border_val = 1.737;
        checker.set_param(param);
85 86 87 88
        UniformIntRNG rng(0, 1);
        checker.set_rng(2, &rng);
        checker.set_dtype(2, dtype::Int32());
        checker.exec({{2, 5, 5, 1}, {4, 3, 3}, {4}, {4, 5, 5, 1}});
89 90 91 92 93 94 95 96
    }
    // resize nan case
    UniformFloatRNG rng_zero(0, 0);
    checker.set_rng(1, &rng_zero);
    {
        param.bmode = BMode::CONSTANT;
        param.border_val = 1.737;
        checker.set_param(param);
97 98 99 100 101
        UniformIntRNG rng(0, 999);
        checker.set_rng(2, &rng);
        checker.set_dtype(2, dtype::Int32());
        checker.exec(
                {{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
102 103 104 105 106 107 108 109 110
    }

    // add linear test
    param.imode = param::WarpPerspective::InterpolationMode::INTER_LINEAR;
    for (auto mode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
                      BMode::WRAP, BMode::CONSTANT}) {
        param.bmode = mode;
        param.border_val = 1.737;
        checker.set_param(param);
111 112 113 114
        UniformIntRNG rng(0, 9);
        checker.set_rng(2, &rng);
        checker.set_dtype(2, dtype::Int32());
        checker.exec({{10, 128, 108, 3}, {20, 3, 3}, {20}, {20, 56, 128, 3}});
115 116 117 118 119 120 121
    }
    // resize nan case
    checker.set_rng(1, &rng_zero);
    {
        param.bmode = BMode::CONSTANT;
        param.border_val = 1.737;
        checker.set_param(param);
122 123 124 125 126
        UniformIntRNG rng(0, 999);
        checker.set_rng(2, &rng);
        checker.set_dtype(2, dtype::Int32());
        checker.exec(
                {{1000, 2, 10, 3}, {2000, 3, 3}, {2000}, {2000, 2, 12, 3}});
127 128 129 130
    }

    auto args = warp_perspective::get_cv_args();
    for (auto&& arg : args) {
131
        ConstValue rng(0.f);
132
        checker.set_param(arg.param)
133
                .set_rng(2, &rng)
134 135
                .set_dtype(0, dtype::Uint8())
                .set_dtype(1, dtype::Float32())
136 137 138
                .set_dtype(2, dtype::Int32())
                .set_dtype(3, dtype::Uint8())
                .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
139 140 141
    }

    for (auto&& arg : args) {
142
        ConstValue rng(0.f);
143
        checker.set_param(arg.param)
144
                .set_rng(2, &rng)
145 146
                .set_dtype(0, dtype::Float32())
                .set_dtype(1, dtype::Float32())
147 148 149
                .set_dtype(2, dtype::Int32())
                .set_dtype(3, dtype::Float32())
                .execs({arg.src, arg.trans, arg.mat_idx, arg.dst});
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    }
}

#if MEGDNN_WITH_BENCHMARK
TEST_F(AARCH64, BENCHMARK_WARP_PERSPECTIVE_FORWARD) {
    Benchmarker<WarpPerspectiveForward> benchmarker(handle());
    auto handle_naive = create_cpu_handle(2);
    Benchmarker<WarpPerspectiveForward> benchmarker_naive(handle_naive.get());
    constexpr size_t NR_RUN = 50;

    using BMode = param::WarpPerspective::BorderMode;
    using IMode = param::WarpPerspective::InterpolationMode;

    WarpPerspective::Param param;
    param.border_val = 0.3f;
    param.format = param::WarpPerspective::Format::NHWC;

    auto run = [&](size_t N, size_t C, size_t IH, size_t IW, size_t OH,
                   size_t OW, size_t scale) {
        printf("src={%zu, %zu, %zu, %zu}, dst={%zu, %zu, %zu, %zu}\n", N, IH,
               IW, C, N, OH, OW, C);
        auto time_ms =
                benchmarker.exec({{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) /
                NR_RUN;
        auto time_naive_ms =
                benchmarker_naive.exec(
                        {{N, IH, IW, C}, {N, 3, 3}, {N, OH, OW, C}}) /
                NR_RUN;
        auto bandwidth = N * C * (scale * OH * OW) * dtype::Float32().size();
        printf("aarch64: %.3f, perf: %.3f GBPS naive: %.3f, perf %.3f GBPS "
               "speedup: %f\n",
               time_ms, bandwidth / time_ms / 1e6, time_naive_ms,
               bandwidth / time_naive_ms / 1e6, time_naive_ms / time_ms);
    };

    std::vector<std::string> bmodestringmap = {
            "REPLICATE", "REFLECT", "REFLECT_101", "WARP", "CONSTANT"};

    std::vector<std::string> imodestringmap = {"NEAREST", "INTER_LINEAR"};
    size_t scales[2] = {2, 5};

    for (auto imode : {IMode::NEAREST, IMode::INTER_LINEAR}) {
        for (auto bmode : {BMode::REFLECT_101, BMode::REPLICATE, BMode::REFLECT,
                           BMode::WRAP, BMode::CONSTANT}) {
            param.imode = imode;
            param.bmode = bmode;
            benchmarker.set_param(param).set_display(false).set_times(NR_RUN);
            benchmarker_naive.set_param(param).set_display(false).set_times(
                    NR_RUN);
            size_t scale = scales[(int)imode];
            printf("\n\n\n warpperspective InterpolationMode::%s "
                   "BorderMode::%s start\n",
                   imodestringmap[(int)imode].c_str(),
                   bmodestringmap[(int)bmode].c_str());
            for (auto&& shape :
                 std::vector<std::pair<size_t, size_t>>{{700, 490},
                                                        {500, 334},
                                                        {472, 342},
                                                        {448, 306},
                                                        {626, 412},
                                                        {140, 144},
                                                        {120, 128},
                                                        {180, 176}}) {
                for (size_t ch : {1, 2, 3}) {
                    run(1, ch, shape.first, shape.second, 256, 256, scale);
                }
            }
        }
    }
}

#endif

}  // namespace test
}  // namespace megdnn

// vim: syntax=cpp.doxygen