未验证 提交 51574ea1 编写于 作者: C cnn 提交者: GitHub

[dev] reorganize code of s2anet_head (#3481)

* reorganize the code, and fix bug

* fix typo

* remve comment

* reorganize code

* Revert "reorganize code"

This reverts commit 4f928c3a46b264ba8815978b914bf81a46b66b62.

* set weight

* set default config

* fix code style
上级 4f96dc2f
...@@ -36,6 +36,8 @@ S2ANetHead: ...@@ -36,6 +36,8 @@ S2ANetHead:
align_conv_type: 'Conv' # AlignConv Conv align_conv_type: 'Conv' # AlignConv Conv
align_conv_size: 3 align_conv_size: 3
use_sigmoid_cls: True use_sigmoid_cls: True
reg_loss_weight: [ 1.0, 1.0, 1.0, 1.0, 1.1 ]
cls_loss_weight: [ 1.1, 1.05 ]
RBoxAssigner: RBoxAssigner:
pos_iou_thr: 0.5 pos_iou_thr: 0.5
...@@ -52,4 +54,3 @@ S2ANetBBoxPostProcess: ...@@ -52,4 +54,3 @@ S2ANetBBoxPostProcess:
score_threshold: 0.05 score_threshold: 0.05
nms_threshold: 0.1 nms_threshold: 0.1
normalized: False normalized: False
#background_label: -1
_BASE_: [ it _BASE_: [
'../datasets/dota.yml', '../datasets/dota.yml',
'../runtime.yml', '../runtime.yml',
'_base_/s2anet_optimizer_1x.yml', '_base_/s2anet_optimizer_1x.yml',
...@@ -6,3 +6,18 @@ _BASE_: [ ...@@ -6,3 +6,18 @@ _BASE_: [
'_base_/s2anet_reader.yml', '_base_/s2anet_reader.yml',
] ]
weights: output/s2anet_1x_dota/model_final weights: output/s2anet_1x_dota/model_final
S2ANetHead:
anchor_strides: [8, 16, 32, 64, 128]
anchor_scales: [4]
anchor_ratios: [1.0]
anchor_assign: RBoxAssigner
stacked_convs: 2
feat_in: 256
feat_out: 256
num_classes: 15
align_conv_type: 'AlignConv' # AlignConv Conv
align_conv_size: 3
use_sigmoid_cls: True
reg_loss_weight: [1.0, 1.0, 1.0, 1.0, 1.1]
cls_loss_weight: [1.1, 1.05]
...@@ -19,3 +19,5 @@ S2ANetHead: ...@@ -19,3 +19,5 @@ S2ANetHead:
align_conv_type: 'Conv' # AlignConv Conv align_conv_type: 'Conv' # AlignConv Conv
align_conv_size: 3 align_conv_size: 3
use_sigmoid_cls: True use_sigmoid_cls: True
reg_loss_weight: [1.0, 1.0, 1.0, 1.0, 1.1]
cls_loss_weight: [1.1, 1.05]
...@@ -267,6 +267,150 @@ def bbox_iou(box1, box2, giou=False, diou=False, ciou=False, eps=1e-9): ...@@ -267,6 +267,150 @@ def bbox_iou(box1, box2, giou=False, diou=False, ciou=False, eps=1e-9):
return iou return iou
def rect2rbox(bboxes):
"""
:param bboxes: shape (n, 4) (xmin, ymin, xmax, ymax)
:return: dbboxes: shape (n, 5) (x_ctr, y_ctr, w, h, angle)
"""
bboxes = bboxes.reshape(-1, 4)
num_boxes = bboxes.shape[0]
x_ctr = (bboxes[:, 2] + bboxes[:, 0]) / 2.0
y_ctr = (bboxes[:, 3] + bboxes[:, 1]) / 2.0
edges1 = np.abs(bboxes[:, 2] - bboxes[:, 0])
edges2 = np.abs(bboxes[:, 3] - bboxes[:, 1])
angles = np.zeros([num_boxes], dtype=bboxes.dtype)
inds = edges1 < edges2
rboxes = np.stack((x_ctr, y_ctr, edges1, edges2, angles), axis=1)
rboxes[inds, 2] = edges2[inds]
rboxes[inds, 3] = edges1[inds]
rboxes[inds, 4] = np.pi / 2.0
return rboxes
def delta2rbox(rrois,
deltas,
means=[0, 0, 0, 0, 0],
stds=[1, 1, 1, 1, 1],
wh_ratio_clip=1e-6):
"""
:param rrois: (cx, cy, w, h, theta)
:param deltas: (dx, dy, dw, dh, dtheta)
:param means:
:param stds:
:param wh_ratio_clip:
:return:
"""
means = paddle.to_tensor(means)
stds = paddle.to_tensor(stds)
deltas = paddle.reshape(deltas, [-1, deltas.shape[-1]])
denorm_deltas = deltas * stds + means
dx = denorm_deltas[:, 0]
dy = denorm_deltas[:, 1]
dw = denorm_deltas[:, 2]
dh = denorm_deltas[:, 3]
dangle = denorm_deltas[:, 4]
max_ratio = np.abs(np.log(wh_ratio_clip))
dw = paddle.clip(dw, min=-max_ratio, max=max_ratio)
dh = paddle.clip(dh, min=-max_ratio, max=max_ratio)
rroi_x = rrois[:, 0]
rroi_y = rrois[:, 1]
rroi_w = rrois[:, 2]
rroi_h = rrois[:, 3]
rroi_angle = rrois[:, 4]
gx = dx * rroi_w * paddle.cos(rroi_angle) - dy * rroi_h * paddle.sin(
rroi_angle) + rroi_x
gy = dx * rroi_w * paddle.sin(rroi_angle) + dy * rroi_h * paddle.cos(
rroi_angle) + rroi_y
gw = rroi_w * dw.exp()
gh = rroi_h * dh.exp()
ga = np.pi * dangle + rroi_angle
ga = (ga + np.pi / 4) % np.pi - np.pi / 4
ga = paddle.to_tensor(ga)
gw = paddle.to_tensor(gw, dtype='float32')
gh = paddle.to_tensor(gh, dtype='float32')
bboxes = paddle.stack([gx, gy, gw, gh, ga], axis=-1)
return bboxes
def rbox2delta(proposals, gt, means=[0, 0, 0, 0, 0], stds=[1, 1, 1, 1, 1]):
"""
Args:
proposals:
gt:
means: 1x5
stds: 1x5
Returns:
"""
proposals = proposals.astype(np.float64)
PI = np.pi
gt_widths = gt[..., 2]
gt_heights = gt[..., 3]
gt_angle = gt[..., 4]
proposals_widths = proposals[..., 2]
proposals_heights = proposals[..., 3]
proposals_angle = proposals[..., 4]
coord = gt[..., 0:2] - proposals[..., 0:2]
dx = (np.cos(proposals[..., 4]) * coord[..., 0] + np.sin(proposals[..., 4])
* coord[..., 1]) / proposals_widths
dy = (-np.sin(proposals[..., 4]) * coord[..., 0] + np.cos(proposals[..., 4])
* coord[..., 1]) / proposals_heights
dw = np.log(gt_widths / proposals_widths)
dh = np.log(gt_heights / proposals_heights)
da = (gt_angle - proposals_angle)
da = (da + PI / 4) % PI - PI / 4
da /= PI
deltas = np.stack([dx, dy, dw, dh, da], axis=-1)
means = np.array(means, dtype=deltas.dtype)
stds = np.array(stds, dtype=deltas.dtype)
deltas = (deltas - means) / stds
deltas = deltas.astype(np.float32)
return deltas
def bbox_decode(bbox_preds,
anchors,
means=[0, 0, 0, 0, 0],
stds=[1, 1, 1, 1, 1]):
"""decode bbox from deltas
Args:
bbox_preds: [N,H,W,5]
anchors: [H*W,5]
return:
bboxes: [N,H,W,5]
"""
means = paddle.to_tensor(means)
stds = paddle.to_tensor(stds)
num_imgs, H, W, _ = bbox_preds.shape
bboxes_list = []
for img_id in range(num_imgs):
bbox_pred = bbox_preds[img_id]
# bbox_pred.shape=[5,H,W]
bbox_delta = bbox_pred
anchors = paddle.to_tensor(anchors)
bboxes = delta2rbox(
anchors, bbox_delta, means, stds, wh_ratio_clip=1e-6)
bboxes = paddle.reshape(bboxes, [H, W, 5])
bboxes_list.append(bboxes)
return paddle.stack(bboxes_list, axis=0)
def poly2rbox(polys): def poly2rbox(polys):
""" """
poly:[x0,y0,x1,y1,x2,y2,x3,y3] poly:[x0,y0,x1,y1,x2,y2,x3,y3]
......
此差异已折叠。
...@@ -296,7 +296,7 @@ class RBoxAssigner(object): ...@@ -296,7 +296,7 @@ class RBoxAssigner(object):
anchors = anchors.reshape(-1, anchors.shape[-1]) anchors = anchors.reshape(-1, anchors.shape[-1])
assert anchors.ndim == 2 assert anchors.ndim == 2
anchor_num = anchors.shape[0] anchor_num = anchors.shape[0]
anchor_valid = np.ones((anchor_num), np.uint8) anchor_valid = np.ones((anchor_num), np.int32)
anchor_inds = np.arange(anchor_num) anchor_inds = np.arange(anchor_num)
return anchor_inds return anchor_inds
...@@ -371,9 +371,8 @@ class RBoxAssigner(object): ...@@ -371,9 +371,8 @@ class RBoxAssigner(object):
# calc rbox iou # calc rbox iou
anchors_xc_yc = anchors_xc_yc.astype(np.float32) anchors_xc_yc = anchors_xc_yc.astype(np.float32)
gt_bboxes_xc_yc = gt_bboxes_xc_yc.astype(np.float32) gt_bboxes_xc_yc = gt_bboxes_xc_yc.astype(np.float32)
anchors_xc_yc = paddle.to_tensor(anchors_xc_yc, place=paddle.CPUPlace()) anchors_xc_yc = paddle.to_tensor(anchors_xc_yc)
gt_bboxes_xc_yc = paddle.to_tensor( gt_bboxes_xc_yc = paddle.to_tensor(gt_bboxes_xc_yc)
gt_bboxes_xc_yc, place=paddle.CPUPlace())
try: try:
from rbox_iou_ops import rbox_iou from rbox_iou_ops import rbox_iou
...@@ -433,8 +432,7 @@ class RBoxAssigner(object): ...@@ -433,8 +432,7 @@ class RBoxAssigner(object):
ignore_iof_thr = self.ignore_iof_thr ignore_iof_thr = self.ignore_iof_thr
anchor_num = anchors.shape[0] anchor_num = anchors.shape[0]
anchors_inds = self.anchor_valid(anchors)
anchors = anchors[anchors_inds]
gt_bboxes = gt_bboxes gt_bboxes = gt_bboxes
is_crowd_slice = is_crowd is_crowd_slice = is_crowd
not_crowd_inds = np.where(is_crowd_slice == 0) not_crowd_inds = np.where(is_crowd_slice == 0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册