未验证 提交 10b62145 编写于 作者: W wangguanzhong 提交者: GitHub

fix centernet deploy

上级 d2304597
......@@ -30,6 +30,6 @@ TestReader:
sample_transforms:
- Decode: {}
- WarpAffine: {keep_res: True, input_h: 512, input_w: 512}
- NormalizeImage: {mean: [0.40789655, 0.44719303, 0.47026116], std: [0.2886383 , 0.27408165, 0.27809834]}
- NormalizeImage: {mean: [0.40789655, 0.44719303, 0.47026116], std: [0.2886383 , 0.27408165, 0.27809834], is_scale: True}
- Permute: {}
batch_size: 1
......@@ -74,7 +74,7 @@ class NormalizeImage : public PreprocessOp {
// CHW or HWC
std::vector<float> mean_;
std::vector<float> scale_;
bool is_scale_;
bool is_scale_ = true;
};
class Permute : public PreprocessOp {
......@@ -143,6 +143,24 @@ class TopDownEvalAffine : public PreprocessOp {
std::vector<int> trainsize_;
};
class WarpAffine : public PreprocessOp {
public:
virtual void Init(const YAML::Node& item) {
input_h_ = item["input_h"].as<int>();
input_w_ = item["input_w"].as<int>();
keep_res_ = item["keep_res"].as<bool>();
}
virtual void Run(cv::Mat* im, ImageBlob* data);
private:
int input_h_;
int input_w_;
int interp_ = 1;
bool keep_res_ = true;
int pad_ = 31;
};
void CropImg(cv::Mat& img,
cv::Mat& crop_img,
std::vector<int>& area,
......@@ -183,6 +201,8 @@ class Preprocessor {
return std::make_shared<PadStride>();
} else if (name == "TopDownEvalAffine") {
return std::make_shared<TopDownEvalAffine>();
} else if (name == "WarpAffine") {
return std::make_shared<WarpAffine>();
}
std::cerr << "can not find function of OP: " << name
<< " and return: nullptr" << std::endl;
......
......@@ -177,11 +177,64 @@ void TopDownEvalAffine::Run(cv::Mat* im, ImageBlob* data) {
};
}
void GetAffineTrans(const cv::Point2f center,
const cv::Point2f input_size,
const cv::Point2f output_size,
cv::Mat* trans) {
cv::Point2f srcTri[3];
cv::Point2f dstTri[3];
float src_w = input_size.x;
float dst_w = output_size.x;
float dst_h = output_size.y;
cv::Point2f src_dir(0, -0.5 * src_w);
cv::Point2f dst_dir(0, -0.5 * dst_w);
srcTri[0] = center;
srcTri[1] = center + src_dir;
cv::Point2f src_d = srcTri[0] - srcTri[1];
srcTri[2] = srcTri[1] + cv::Point2f(-src_d.y, src_d.x);
dstTri[0] = cv::Point2f(dst_w * 0.5, dst_h * 0.5);
dstTri[1] = cv::Point2f(dst_w * 0.5, dst_h * 0.5) + dst_dir;
cv::Point2f dst_d = dstTri[0] - dstTri[1];
dstTri[2] = dstTri[1] + cv::Point2f(-dst_d.y, dst_d.x);
*trans = cv::getAffineTransform(srcTri, dstTri);
}
void WarpAffine::Run(cv::Mat* im, ImageBlob* data) {
cv::cvtColor(*im, *im, cv::COLOR_RGB2BGR);
cv::Mat trans(2, 3, CV_32FC1);
cv::Point2f center;
cv::Point2f input_size;
int h = im->rows;
int w = im->cols;
if (keep_res_) {
input_h_ = (h | pad_) + 1;
input_w_ = (w + pad_) + 1;
input_size = cv::Point2f(input_w_, input_h_);
center = cv::Point2f(w / 2, h / 2);
} else {
float s = std::max(h, w) * 1.0;
input_size = cv::Point2f(s, s);
center = cv::Point2f(w / 2., h / 2.);
}
cv::Point2f output_size(input_w_, input_h_);
GetAffineTrans(center, input_size, output_size, &trans);
cv::warpAffine(*im, *im, trans, cv::Size(input_w_, input_h_));
data->in_net_shape_ = {
static_cast<float>(input_h_), static_cast<float>(input_w_),
};
}
// Preprocessor op running order
const std::vector<std::string> Preprocessor::RUN_ORDER = {"InitInfo",
"TopDownEvalAffine",
"Resize",
"LetterBoxResize",
"WarpAffine",
"NormalizeImage",
"PadStride",
"Permute"};
......
......@@ -524,7 +524,7 @@ class CenterNetPostProcess(TTFBox):
x2 = xs + wh[:, 0:1] / 2
y2 = ys + wh[:, 1:2] / 2
n, c, feat_h, feat_w = hm.shape[:]
n, c, feat_h, feat_w = paddle.shape(hm)
padw = (feat_w * self.down_ratio - im_shape[0, 1]) / 2
padh = (feat_h * self.down_ratio - im_shape[0, 0]) / 2
x1 = x1 * self.down_ratio
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册