test_normalize.cpp 3.21 KB
Newer Older
limm's avatar
limm committed
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
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
// Copyright (c) OpenMMLab. All rights reserved.

#include "catch.hpp"
#include "mmdeploy/core/mat.h"
#include "mmdeploy/core/utils/device_utils.h"
#include "mmdeploy/preprocess/transform/transform.h"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv_utils.h"
#include "test_resource.h"
#include "test_utils.h"

using namespace mmdeploy;
using namespace framework;
using namespace mmdeploy::test;
using namespace std;

void TestNormalize(const Value &cfg, const cv::Mat &mat) {
  auto gResource = MMDeployTestResources::Get();
  for (auto const &device_name : gResource.device_names()) {
    Device device{device_name.c_str()};
    Stream stream{device};
    auto transform = CreateTransform(cfg, device, stream);
    REQUIRE(transform != nullptr);

    vector<float> mean;
    vector<float> std;
    for (auto &v : cfg["mean"]) {
      mean.push_back(v.get<float>());
    }
    for (auto &v : cfg["std"]) {
      std.push_back(v.get<float>());
    }
    bool to_rgb = cfg.value("to_rgb", false);

    auto _mat = mat.clone();
    auto ref_mat = mmdeploy::cpu::Normalize(_mat, mean, std, to_rgb);

    auto res = transform->Process({{"img", cpu::CVMat2Tensor(mat)}});
    REQUIRE(!res.has_error());
    auto res_tensor = res.value()["img"].get<Tensor>();
    REQUIRE(res_tensor.device() == device);
    REQUIRE(res_tensor.desc().data_type == DataType::kFLOAT);
    REQUIRE(ImageNormCfg(res.value(), "mean") == mean);
    REQUIRE(ImageNormCfg(res.value(), "std") == std);

    Device kHost{"cpu"};
    auto host_tensor = MakeAvailableOnDevice(res_tensor, kHost, stream);
    REQUIRE(stream.Wait());
    auto res_mat = mmdeploy::cpu::Tensor2CVMat(host_tensor.value());
    REQUIRE(mmdeploy::cpu::Compare(ref_mat, res_mat));
  }
}

TEST_CASE("transform Normalize", "[normalize]") {
  auto gResource = MMDeployTestResources::Get();
  auto img_list = gResource.LocateImageResources("transform");
  REQUIRE(!img_list.empty());

  auto img_path = img_list.front();
  cv::Mat bgr_mat = cv::imread(img_path);
  cv::Mat gray_mat;
  cv::Mat float_bgr_mat;
  cv::Mat float_gray_mat;

  cv::cvtColor(bgr_mat, gray_mat, cv::COLOR_BGR2GRAY);
  bgr_mat.convertTo(float_bgr_mat, CV_32FC3);
  gray_mat.convertTo(float_gray_mat, CV_32FC1);

  SECTION("cpu vs gpu: 3 channel mat") {
    bool to_rgb = true;
    Value cfg{{"type", "Normalize"},
              {"mean", {123.675, 116.28, 103.53}},
              {"std", {58.395, 57.12, 57.375}},
              {"to_rgb", to_rgb}};
    vector<cv::Mat> mats{bgr_mat, float_bgr_mat};
    for (auto &mat : mats) {
      TestNormalize(cfg, mat);
    }
  }

  SECTION("cpu vs gpu: 3 channel mat, to_rgb false") {
    bool to_rgb = false;
    Value cfg{{"type", "Normalize"},
              {"mean", {123.675, 116.28, 103.53}},
              {"std", {58.395, 57.12, 57.375}},
              {"to_rgb", to_rgb}};

    vector<cv::Mat> mats{bgr_mat, float_bgr_mat};
    for (auto &mat : mats) {
      TestNormalize(cfg, mat);
    }
  }

  SECTION("cpu vs gpu: 1 channel mat") {
    bool to_rgb = true;
    Value cfg{{"type", "Normalize"}, {"mean", {123.675}}, {"std", {58.395}}, {"to_rgb", to_rgb}};

    vector<cv::Mat> mats{gray_mat, float_gray_mat};
    for (auto &mat : mats) {
      TestNormalize(cfg, mat);
    }
  }
}