Skip to content

Commit

Permalink
Refactor tests (open-mmlab#283)
Browse files Browse the repository at this point in the history
* fix sdk model's pipeline.json

* resize INT64 mask

* refactor unit tests

* fix api in model.h

* remove 'customs' from meta info

* fix zip model

* fix clang-format issue

* put tc on each backend into a SECTION

* change SECTION title

* add DYNAMIC_SECTION for capi unit test

* change 'devices' to 'device_names'

* change trt to tensorrt

* remove uncessary check

* add color_type 'color_ignore_orientation' which is used in ocr

* 'min_width', 'max_width' and 'backend' might be null in pipeline config

* fix clang-format issue

* remove useless code
  • Loading branch information
lvhan028 authored Dec 17, 2021
1 parent d3e8473 commit 3be1779
Show file tree
Hide file tree
Showing 54 changed files with 1,864 additions and 2,276 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ work_dirs/

# install directory
/install

# the generated header files
/tests/test_csrc/test_define.h
2 changes: 1 addition & 1 deletion csrc/apis/c/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ int mmdeploy_model_create_by_path(const char* path, mm_model_t* model);

int mmdeploy_model_create(const void* buffer, int size, mm_model_t* model);

void mmdeploy_model_destroy(mm_model_t* model);
void mmdeploy_model_destroy(mm_model_t model);

#endif // MMDEPLOY_SRC_APIS_C_MODEL_H_
12 changes: 9 additions & 3 deletions csrc/codebase/mmocr/resize_ocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ class ResizeOCRImpl : public Module {
public:
explicit ResizeOCRImpl(const Value& args) noexcept {
height_ = args.value("height", height_);
min_width_ = args.value("min_width", min_width_);
max_width_ = args.value("max_width", max_width_);
min_width_ = args.contains("min_width") && args["min_width"].is_number_integer()
? args["min_width"].get<int>()
: min_width_;
max_width_ = args.contains("max_width") && args["max_width"].is_number_integer()
? args["max_width"].get<int>()
: max_width_;
keep_aspect_ratio_ = args.value("keep_aspect_ratio", keep_aspect_ratio_);
backend_ = args.contains("backend") && args["backend"].is_string()
? args["backend"].get<string>()
: backend_;
img_pad_value_ = args.value("img_pad_value", img_pad_value_);
width_downsample_ratio_ = args.value("width_downsample_ratio", width_downsample_ratio_);
backend_ = args.value("backend", backend_);
stream_ = args["context"]["stream"].get<Stream>();
}

Expand Down
76 changes: 41 additions & 35 deletions csrc/codebase/mmseg/segment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,66 @@

namespace mmdeploy::mmseg {

static Result<void> VisualizeMask(const std::string &image_name, const Tensor &mask, int height,
int width, Stream &stream) {
Device cpu_device{"cpu"};
OUTCOME_TRY(auto host_mask, MakeAvailableOnDevice(mask, cpu_device, stream));
OUTCOME_TRY(stream.Wait());
// cv::Mat mask_image(height, width, CV_32SC1, host_mask.data<int>());
// cv::imwrite(image_name + ".png", mask_image * 10);
// ofstream ofs(image_name + ".data");
// auto _data_ptr = host_mask.data<int>();
// for (auto i = 0; i < height; ++i) {
// for (auto j = 0; j < width; ++j) {
// ofs << *_data_ptr++ << ", ";
// }
// ofs << "\n";
// }
return success();
}

class ResizeMask : public MMSegmentation {
public:
explicit ResizeMask(const Value &cfg) : MMSegmentation(cfg) {
classes_ = cfg["params"]["num_classes"].get<int>();
try {
classes_ = cfg["params"]["num_classes"].get<int>();
} catch (const std::exception &e) {
ERROR("no ['params']['num_classes'] is specified in cfg: {}", cfg);
throw_exception(eInvalidArgument);
}
}

Result<Value> operator()(const Value &preprocess_result, const Value &inference_result) {
DEBUG("preprocess: {}\ninference: {}", preprocess_result, inference_result);

auto mask = inference_result["output"].get<Tensor>();
INFO("tensor.name: {}, tensor.shape: {}", mask.name(), mask.shape());
assert(mask.data_type() == DataType::kINT32);
INFO("tensor.name: {}, tensor.shape: {}, tensor.data_type: {}", mask.name(), mask.shape(),
mask.data_type());
assert(mask.data_type() == DataType::kINT32 || mask.data_type() == DataType::kINT64);
assert(mask.shape(0) == 1);
assert(mask.shape(1) == 1);

auto height = (int)mask.shape(2);
auto width = (int)mask.shape(3);
auto input_height = preprocess_result["img_metas"]["ori_shape"][1].get<int>();
auto input_width = preprocess_result["img_metas"]["ori_shape"][2].get<int>();
if (height == input_height && width == input_width) {
SegmentorOutput output{mask, input_height, input_width, classes_};
return to_value(output);
Device host{"cpu"};
OUTCOME_TRY(auto host_tensor, MakeAvailableOnDevice(mask, host, stream_));
stream_.Wait().value();
if (mask.data_type() == DataType::kINT64) {
// change kINT64 to 2 INT32
TensorDesc desc{.device = host_tensor.device(),
.data_type = DataType::kINT32,
.shape = {1, 2, height, width},
.name = host_tensor.name()};
Tensor _host_tensor(desc, mask.buffer());
return MaskResize(_host_tensor, input_height, input_width);
} else {
Device host{"cpu"};

OUTCOME_TRY(auto host_tensor, MakeAvailableOnDevice(mask, host, stream_));
host_tensor.Reshape({1, height, width, 1});
auto mat = cpu::Tensor2CVMat(host_tensor);
auto dst = cpu::Resize(mat, input_height, input_width, "nearest");
auto output_tensor = cpu::CVMat2Tensor(dst);
return MaskResize(host_tensor, input_height, input_width);
}
}

SegmentorOutput output{output_tensor, input_height, input_width, classes_};
private:
Result<Value> MaskResize(Tensor &tensor, int dst_height, int dst_width) {
auto channel = tensor.shape(1);
auto height = tensor.shape(2);
auto width = tensor.shape(3);

// OUTCOME_TRY(
// VisualizeMask("resize_mask", output_tensor, input_height, input_width,
// stream_));
// reshape tensor to convert it to cv::Mat
tensor.Reshape({1, height, width, channel});
auto mat = cpu::Tensor2CVMat(tensor);
auto dst = cpu::Resize(mat, dst_height, dst_width, "nearest");
if (channel == 1) {
auto output_tensor = cpu::CVMat2Tensor(dst);
SegmentorOutput output{output_tensor, dst_height, dst_width, classes_};
return to_value(output);
} else {
cv::Mat _dst;
cv::extractChannel(dst, _dst, 0);
auto output_tensor = cpu::CVMat2Tensor(_dst);
SegmentorOutput output{output_tensor, dst_height, dst_width, classes_};
return to_value(output);
}
}
Expand Down
3 changes: 1 addition & 2 deletions csrc/core/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ struct model_meta_info_t {
struct deploy_meta_info_t {
std::string version;
std::vector<model_meta_info_t> models;
std::vector<std::string> customs;
MMDEPLOY_ARCHIVE_MEMBERS(version, models, customs);
MMDEPLOY_ARCHIVE_MEMBERS(version, models);
};

class ModelImpl;
Expand Down
20 changes: 15 additions & 5 deletions csrc/model/zip_model_impl.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright (c) OpenMMLab. All rights reserved.

#include <string.h>

#include <fstream>
#include <map>

Expand All @@ -10,6 +8,13 @@
#include "core/model.h"
#include "core/model_impl.h"
#include "zip.h"
#if __GNUC__ >= 8
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif

using nlohmann::json;

Expand Down Expand Up @@ -62,6 +67,7 @@ class ZipModelImpl : public ModelImpl {
Result<std::string> ReadFile(const std::string& file_path) const override {
int ret = 0;
int index = -1;

auto iter = file_index_.find(file_path);
if (iter == file_index_.end()) {
ERROR("cannot find file {} under dir {}", file_path.c_str(), root_dir_.c_str());
Expand Down Expand Up @@ -103,16 +109,20 @@ class ZipModelImpl : public ModelImpl {
Result<void> InitZip() {
int files = zip_get_num_files(zip_);
INFO("there are {} files in sdk model file", files);

if (files == 0) {
return Status(eFail);
}
for (int i = 0; i < files; ++i) {
struct zip_stat stat;
zip_stat_init(&stat);
zip_stat_index(zip_, i, 0, &stat);
if (stat.name[strlen(stat.name) - 1] == '/') {
fs::path path(stat.name);
auto file_name = path.filename().string();
if (file_name == ".") {
DEBUG("{}-th file name is: {}, which is a directory", i, stat.name);
} else {
DEBUG("{}-th file name is: {}, which is a file", i, stat.name);
file_index_[stat.name] = i;
file_index_[file_name] = i;
}
}
return success();
Expand Down
2 changes: 1 addition & 1 deletion csrc/net/trt/trt_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ Result<void> TRTNet::ForwardAsync(Event* event) { return Status(eNotSupported);

class TRTNetCreator : public Creator<Net> {
public:
const char* GetName() const override { return "trt"; }
const char* GetName() const override { return "tensorrt"; }
int GetVersion() const override { return 0; }
std::unique_ptr<Net> Create(const Value& args) override {
auto p = std::make_unique<TRTNet>();
Expand Down
4 changes: 3 additions & 1 deletion csrc/preprocess/transform/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ Result<Value> PrepareImageImpl::Process(const Value& input) {
Value output = input;

Mat src_mat = input["ori_img"].get<Mat>();
auto res = (arg_.color_type == "color" ? ConvertToBGR(src_mat) : ConvertToGray(src_mat));
auto res = (arg_.color_type == "color" || arg_.color_type == "color_ignore_orientation"
? ConvertToBGR(src_mat)
: ConvertToGray(src_mat));

OUTCOME_TRY(auto tensor, std::move(res));

Expand Down
4 changes: 2 additions & 2 deletions demo/csrc/config/resnet50_ort/pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
{
"name": "postprocess",
"type": "Task",
"module": "MMClsPostprocess",
"postprocess_type": "SoftmaxPost",
"module": "mmcls",
"component": "LinearClsHead",
"input": [
"data",
"cls_res"
Expand Down
11 changes: 7 additions & 4 deletions demo/csrc/config/retinanet_ort/pipeline.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,21 @@
}
},
{
"name": "retinanet_post",
"name": "postprocess",
"type": "Task",
"module": "MMDetPostprocess",
"postprocess_type": "SingleStagePost",
"module": "mmdet",
"component": "ResizeBBox",
"input": [
"prep_res",
"infer_res"
],
"output": [
"bboxes"
],
"score_thr": 0.3
"params": {
"score_thr": 0.3,
"min_bbox_size": 10
}
}
]
}
Expand Down
Loading

0 comments on commit 3be1779

Please sign in to comment.