Skip to content

Commit efbbde2

Browse files
ruisvirexyc
andauthored
add is_resize_mask cfg in inst seg (#2229)
* add is_resize_mask cfg in inst seg * Update csrc/mmdeploy/codebase/mmdet/instance_segmentation.cpp set is_resize_mask default fasle Co-authored-by: Chen Xin <[email protected]> * Update mmdeploy/codebase/mmdet/deploy/object_detection.py set is_resize_mask default fasle Co-authored-by: Chen Xin <[email protected]> * fix format --------- Co-authored-by: Chen Xin <[email protected]>
1 parent 6cd29e2 commit efbbde2

File tree

2 files changed

+42
-37
lines changed

2 files changed

+42
-37
lines changed

csrc/mmdeploy/codebase/mmdet/instance_segmentation.cpp

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ResizeInstanceMask : public ResizeBBox {
1717
if (cfg.contains("params")) {
1818
mask_thr_binary_ = cfg["params"].value("mask_thr_binary", mask_thr_binary_);
1919
is_rcnn_ = cfg["params"].contains("rcnn");
20+
is_resize_mask_ = cfg["params"].value("is_resize_mask", is_resize_mask_);
2021
}
2122
operation::Context ctx(device_, stream_);
2223
warp_affine_ = operation::Managed<operation::WarpAffine>::Create("bilinear");
@@ -95,44 +96,46 @@ class ResizeInstanceMask : public ResizeBBox {
9596
auto mask = d_mask.Slice(det.index);
9697
auto mask_height = (int)mask.shape(1);
9798
auto mask_width = (int)mask.shape(2);
98-
auto& bbox = det.bbox;
99-
// same as mmdet with skip_empty = True
100-
auto x0 = std::max(std::floor(bbox[0]) - 1, 0.f);
101-
auto y0 = std::max(std::floor(bbox[1]) - 1, 0.f);
102-
auto x1 = std::min(std::ceil(bbox[2]) + 1, (float)img_w);
103-
auto y1 = std::min(std::ceil(bbox[3]) + 1, (float)img_h);
104-
auto width = static_cast<int>(x1 - x0);
105-
auto height = static_cast<int>(y1 - y0);
106-
// params align_corners = False
107-
float fx;
108-
float fy;
109-
float tx;
110-
float ty;
111-
if (is_rcnn_) { // mask r-cnn
112-
fx = (float)mask_width / (bbox[2] - bbox[0]);
113-
fy = (float)mask_height / (bbox[3] - bbox[1]);
114-
tx = (x0 + .5f - bbox[0]) * fx - .5f;
115-
ty = (y0 + .5f - bbox[1]) * fy - .5f;
116-
} else { // rtmdet-ins
117-
auto raw_bbox = cpu_dets.Slice(det.index);
118-
auto raw_bbox_data = raw_bbox.data<float>();
119-
fx = (raw_bbox_data[2] - raw_bbox_data[0]) / (bbox[2] - bbox[0]);
120-
fy = (raw_bbox_data[3] - raw_bbox_data[1]) / (bbox[3] - bbox[1]);
121-
tx = (x0 + .5f - bbox[0]) * fx - .5f + raw_bbox_data[0];
122-
ty = (y0 + .5f - bbox[1]) * fy - .5f + raw_bbox_data[1];
123-
}
124-
125-
float affine_matrix[] = {fx, 0, tx, 0, fy, ty};
126-
127-
cv::Mat_<float> m(2, 3, affine_matrix);
128-
cv::invertAffineTransform(m, m);
129-
13099
mask.Reshape({1, mask_height, mask_width, 1});
131-
132-
Tensor& warped_mask = warped_masks.emplace_back();
133-
OUTCOME_TRY(warp_affine_.Apply(mask, warped_mask, affine_matrix, height, width));
134-
135-
OUTCOME_TRY(CopyToHost(warped_mask, h_warped_masks.emplace_back()));
100+
if (is_resize_mask_) {
101+
auto& bbox = det.bbox;
102+
// same as mmdet with skip_empty = True
103+
auto x0 = std::max(std::floor(bbox[0]) - 1, 0.f);
104+
auto y0 = std::max(std::floor(bbox[1]) - 1, 0.f);
105+
auto x1 = std::min(std::ceil(bbox[2]) + 1, (float)img_w);
106+
auto y1 = std::min(std::ceil(bbox[3]) + 1, (float)img_h);
107+
auto width = static_cast<int>(x1 - x0);
108+
auto height = static_cast<int>(y1 - y0);
109+
// params align_corners = False
110+
float fx;
111+
float fy;
112+
float tx;
113+
float ty;
114+
if (is_rcnn_) { // mask r-cnn
115+
fx = (float)mask_width / (bbox[2] - bbox[0]);
116+
fy = (float)mask_height / (bbox[3] - bbox[1]);
117+
tx = (x0 + .5f - bbox[0]) * fx - .5f;
118+
ty = (y0 + .5f - bbox[1]) * fy - .5f;
119+
} else { // rtmdet-ins
120+
auto raw_bbox = cpu_dets.Slice(det.index);
121+
auto raw_bbox_data = raw_bbox.data<float>();
122+
fx = (raw_bbox_data[2] - raw_bbox_data[0]) / (bbox[2] - bbox[0]);
123+
fy = (raw_bbox_data[3] - raw_bbox_data[1]) / (bbox[3] - bbox[1]);
124+
tx = (x0 + .5f - bbox[0]) * fx - .5f + raw_bbox_data[0];
125+
ty = (y0 + .5f - bbox[1]) * fy - .5f + raw_bbox_data[1];
126+
}
127+
128+
float affine_matrix[] = {fx, 0, tx, 0, fy, ty};
129+
130+
cv::Mat_<float> m(2, 3, affine_matrix);
131+
cv::invertAffineTransform(m, m);
132+
Tensor& warped_mask = warped_masks.emplace_back();
133+
OUTCOME_TRY(warp_affine_.Apply(mask, warped_mask, affine_matrix, height, width));
134+
OUTCOME_TRY(CopyToHost(warped_mask, h_warped_masks.emplace_back()));
135+
136+
} else {
137+
OUTCOME_TRY(CopyToHost(mask, h_warped_masks.emplace_back()));
138+
}
136139
}
137140

138141
OUTCOME_TRY(stream_.Wait());
@@ -165,6 +168,7 @@ class ResizeInstanceMask : public ResizeBBox {
165168
operation::Managed<operation::WarpAffine> warp_affine_;
166169
float mask_thr_binary_{.5f};
167170
bool is_rcnn_{true};
171+
bool is_resize_mask_{false};
168172
};
169173

170174
MMDEPLOY_REGISTER_CODEBASE_COMPONENT(MMDetection, ResizeInstanceMask);

mmdeploy/codebase/mmdet/deploy/object_detection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ def get_postprocess(self, *args, **kwargs) -> Dict:
316316
params['mask_thr_binary'] = params['rcnn']['mask_thr_binary']
317317
if 'mask_thr_binary' in params:
318318
type = 'ResizeInstanceMask' # for instance-seg
319+
params['is_resize_mask'] = False # resize and crop mask default
319320
if get_backend(self.deploy_cfg) == Backend.RKNN:
320321
if 'YOLO' in self.model_cfg.model.type or \
321322
'RTMDet' in self.model_cfg.model.type:

0 commit comments

Comments
 (0)