fix build error after cherry-picks

pull/1502/head
lvhan028 2022-12-12 14:55:24 +08:00
parent 57f7712267
commit 5b96f20a9b
3 changed files with 47 additions and 159 deletions

View File

@ -2,24 +2,21 @@
#include <set>
#include "mmdeploy/archive/json_archive.h"
#include "mmdeploy/archive/value_archive.h"
#include "mmdeploy/core/registry.h"
#include "mmdeploy/core/tensor.h"
#include "mmdeploy/core/utils/device_utils.h"
#include "mmdeploy/core/utils/formatter.h"
#include "mmdeploy/preprocess/transform/resize.h"
#include "mmdeploy/operation/managed.h"
#include "mmdeploy/operation/vision.h"
#include "mmdeploy/preprocess/transform/transform.h"
#include "opencv2/imgproc.hpp"
#include "opencv_utils.h"
using namespace std;
namespace mmdeploy {
namespace mmdeploy::mmocr {
class RescaleToHeightImpl : public Module {
class RescaleToHeight : public transform::Transform {
public:
explicit RescaleToHeightImpl(const Value& args) noexcept {
explicit RescaleToHeight(const Value& args) noexcept {
height_ = args.value("height", height_);
min_width_ = args.contains("min_width") && args["min_width"].is_number_integer()
? args["min_width"].get<int>()
@ -30,34 +27,28 @@ class RescaleToHeightImpl : public Module {
width_divisor_ = args.contains("width_divisor") && args["width_divisor"].is_number_integer()
? args["width_divisor"].get<int>()
: width_divisor_;
resize_type_ = args.contains("resize_type") && args["resize_type"].is_string()
? args["resize_type"].get<string>()
: resize_type_;
stream_ = args["context"]["stream"].get<Stream>();
resize_ = operation::Managed<operation::Resize>::Create("bilinear");
}
~RescaleToHeightImpl() override = default;
~RescaleToHeight() override = default;
Result<Value> Process(const Value& input) override {
MMDEPLOY_DEBUG("input: {}", input);
Result<void> Apply(Value& data) override {
MMDEPLOY_DEBUG("input: {}", data);
auto dst_height = height_;
auto dst_min_width = min_width_;
auto dst_max_width = max_width_;
std::vector<int> img_shape; // NHWC
from_value(input["img_shape"], img_shape);
from_value(data["img_shape"], img_shape);
std::vector<int> ori_shape; // NHWC
from_value(input["ori_shape"], ori_shape);
from_value(data["ori_shape"], ori_shape);
auto ori_height = ori_shape[1];
auto ori_width = ori_shape[2];
auto valid_ratio = 1.f;
Device host{"cpu"};
auto _img = input["img"].get<Tensor>();
OUTCOME_TRY(auto img, MakeAvailableOnDevice(_img, host, stream_));
stream_.Wait().value();
auto img = data["img"].get<Tensor>();
Tensor img_resize;
auto new_width = static_cast<int>(std::ceil(1.f * dst_height / ori_height * ori_width));
auto width_divisor = width_divisor_;
@ -65,78 +56,30 @@ class RescaleToHeightImpl : public Module {
new_width = std::max(dst_min_width, new_width);
}
if (dst_max_width > 0) {
auto resize_width = std::min(dst_max_width, new_width);
new_width = std::min(dst_max_width, new_width);
}
if (new_width % width_divisor != 0) {
new_width = std::round(1.f * new_width / width_divisor) * width_divisor;
}
img_resize = ResizeImage(img, dst_height, new_width);
Value output = input;
output["img"] = img_resize;
output["resize_shape"] = to_value(img_resize.desc().shape);
output["pad_shape"] = output["resize_shape"];
output["ori_shape"] = input["ori_shape"];
output["scale"] = to_value(std::vector<int>({new_width, dst_height}));
output["valid_ratio"] = valid_ratio;
MMDEPLOY_DEBUG("output: {}", to_json(output).dump(2));
return output;
}
Tensor ResizeImage(const Tensor& img, int dst_h, int dst_w) {
TensorDesc desc = img.desc();
assert(desc.shape.size() == 4);
assert(desc.data_type == DataType::kINT8);
int h = desc.shape[1];
int w = desc.shape[2];
int c = desc.shape[3];
assert(c == 3 || c == 1);
cv::Mat src_mat, dst_mat;
if (3 == c) { // rgb
src_mat = cv::Mat(h, w, CV_8UC3, const_cast<uint8_t*>(img.data<uint8_t>()));
} else { // gray
src_mat = cv::Mat(h, w, CV_8UC1, const_cast<uint8_t*>(img.data<uint8_t>()));
}
cv::Size size{dst_w, dst_h};
cv::resize(src_mat, dst_mat, size, cv::INTER_LINEAR);
return Tensor({desc.device, desc.data_type, {1, dst_h, dst_w, c}, ""},
{dst_mat.data, [mat = dst_mat](void* ptr) {}});
OUTCOME_TRY(resize_.Apply(img, img_resize, dst_height, new_width));
data["img"] = img_resize;
data["resize_shape"] = to_value(img_resize.desc().shape);
data["pad_shape"] = data["resize_shape"];
data["ori_shape"] = data["ori_shape"];
data["scale"] = to_value(std::vector<int>({new_width, dst_height}));
data["valid_ratio"] = valid_ratio;
MMDEPLOY_DEBUG("output: {}", data);
return success();
}
protected:
operation::Managed<operation::Resize> resize_;
int height_{-1};
int min_width_{-1};
int max_width_{-1};
bool keep_aspect_ratio_{true};
int width_divisor_{1};
std::string resize_type_{"Resize"};
Stream stream_;
};
MMDEPLOY_CREATOR_SIGNATURE(RescaleToHeightImpl,
std::unique_ptr<RescaleToHeightImpl>(const Value& config));
MMDEPLOY_DEFINE_REGISTRY(RescaleToHeightImpl);
MMDEPLOY_REGISTER_FACTORY_FUNC(RescaleToHeightImpl, (cpu, 0), [](const Value& config) {
return std::make_unique<RescaleToHeightImpl>(config);
});
class RescaleToHeight : public Transform {
public:
explicit RescaleToHeight(const Value& args) : Transform(args) {
impl_ = Instantiate<RescaleToHeightImpl>("RescaleToHeight", args);
}
~RescaleToHeight() override = default;
Result<Value> Process(const Value& input) override { return impl_->Process(input); }
private:
std::unique_ptr<RescaleToHeightImpl> impl_;
static const std::string name_;
};
MMDEPLOY_REGISTER_FACTORY_FUNC(Transform, (ResizeOCR, 0), [](const Value& config) {
return std::make_unique<RescaleToHeight>(config);
});
} // namespace mmdeploy
MMDEPLOY_REGISTER_TRANSFORM(RescaleToHeight);
} // namespace mmdeploy::mmocr

View File

@ -2,24 +2,21 @@
#include <set>
#include "mmdeploy/archive/json_archive.h"
#include "mmdeploy/archive/value_archive.h"
#include "mmdeploy/core/registry.h"
#include "mmdeploy/core/tensor.h"
#include "mmdeploy/core/utils/device_utils.h"
#include "mmdeploy/core/utils/formatter.h"
#include "mmdeploy/preprocess/transform/resize.h"
#include "mmdeploy/operation/managed.h"
#include "mmdeploy/operation/vision.h"
#include "mmdeploy/preprocess/transform/transform.h"
#include "opencv2/imgproc.hpp"
#include "opencv_utils.h"
using namespace std;
namespace mmdeploy {
namespace mmdeploy::mmocr {
class ShortScaleAspectJitterImpl : public Module {
class ShortScaleAspectJitter : public transform::Transform {
public:
explicit ShortScaleAspectJitterImpl(const Value& args) noexcept {
explicit ShortScaleAspectJitter(const Value& args) noexcept {
short_size_ = args.contains("short_size") && args["short_size"].is_number_integer()
? args["short_size"].get<int>()
: short_size_;
@ -41,16 +38,13 @@ class ShortScaleAspectJitterImpl : public Module {
scale_divisor_ = args.contains("scale_divisor") && args["scale_divisor"].is_number_integer()
? args["scale_divisor"].get<int>()
: scale_divisor_;
resize_type_ = args.contains("resize_type") && args["resize_type"].is_string()
? args["resize_type"].get<string>()
: resize_type_;
stream_ = args["context"]["stream"].get<Stream>();
resize_ = operation::Managed<operation::Resize>::Create("bilinear");
}
~ShortScaleAspectJitterImpl() override = default;
~ShortScaleAspectJitter() override = default;
Result<Value> Process(const Value& input) override {
MMDEPLOY_DEBUG("input: {}", input);
Result<void> Apply(Value& data) override {
MMDEPLOY_DEBUG("input: {}", data);
auto short_size = short_size_;
auto ratio_range = ratio_range_;
auto aspect_ratio_range = aspect_ratio_range_;
@ -62,18 +56,15 @@ class ShortScaleAspectJitterImpl : public Module {
return Status(eNotSupported);
}
std::vector<int> img_shape; // NHWC
from_value(input["img_shape"], img_shape);
from_value(data["img_shape"], img_shape);
std::vector<int> ori_shape; // NHWC
from_value(input["ori_shape"], ori_shape);
from_value(data["ori_shape"], ori_shape);
auto ori_height = ori_shape[1];
auto ori_width = ori_shape[2];
Device host{"cpu"};
auto _img = input["img"].get<Tensor>();
OUTCOME_TRY(auto img, MakeAvailableOnDevice(_img, host, stream_));
stream_.Wait().value();
auto img = data["img"].get<Tensor>();
Tensor img_resize;
auto scale = static_cast<float>(1.0 * short_size / std::min(img_shape[1], img_shape[2]));
auto dst_height = static_cast<int>(std::round(scale * img_shape[1]));
@ -83,69 +74,23 @@ class ShortScaleAspectJitterImpl : public Module {
std::vector<float> scale_factor = {(float)1.0 * dst_width / img_shape[2],
(float)1.0 * dst_height / img_shape[1]};
img_resize = ResizeImage(img, dst_height, dst_width);
Value output = input;
output["img"] = img_resize;
output["resize_shape"] = to_value(img_resize.desc().shape);
output["scale"] = to_value(std::vector<int>({dst_width, dst_height}));
output["scale_factor"] = to_value(scale_factor);
MMDEPLOY_DEBUG("output: {}", to_json(output).dump(2));
return output;
}
Tensor ResizeImage(const Tensor& img, int dst_h, int dst_w) {
TensorDesc desc = img.desc();
assert(desc.shape.size() == 4);
assert(desc.data_type == DataType::kINT8);
int h = desc.shape[1];
int w = desc.shape[2];
int c = desc.shape[3];
assert(c == 3 || c == 1);
cv::Mat src_mat, dst_mat;
if (3 == c) { // rgb
src_mat = cv::Mat(h, w, CV_8UC3, const_cast<uint8_t*>(img.data<uint8_t>()));
} else { // gray
src_mat = cv::Mat(h, w, CV_8UC1, const_cast<uint8_t*>(img.data<uint8_t>()));
}
cv::Size size{dst_w, dst_h};
cv::resize(src_mat, dst_mat, size, cv::INTER_LINEAR);
return Tensor({desc.device, desc.data_type, {1, dst_h, dst_w, c}, ""},
{dst_mat.data, [mat = dst_mat](void* ptr) {}});
OUTCOME_TRY(resize_.Apply(img, img_resize, dst_height, dst_width));
data["img"] = img_resize;
data["resize_shape"] = to_value(img_resize.desc().shape);
data["scale"] = to_value(std::vector<int>({dst_width, dst_height}));
data["scale_factor"] = to_value(scale_factor);
MMDEPLOY_DEBUG("output: {}", data);
return success();
}
protected:
operation::Managed<operation::Resize> resize_;
int short_size_{736};
std::vector<float> ratio_range_{0.7, 1.3};
std::vector<float> aspect_ratio_range_{0.9, 1.1};
int scale_divisor_{1};
std::string resize_type_{"Resize"};
Stream stream_;
};
MMDEPLOY_CREATOR_SIGNATURE(ShortScaleAspectJitterImpl,
std::unique_ptr<ShortScaleAspectJitterImpl>(const Value& config));
MMDEPLOY_DEFINE_REGISTRY(ShortScaleAspectJitterImpl);
MMDEPLOY_REGISTER_TRANSFORM(ShortScaleAspectJitter);
MMDEPLOY_REGISTER_FACTORY_FUNC(ShortScaleAspectJitterImpl, (cpu, 0), [](const Value& config) {
return std::make_unique<ShortScaleAspectJitterImpl>(config);
});
class ShortScaleAspectJitter : public Transform {
public:
explicit ShortScaleAspectJitter(const Value& args) : Transform(args) {
impl_ = Instantiate<ShortScaleAspectJitterImpl>("ShortScaleAspectJitter", args);
}
~ShortScaleAspectJitter() override = default;
Result<Value> Process(const Value& input) override { return impl_->Process(input); }
private:
std::unique_ptr<ShortScaleAspectJitterImpl> impl_;
static const std::string name_;
};
MMDEPLOY_REGISTER_FACTORY_FUNC(Transform, (ShortScaleAspectJitter, 0), [](const Value& config) {
return std::make_unique<ShortScaleAspectJitter>(config);
});
} // namespace mmdeploy
} // namespace mmdeploy::mmocr