mmdeploy/tests/test_csrc/preprocess/test_load.cpp

82 lines
3.1 KiB
C++
Raw Normal View History

// Copyright (c) OpenMMLab. All rights reserved.
#include "catch.hpp"
#include "mmdeploy/core/mat.h"
#include "mmdeploy/core/tensor.h"
#include "mmdeploy/core/utils/device_utils.h"
#include "mmdeploy/preprocess/transform/transform.h"
Support Windows (#106) * minor changes * support windows * fix GCC build * fix lint * reformat * fix Windows build * fix GCC build * search backend ops for onnxruntime * fix lint * fix lint * code clean-up * code clean-up * fix clang build * fix trt support * fix cmake for ncnn * fix cmake for openvino * fix SDK Python API * handle ops for other backends (ncnn, trt) * handle SDK Python API library location * robustify linkage * fix cuda * minor fix for openvino & ncnn * use CMAKE_CUDA_ARCHITECTURES if set * fix cuda preprocessor * fix misc * fix pplnn & pplcv, drop support for pplcv<0.6.0 * robustify cmake * update build.md (#2) * build dynamic modules as module library & fix demo (partially) * fix candidate path for mmdeploy_python * move "enable CUDA" to cmake config for demo * refine demo cmake * add comment * fix ubuntu build * revert docs/en/build.md * fix C API * fix lint * Windows build doc (#3) * check in docs related to mmdeploy build on windows * update build guide on windows platform * update build guide on windows platform * make path of thirdparty libraries consistent * make path consistency * correct build command for custom ops * correct build command for sdk * update sdk build instructions * update doc * correct build command * fix lint * correct build command and fix lint Co-authored-by: lvhan <lvhan@pjlab.org> * trailing whitespace (#4) * minor fix * fix sr sdk model * fix type deduction * fix cudaFree after driver shutting down * update ppl.cv installation warning (#5) * fix device allocator threshold & fix lint * update doc (#6) * update ppl.cv installation warning * missing 'git clone' Co-authored-by: chenxin <chenxin2@sensetime.com> Co-authored-by: zhangli <zhangli@sensetime.com> Co-authored-by: lvhan028 <lvhan_028@163.com> Co-authored-by: lvhan <lvhan@pjlab.org>
2022-02-24 20:08:44 +08:00
#include "opencv_utils.h"
#include "test_resource.h"
#include "test_utils.h"
using namespace mmdeploy;
using namespace std;
using namespace mmdeploy::test;
void TestLoad(const Value& cfg, const cv::Mat& mat, PixelFormat src_format,
PixelFormat dst_format) {
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);
auto ref_mat = mmdeploy::cpu::ColorTransfer(mat, src_format, dst_format);
auto res = transform->Process({{"ori_img", cpu::CVMat2Mat(mat, PixelFormat(src_format))}});
REQUIRE(!res.has_error());
auto res_tensor = res.value()["img"].get<Tensor>();
REQUIRE(res_tensor.device() == device);
REQUIRE(Shape(res.value(), "img_shape") ==
vector<int64_t>{1, ref_mat.rows, ref_mat.cols, ref_mat.channels()});
REQUIRE(Shape(res.value(), "ori_shape") ==
vector<int64_t>{1, mat.rows, mat.cols, mat.channels()});
REQUIRE(res.value().contains("img_fields"));
REQUIRE(res.value()["img_fields"].is_array());
REQUIRE(res.value()["img_fields"].size() == 1);
REQUIRE(res.value()["img_fields"][0].get<string>() == "img");
const 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("prepare image, that is LoadImageFromFile transform", "[load]") {
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::IMREAD_COLOR);
cv::Mat gray_mat = cv::imread(img_path, cv::IMREAD_GRAYSCALE);
cv::Mat rgb_mat;
cv::Mat bgra_mat;
// TODO: make up yuv nv12/nv21 mat
cv::cvtColor(bgr_mat, rgb_mat, cv::COLOR_BGR2RGB);
cv::cvtColor(bgr_mat, bgra_mat, cv::COLOR_BGR2BGRA);
vector<pair<cv::Mat, PixelFormat>> mats{{bgr_mat, PixelFormat::kBGR},
{rgb_mat, PixelFormat::kRGB},
{gray_mat, PixelFormat::kGRAYSCALE},
{bgra_mat, PixelFormat::kBGRA}};
// pair is <color_type, to_float32>
vector<pair<std::string, bool>> conditions{
{"color", true}, {"color", false}, {"grayscale", true}, {"grayscale", false}};
for (auto& condition : conditions) {
Value cfg{{"type", "LoadImageFromFile"},
{"to_float32", condition.second},
{"color_type", condition.first}};
for (auto& mat : mats) {
TestLoad(cfg, mat.first, mat.second,
condition.first == "color" ? PixelFormat::kBGR : PixelFormat::kGRAYSCALE);
}
}
}