cvt_color.h 7.8 KB
Newer Older
1 2 3 4
/**
 * \file dnn/test/common/cvt_color.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
 *
 * 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.
 */
#pragma once

#include "megdnn/basic_types.h"
#include "megdnn/opr_param_defs.h"

namespace megdnn {
namespace test {
namespace cvt_color {

struct TestArg {
    param::CvtColor param;
    TensorShape src;
    DType dtype;
    TestArg(param::CvtColor param, TensorShape src, DType dtype)
            : param(param), src(src), dtype(dtype) {}
};

inline std::vector<TestArg> get_args() {
    std::vector<TestArg> args;
    using Mode = param::CvtColor::Mode;
    param::CvtColor cur_param;
    for (size_t i = 2; i <= 10; ++i) {
        for (size_t j = 2; j <= 10; ++j) {
            cur_param.mode = Mode::RGB2GRAY;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::RGB2YUV;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::YUV2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::GRAY2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 1},
                              dtype::Uint8());
            cur_param.mode = Mode::RGBA2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 4},
                              dtype::Uint8());
            cur_param.mode = Mode::RGBA2BGR;
            args.emplace_back(cur_param, TensorShape{1, i, j, 4},
                              dtype::Uint8());
            cur_param.mode = Mode::RGBA2GRAY;
            args.emplace_back(cur_param, TensorShape{1, i, j, 4},
                              dtype::Uint8());
            cur_param.mode = Mode::RGB2BGR;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::BGR2GRAY;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::BGR2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            // float32 test
            cur_param.mode = Mode::RGB2GRAY;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Float32());
            cur_param.mode = Mode::RGB2YUV;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Float32());
            cur_param.mode = Mode::YUV2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Float32());
            cur_param.mode = Mode::GRAY2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 1},
                              dtype::Float32());
        }
    }
    for (size_t i = 18; i < 164; i *= 3) {
        for (auto mode : {
                     Mode::YUV2GRAY_NV21,       Mode::YUV2BGR_NV21,
                     Mode::YUV2RGB_NV21,        Mode::YUV2GRAY_NV12,
                     Mode::YUV2BGR_NV12,        Mode::YUV2RGB_NV12,
                     Mode::YUV2GRAY_YV12,       Mode::YUV2BGR_YV12,
                     Mode::YUV2RGB_YV12,        Mode::YUV2GRAY_YU12,
                     Mode::YUV2BGR_YU12,        Mode::YUV2RGB_YU12,

                     Mode::BT601_YUV2GRAY_NV21, Mode::BT601_YUV2BGR_NV21,
                     Mode::BT601_YUV2RGB_NV21,  Mode::BT601_YUV2GRAY_NV12,
                     Mode::BT601_YUV2BGR_NV12,  Mode::BT601_YUV2RGB_NV12,
                     Mode::BT601_YUV2GRAY_YV12, Mode::BT601_YUV2BGR_YV12,
                     Mode::BT601_YUV2RGB_YV12,  Mode::BT601_YUV2GRAY_YU12,
                     Mode::BT601_YUV2BGR_YU12,  Mode::BT601_YUV2RGB_YU12,

             }) {
            cur_param.mode = mode;
            args.emplace_back(cur_param, TensorShape{1, i, i, 1},
                              dtype::Uint8());
        }
    }

    //! test case for nv12(nv21), which height is not even, height % 3 == 0,
    //! height % 2 == 1
    for (auto mode : {
                 Mode::YUV2GRAY_NV21,
                 Mode::YUV2BGR_NV21,
                 Mode::YUV2RGB_NV21,
                 Mode::YUV2GRAY_NV12,
                 Mode::YUV2BGR_NV12,
                 Mode::YUV2RGB_NV12,

                 Mode::BT601_YUV2GRAY_NV21,
                 Mode::BT601_YUV2BGR_NV21,
                 Mode::BT601_YUV2RGB_NV21,
                 Mode::BT601_YUV2GRAY_NV12,
                 Mode::BT601_YUV2BGR_NV12,
                 Mode::BT601_YUV2RGB_NV12,

         }) {
        cur_param.mode = mode;
        args.emplace_back(cur_param, TensorShape{1, 3, 18, 1}, dtype::Uint8());
        args.emplace_back(cur_param, TensorShape{1, 9, 18, 1}, dtype::Uint8());
    }

    return args;
}

inline std::vector<TestArg> get_cuda_args() {
    std::vector<TestArg> args;
    using Mode = param::CvtColor::Mode;
    param::CvtColor cur_param;

    for (size_t i = 2; i <= 10; ++i) {
        for (size_t j = 2; j <= 10; ++j) {
            cur_param.mode = Mode::RGB2GRAY;
136 137 138
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::BGR2GRAY;
139 140 141 142 143 144 145 146 147 148 149 150 151
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::RGB2YUV;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::YUV2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Uint8());
            cur_param.mode = Mode::GRAY2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 1},
                              dtype::Uint8());
            // float32 test
            cur_param.mode = Mode::RGB2GRAY;
152 153 154
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Float32());
            cur_param.mode = Mode::BGR2GRAY;
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
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Float32());
            cur_param.mode = Mode::RGB2YUV;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Float32());
            cur_param.mode = Mode::YUV2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 3},
                              dtype::Float32());
            cur_param.mode = Mode::GRAY2RGB;
            args.emplace_back(cur_param, TensorShape{1, i, j, 1},
                              dtype::Float32());
            args.emplace_back(cur_param, TensorShape{3, i, j, 1},
                              dtype::Float32());
        }
    }

    for (size_t i = 18; i < 164; i *= 3) {
        for (auto mode : {
                     Mode::YUV2GRAY_NV21,
                     Mode::YUV2BGR_NV21,
                     Mode::YUV2RGB_NV21,
                     Mode::YUV2GRAY_NV12,
                     Mode::YUV2BGR_NV12,
                     Mode::YUV2RGB_NV12,
                     Mode::YUV2GRAY_YV12,
                     Mode::YUV2BGR_YV12,
                     Mode::YUV2RGB_YV12,
                     Mode::YUV2GRAY_YU12,
                     Mode::YUV2BGR_YU12,
                     Mode::YUV2RGB_YU12,
             }) {
            cur_param.mode = mode;
            args.emplace_back(cur_param, TensorShape{1, i, i, 1},
                              dtype::Uint8());
        }
    }

    return args;
}

}  // namespace cvt_color
}  // namespace test
}  // namespace megdnn

// vim: syntax=cpp.doxygen