245 lines
9.1 KiB
Python
245 lines
9.1 KiB
Python
# Copyright (c) 2020 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.
|
|
|
|
import numpy as np
|
|
import paddle
|
|
import paddle.nn.functional as F
|
|
from ppdet.core.workspace import register
|
|
from .transformers import bbox_cxcywh_to_xyxy
|
|
|
|
__all__ = [
|
|
'DETRPostProcess',
|
|
]
|
|
|
|
@register
|
|
class DETRPostProcess(object):
|
|
__shared__ = ['num_classes', 'use_focal_loss', 'with_mask']
|
|
__inject__ = []
|
|
|
|
def __init__(self,
|
|
num_classes=80,
|
|
num_top_queries=100,
|
|
dual_queries=False,
|
|
dual_groups=0,
|
|
use_focal_loss=False,
|
|
with_mask=False,
|
|
mask_threshold=0.5,
|
|
use_avg_mask_score=False,
|
|
bbox_decode_type='origin'):
|
|
super(DETRPostProcess, self).__init__()
|
|
assert bbox_decode_type in ['origin', 'pad']
|
|
|
|
self.num_classes = num_classes
|
|
self.num_top_queries = num_top_queries
|
|
self.dual_queries = dual_queries
|
|
self.dual_groups = dual_groups
|
|
self.use_focal_loss = use_focal_loss
|
|
self.with_mask = with_mask
|
|
self.mask_threshold = mask_threshold
|
|
self.use_avg_mask_score = use_avg_mask_score
|
|
self.bbox_decode_type = bbox_decode_type
|
|
|
|
def _mask_postprocess(self, mask_pred, score_pred, index):
|
|
mask_score = F.sigmoid(paddle.gather_nd(mask_pred, index))
|
|
mask_pred = (mask_score > self.mask_threshold).astype(mask_score.dtype)
|
|
if self.use_avg_mask_score:
|
|
avg_mask_score = (mask_pred * mask_score).sum([-2, -1]) / (
|
|
mask_pred.sum([-2, -1]) + 1e-6)
|
|
score_pred *= avg_mask_score
|
|
|
|
return mask_pred[0].astype('int32'), score_pred
|
|
|
|
def __call__(self, head_out, im_shape, scale_factor, pad_shape):
|
|
"""
|
|
Decode the bbox and mask.
|
|
|
|
Args:
|
|
head_out (tuple): bbox_pred, cls_logit and masks of bbox_head output.
|
|
im_shape (Tensor): The shape of the input image without padding.
|
|
scale_factor (Tensor): The scale factor of the input image.
|
|
pad_shape (Tensor): The shape of the input image with padding.
|
|
Returns:
|
|
bbox_pred (Tensor): The output prediction with shape [N, 6], including
|
|
labels, scores and bboxes. The size of bboxes are corresponding
|
|
to the input image, the bboxes may be used in other branch.
|
|
bbox_num (Tensor): The number of prediction boxes of each batch with
|
|
shape [bs], and is N.
|
|
"""
|
|
bboxes, logits, masks = head_out
|
|
if self.dual_queries:
|
|
num_queries = logits.shape[1]
|
|
logits, bboxes = logits[:, :int(num_queries // (self.dual_groups + 1)), :], \
|
|
bboxes[:, :int(num_queries // (self.dual_groups + 1)), :]
|
|
|
|
bbox_pred = bbox_cxcywh_to_xyxy(bboxes)
|
|
# calculate the original shape of the image
|
|
origin_shape = paddle.floor(im_shape / scale_factor + 0.5)
|
|
img_h, img_w = paddle.split(origin_shape, 2, axis=-1)
|
|
if self.bbox_decode_type == 'pad':
|
|
# calculate the shape of the image with padding
|
|
out_shape = pad_shape / im_shape * origin_shape
|
|
out_shape = out_shape.flip(1).tile([1, 2]).unsqueeze(1)
|
|
elif self.bbox_decode_type == 'origin':
|
|
out_shape = origin_shape.flip(1).tile([1, 2]).unsqueeze(1)
|
|
else:
|
|
raise Exception(
|
|
f'Wrong `bbox_decode_type`: {self.bbox_decode_type}.')
|
|
bbox_pred *= out_shape
|
|
|
|
scores = F.sigmoid(logits) if self.use_focal_loss else F.softmax(
|
|
logits)[:, :, :-1]
|
|
|
|
if not self.use_focal_loss:
|
|
scores, labels = scores.max(-1), scores.argmax(-1)
|
|
if scores.shape[1] > self.num_top_queries:
|
|
scores, index = paddle.topk(
|
|
scores, self.num_top_queries, axis=-1)
|
|
batch_ind = paddle.arange(
|
|
end=scores.shape[0]).unsqueeze(-1).tile(
|
|
[1, self.num_top_queries])
|
|
index = paddle.stack([batch_ind, index], axis=-1)
|
|
labels = paddle.gather_nd(labels, index)
|
|
bbox_pred = paddle.gather_nd(bbox_pred, index)
|
|
else:
|
|
scores, index = paddle.topk(
|
|
scores.flatten(1), self.num_top_queries, axis=-1)
|
|
labels = index % self.num_classes
|
|
index = index // self.num_classes
|
|
batch_ind = paddle.arange(end=scores.shape[0]).unsqueeze(-1).tile(
|
|
[1, self.num_top_queries])
|
|
index = paddle.stack([batch_ind, index], axis=-1)
|
|
bbox_pred = paddle.gather_nd(bbox_pred, index)
|
|
|
|
mask_pred = None
|
|
if self.with_mask:
|
|
assert masks is not None
|
|
masks = F.interpolate(
|
|
masks, scale_factor=4, mode="bilinear", align_corners=False)
|
|
# TODO: Support prediction with bs>1.
|
|
# remove padding for input image
|
|
h, w = im_shape.astype('int32')[0]
|
|
masks = masks[..., :h, :w]
|
|
# get pred_mask in the original resolution.
|
|
img_h = img_h[0].astype('int32')
|
|
img_w = img_w[0].astype('int32')
|
|
masks = F.interpolate(
|
|
masks,
|
|
size=(img_h, img_w),
|
|
mode="bilinear",
|
|
align_corners=False)
|
|
mask_pred, scores = self._mask_postprocess(masks, scores, index)
|
|
|
|
bbox_pred = paddle.concat(
|
|
[
|
|
labels.unsqueeze(-1).astype('float32'), scores.unsqueeze(-1),
|
|
bbox_pred
|
|
],
|
|
axis=-1)
|
|
bbox_num = paddle.to_tensor(
|
|
self.num_top_queries, dtype='int32').tile([bbox_pred.shape[0]])
|
|
bbox_pred = bbox_pred.reshape([-1, 6])
|
|
return bbox_pred, bbox_num, mask_pred
|
|
|
|
|
|
|
|
def paste_mask(masks, boxes, im_h, im_w, assign_on_cpu=False):
|
|
"""
|
|
Paste the mask prediction to the original image.
|
|
"""
|
|
x0_int, y0_int = 0, 0
|
|
x1_int, y1_int = im_w, im_h
|
|
x0, y0, x1, y1 = paddle.split(boxes, 4, axis=1)
|
|
N = masks.shape[0]
|
|
img_y = paddle.arange(y0_int, y1_int) + 0.5
|
|
img_x = paddle.arange(x0_int, x1_int) + 0.5
|
|
|
|
img_y = (img_y - y0) / (y1 - y0) * 2 - 1
|
|
img_x = (img_x - x0) / (x1 - x0) * 2 - 1
|
|
# img_x, img_y have shapes (N, w), (N, h)
|
|
|
|
if assign_on_cpu:
|
|
paddle.set_device('cpu')
|
|
gx = img_x[:, None, :].expand(
|
|
[N, paddle.shape(img_y)[1], paddle.shape(img_x)[1]])
|
|
gy = img_y[:, :, None].expand(
|
|
[N, paddle.shape(img_y)[1], paddle.shape(img_x)[1]])
|
|
grid = paddle.stack([gx, gy], axis=3)
|
|
img_masks = F.grid_sample(masks, grid, align_corners=False)
|
|
return img_masks[:, 0]
|
|
|
|
|
|
def multiclass_nms(bboxs, num_classes, match_threshold=0.6, match_metric='iou'):
|
|
final_boxes = []
|
|
for c in range(num_classes):
|
|
idxs = bboxs[:, 0] == c
|
|
if np.count_nonzero(idxs) == 0: continue
|
|
r = nms(bboxs[idxs, 1:], match_threshold, match_metric)
|
|
final_boxes.append(np.concatenate([np.full((r.shape[0], 1), c), r], 1))
|
|
return final_boxes
|
|
|
|
|
|
def nms(dets, match_threshold=0.6, match_metric='iou'):
|
|
""" Apply NMS to avoid detecting too many overlapping bounding boxes.
|
|
Args:
|
|
dets: shape [N, 5], [score, x1, y1, x2, y2]
|
|
match_metric: 'iou' or 'ios'
|
|
match_threshold: overlap thresh for match metric.
|
|
"""
|
|
if dets.shape[0] == 0:
|
|
return dets[[], :]
|
|
scores = dets[:, 0]
|
|
x1 = dets[:, 1]
|
|
y1 = dets[:, 2]
|
|
x2 = dets[:, 3]
|
|
y2 = dets[:, 4]
|
|
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
|
|
order = scores.argsort()[::-1]
|
|
|
|
ndets = dets.shape[0]
|
|
suppressed = np.zeros((ndets), dtype=np.int32)
|
|
|
|
for _i in range(ndets):
|
|
i = order[_i]
|
|
if suppressed[i] == 1:
|
|
continue
|
|
ix1 = x1[i]
|
|
iy1 = y1[i]
|
|
ix2 = x2[i]
|
|
iy2 = y2[i]
|
|
iarea = areas[i]
|
|
for _j in range(_i + 1, ndets):
|
|
j = order[_j]
|
|
if suppressed[j] == 1:
|
|
continue
|
|
xx1 = max(ix1, x1[j])
|
|
yy1 = max(iy1, y1[j])
|
|
xx2 = min(ix2, x2[j])
|
|
yy2 = min(iy2, y2[j])
|
|
w = max(0.0, xx2 - xx1 + 1)
|
|
h = max(0.0, yy2 - yy1 + 1)
|
|
inter = w * h
|
|
if match_metric == 'iou':
|
|
union = iarea + areas[j] - inter
|
|
match_value = inter / union
|
|
elif match_metric == 'ios':
|
|
smaller = min(iarea, areas[j])
|
|
match_value = inter / smaller
|
|
else:
|
|
raise ValueError()
|
|
if match_value >= match_threshold:
|
|
suppressed[j] = 1
|
|
keep = np.where(suppressed == 0)[0]
|
|
dets = dets[keep, :]
|
|
return dets
|