Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine ppmatting code #37

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastdeploy/vision/classification/ppcls/model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool PaddleClasModel::BuildPreprocessPipelineFromConfig() {
bool use_scale = false;
int interp = 1;
processors_.push_back(
std::make_shared<ResizeByShort>(target_size, -1, -1, 1, use_scale));
std::make_shared<ResizeByShort>(target_size, 1, use_scale));
} else if (op_name == "CropImage") {
int width = op.begin()->second["size"].as<int>();
int height = op.begin()->second["size"].as<int>();
Expand Down
65 changes: 65 additions & 0 deletions fastdeploy/vision/common/processors/crop.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "fastdeploy/vision/common/processors/crop.h"

namespace fastdeploy {
namespace vision {

bool Crop::CpuRun(Mat* mat) {
cv::Mat* im = mat->GetCpuMat();
int height = static_cast<int>(im->rows);
int width = static_cast<int>(im->cols);
if (height < height_ + offset_h_ || width < width_ + offset_w_) {
FDERROR << "[Crop] Cannot crop [" << height_ << ", " << width_
<< "] from the input image [" << height << ", " << width
<< "], with offset [" << offset_h_ << ", " << offset_w_ << "]."
<< std::endl;
return false;
}
cv::Rect crop_roi(offset_w_, offset_h_, width_, height_);
*im = (*im)(crop_roi);
mat->SetWidth(width_);
mat->SetHeight(height_);
return true;
}

#ifdef ENABLE_OPENCV_CUDA
bool Crop::GpuRun(Mat* mat) {
cv::cuda::GpuMat* im = mat->GetGpuMat();
int height = static_cast<int>(im->rows);
int width = static_cast<int>(im->cols);
if (height < height_ + offset_h_ || width < width_ + offset_w_) {
FDERROR << "[Crop] Cannot crop [" << height_ << ", " << width_
<< "] from the input image [" << height << ", " << width
<< "], with offset [" << offset_h_ << ", " << offset_w_ << "]."
<< std::endl;
return false;
}
cv::Rect crop_roi(offset_w_, offset_h_, width_, height_);
*im = (*im)(crop_roi);
mat->SetWidth(width_);
mat->SetHeight(height_);
return true;
}
#endif

bool Crop::Run(Mat* mat, int offset_w, int offset_h, int width, int height,
ProcLib lib) {
auto c = Crop(offset_w, offset_h, width, height);
return c(mat, lib);
}

} // namespace vision
} // namespace fastdeploy
47 changes: 47 additions & 0 deletions fastdeploy/vision/common/processors/crop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include "fastdeploy/vision/common/processors/base.h"

namespace fastdeploy {
namespace vision {

class Crop : public Processor {
public:
Crop(int offset_w, int offset_h, int width, int height) {
offset_w_ = offset_w;
offset_h_ = offset_h;
width_ = width;
height_ = height;
}
bool CpuRun(Mat* mat);
#ifdef ENABLE_OPENCV_CUDA
bool GpuRun(Mat* mat);
#endif
std::string Name() { return "Crop"; }

static bool Run(Mat* mat, int offset_w, int offset_h, int width, int height,
ProcLib lib = ProcLib::OPENCV_CPU);

private:
int offset_w_;
int offset_h_;
int height_;
int width_;
};

} // namespace vision
} // namespace fastdeploy
25 changes: 2 additions & 23 deletions fastdeploy/vision/common/processors/limit_short.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ bool LimitShort::CpuRun(Mat* mat) {
if (target != im_size_min) {
scale = static_cast<double>(target) / static_cast<double>(im_size_min);
}
// 如果给了固定的shape,按照固定shape就好。
if (input_w_ > 0 && input_h_ > 0) {
// 给出的input_shape 大于原始的shape(origin_w, origin_h) 则直接返回。
if (origin_w <= input_w_ && origin_h <= input_h_) {
return true;
}
float scale_w = input_w_ * 1.0 / origin_w;
float scale_h = input_h_ * 1.0 / origin_h;
scale = std::min(scale_w, scale_h);
}
if (scale > 0) {
cv::resize(*im, *im, cv::Size(), scale, scale, interp_);
mat->SetWidth(im->cols);
Expand All @@ -67,16 +57,6 @@ bool LimitShort::GpuRun(Mat* mat) {
if (target != im_size_min) {
scale = static_cast<double>(target) / static_cast<double>(im_size_min);
}
// 如果给了固定的shape,按照固定shape就好。
if (input_w_ > 0 && input_h_ > 0) {
// 给出的input_shape 大于原始的shape(origin_w, origin_h) 则直接返回。
if (origin_w <= input_w_ && origin_h <= input_h_) {
return true;
}
float scale_w = input_w_ * 1.0 / origin_w;
float scale_h = input_h_ * 1.0 / origin_h;
scale = std::min(scale_w, scale_h);
}
if (scale > 0) {
cv::cuda::resize(*im, *im, cv::Size(), scale, scale, interp_);
mat->SetWidth(im->cols);
Expand All @@ -86,9 +66,8 @@ bool LimitShort::GpuRun(Mat* mat) {
}
#endif

bool LimitShort::Run(Mat* mat, int max_short, int min_short, int input_w,
int input_h, ProcLib lib) {
auto l = LimitShort(max_short, min_short, input_w, input_h);
bool LimitShort::Run(Mat* mat, int max_short, int min_short, ProcLib lib) {
auto l = LimitShort(max_short, min_short);
return l(mat, lib);
}
} // namespace vision
Expand Down
8 changes: 1 addition & 7 deletions fastdeploy/vision/common/processors/limit_short.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ namespace vision {

class LimitShort : public Processor {
public:
explicit LimitShort(int max_short = -1, int min_short = -1, int input_w = -1,
int input_h = -1, int interp = 1) {
explicit LimitShort(int max_short = -1, int min_short = -1, int interp = 1) {
max_short_ = max_short;
min_short_ = min_short;
input_w_ = input_w;
input_h_ = input_h;
interp_ = interp;
}

Expand All @@ -42,15 +39,12 @@ class LimitShort : public Processor {
std::string Name() { return "LimitShort"; }

static bool Run(Mat* mat, int max_short = -1, int min_short = -1,
int input_w = -1, int input_h = -1,
ProcLib lib = ProcLib::OPENCV_CPU);
int GetMaxShort() const { return max_short_; }

private:
int max_short_;
int min_short_;
int input_w_;
int input_h_;
int interp_;
};
} // namespace vision
Expand Down
2 changes: 1 addition & 1 deletion fastdeploy/vision/common/processors/pad_to_size.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace vision {

class PadToSize : public Processor {
public:
// only support pad with left-top padding mode
// only support pad with right-bottom padding mode
PadToSize(int width, int height, const std::vector<float>& value) {
width_ = width;
height_ = height;
Expand Down
52 changes: 23 additions & 29 deletions fastdeploy/vision/common/processors/resize_by_short.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,14 @@ bool ResizeByShort::CpuRun(Mat* mat) {
int origin_w = im->cols;
int origin_h = im->rows;
double scale = GenerateScale(origin_w, origin_h);
if (input_w_ > 0 && input_h_ > 0) {
// 给出的input_shape 大于原始的shape(origin_w, origin_h) 则直接返回。
if (origin_w <= input_w_ && origin_h <= input_h_) {
return true;
}
float scale_w = input_w_ * 1.0 / origin_w;
float scale_h = input_h_ * 1.0 / origin_h;
scale = std::min(scale_w, scale_h);
}
if (use_scale_) {
if (use_scale_ && fabs(scale - 1.0) >= 1e-06) {
cv::resize(*im, *im, cv::Size(), scale, scale, interp_);
} else {
int width = static_cast<int>(round(scale * im->cols));
int height = static_cast<int>(round(scale * im->rows));
cv::resize(*im, *im, cv::Size(width, height), 0, 0, interp_);
if (width != origin_w || height != origin_h) {
cv::resize(*im, *im, cv::Size(width, height), 0, 0, interp_);
}
}
mat->SetWidth(im->cols);
mat->SetHeight(im->rows);
Expand All @@ -50,21 +43,14 @@ bool ResizeByShort::GpuRun(Mat* mat) {
int origin_h = im->rows;
double scale = GenerateScale(origin_w, origin_h);
im->convertTo(*im, CV_32FC(im->channels()));
if (input_w_ > 0 && input_h_ > 0) {
// 给出的input_shape 大于原始的shape(origin_w, origin_h) 则直接返回。
if (origin_w <= input_w_ && origin_h <= input_h_) {
return true;
}
float scale_w = input_w_ * 1.0 / origin_w;
float scale_h = input_h_ * 1.0 / origin_h;
scale = std::min(scale_w, scale_h);
}
if (use_scale_) {
if (use_scale_ && fabs(scale - 1.0) >= 1e-06) {
cv::cuda::resize(*im, *im, cv::Size(), scale, scale, interp_);
} else {
int width = static_cast<int>(round(scale * im->cols));
int height = static_cast<int>(round(scale * im->rows));
cv::cuda::resize(*im, *im, cv::Size(width, height), 0, 0, interp_);
if (width != origin_w || height != origin_h) {
cv::cuda::resize(*im, *im, cv::Size(width, height), 0, 0, interp_);
}
}
mat->SetWidth(im->cols);
mat->SetHeight(im->rows);
Expand All @@ -77,18 +63,26 @@ double ResizeByShort::GenerateScale(const int origin_w, const int origin_h) {
int im_size_min = std::min(origin_w, origin_h);
double scale =
static_cast<double>(target_size_) / static_cast<double>(im_size_min);
if (max_size_ > 0) {
if (round(scale * im_size_max) > max_size_) {
scale = static_cast<double>(max_size_) / static_cast<double>(im_size_max);
if (max_hw_.size() > 0) {
FDASSERT(max_hw_.size() == 2,
"Require size of max_hw_ be 2, but now it's %zu.", max_hw_.size());
FDASSERT(
max_hw_[0] > 0 && max_hw_[1] > 0,
"Require elements in max_hw_ greater than 0, but now it's [%d, %d].",
max_hw_[0], max_hw_[1]);
if (round(scale * origin_h) > max_hw_[0]) {
scale = static_cast<double>(max_hw_[0]) / static_cast<double>(origin_h);
}
if (round(scale * origin_w) > max_hw_[1]) {
scale = static_cast<double>(max_hw_[1]) / static_cast<double>(origin_w);
}
}
return scale;
}

bool ResizeByShort::Run(Mat* mat, int target_size, int input_w, int input_h,
int interp, bool use_scale, int max_size, ProcLib lib) {
auto r =
ResizeByShort(target_size, input_w, input_h, interp, use_scale, max_size);
bool ResizeByShort::Run(Mat* mat, int target_size, int interp, bool use_scale,
const std::vector<int>& max_hw, ProcLib lib) {
auto r = ResizeByShort(target_size, interp, use_scale, max_hw);
return r(mat, lib);
}
} // namespace vision
Expand Down
17 changes: 7 additions & 10 deletions fastdeploy/vision/common/processors/resize_by_short.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ namespace vision {

class ResizeByShort : public Processor {
public:
ResizeByShort(int target_size, int input_w = -1, int input_h = -1,
int interp = 1, bool use_scale = true, int max_size = -1) {
ResizeByShort(int target_size, int interp = 1, bool use_scale = true,
const std::vector<int>& max_hw = std::vector<int>()) {
target_size_ = target_size;
input_h_ = input_h;
input_w_ = input_w;
max_size_ = max_size;
max_hw_ = max_hw;
interp_ = interp;
use_scale_ = use_scale;
}
Expand All @@ -36,16 +34,15 @@ class ResizeByShort : public Processor {
#endif
std::string Name() { return "ResizeByShort"; }

static bool Run(Mat* mat, int target_size, int input_w = -1, int input_h = -1,
int interp = 1, bool use_scale = true, int max_size = -1,
static bool Run(Mat* mat, int target_size, int interp = 1,
bool use_scale = true,
const std::vector<int>& max_hw = std::vector<int>(),
ProcLib lib = ProcLib::OPENCV_CPU);

private:
double GenerateScale(const int origin_w, const int origin_h);
int target_size_;
int input_h_;
int input_w_;
int max_size_;
std::vector<int> max_hw_;
int interp_;
bool use_scale_;
};
Expand Down
1 change: 1 addition & 0 deletions fastdeploy/vision/common/processors/transform.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "fastdeploy/vision/common/processors/center_crop.h"
#include "fastdeploy/vision/common/processors/color_space_convert.h"
#include "fastdeploy/vision/common/processors/convert.h"
#include "fastdeploy/vision/common/processors/crop.h"
#include "fastdeploy/vision/common/processors/hwc2chw.h"
#include "fastdeploy/vision/common/processors/limit_by_stride.h"
#include "fastdeploy/vision/common/processors/limit_long.h"
Expand Down
7 changes: 6 additions & 1 deletion fastdeploy/vision/detection/ppdet/ppyoloe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,13 @@ bool PPYOLOE::BuildPreprocessPipelineFromConfig() {
} else {
int min_target_size = std::min(target_size[0], target_size[1]);
int max_target_size = std::max(target_size[0], target_size[1]);
std::vector<int> max_size;
if (max_target_size > 0) {
max_size.push_back(max_target_size);
max_size.push_back(max_target_size);
}
processors_.push_back(std::make_shared<ResizeByShort>(
min_target_size, -1, -1, interp, true, max_target_size));
min_target_size, interp, true, max_size));
}
} else if (op_name == "Permute") {
// Do nothing, do permute as the last operation
Expand Down
Loading