-
Notifications
You must be signed in to change notification settings - Fork 6
/
resnest_cascade_rcnn.py
221 lines (184 loc) · 7.66 KB
/
resnest_cascade_rcnn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding:utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import megengine
import numpy as np
import megengine.functional as F
import megengine.module as M
from functools import partial
import layers
from layers.det import ResNeSt
class ResNeStCascadeRCNN(M.Module):
def __init__(self, cfg):
super().__init__()
self.cfg = cfg
# ----------------------- build backbone ------------------------ #
bottom_up = ResNeSt(
stem_channels=128,
depth=101,
radix=2,
reduction_factor=4,
avg_down_stride=True,
num_stages=4,
out_indices=(0, 1, 2, 3),
frozen_stages=1,
norm_cfg=dict(type='SyncBN', requires_grad=True),
norm_eval=False,
style='pytorch',
dcn=getattr(cfg, 'backbone_dcn', None),
stage_with_dcn=getattr(cfg, 'backbone_stage_with_dcn', [False, False, False, False])
)
bottom_up.init_weights('./pretrained_weights/CASCADE-S-101-FPN.pkl')
# ----------------------- build FPN ----------------------------- #
self.backbone = layers.FPN(
bottom_up=bottom_up,
in_features=cfg.fpn_in_features,
out_channels=cfg.fpn_out_channels,
norm=cfg.fpn_norm,
top_block=layers.FPNP6(),
strides=cfg.fpn_in_strides,
channels=cfg.fpn_in_channels,
)
# ----------------------- build RPN ----------------------------- #
self.rpn = layers.RPN(cfg)
# ----------------------- build RCNN head ----------------------- #
self.rcnn = layers.CascadeRCNN(cfg)
def preprocess_image(self, image):
padded_image = layers.get_padded_tensor(image, 32, 0.0)
return padded_image
def forward(self, image, im_info, gt_boxes=None):
image = self.preprocess_image(image)
features = self.backbone(image)
if self.training:
return self._forward_train(features, im_info, gt_boxes)
else:
return self.inference(features, im_info)
def _forward_train(self, features, im_info, gt_boxes):
loss_dict = {
"total_loss" : None
}
rpn_rois, rpn_losses = self.rpn(features, im_info, gt_boxes)
total_loss_temp = {
'rpn_cls': rpn_losses["loss_rpn_cls"],
'rpn_bbox': rpn_losses["loss_rpn_bbox"],
}
# rpn_rois = np.ascontiguousarray(rpn_rois.detach().numpy())
# rpn_rois = megengine.tensor(rpn_rois).astype('float32')
rcnn_losses = self.rcnn(features, rpn_rois, im_info, gt_boxes)
total_loss_temp.update(rcnn_losses)
total_loss = sum(total_loss_temp.values())
total_loss_temp["total_loss"] = total_loss
loss_dict.update(total_loss_temp)
self.cfg.losses_keys = list(loss_dict.keys())
return loss_dict
def inference(self, features, im_info):
rpn_rois = self.rpn(features, im_info)
pred_boxes, pred_score, pred_label = self.rcnn(features, rpn_rois, im_info)
pred_boxes = pred_boxes.reshape(-1, 4)
scale_w = im_info[0, 1] / im_info[0, 3]
scale_h = im_info[0, 0] / im_info[0, 2]
pred_boxes = pred_boxes / F.concat([scale_w, scale_h, scale_w, scale_h], axis=0)
if pred_boxes.shape[0] != 0:
pred_boxes = layers.get_clipped_boxes(
pred_boxes, im_info[0, 2:4]
)
return pred_score, pred_boxes, pred_label
class ResNeStCascadeRCNNConfig:
# pylint: disable=too-many-statements
def __init__(self):
self.backbone_freeze_at = 2
self.fpn_norm = None
self.fpn_in_features = [0, 1, 2, 3]
self.fpn_in_strides = [4, 8, 16, 32]
self.fpn_in_channels = [256, 512, 1024, 2048]
self.fpn_out_channels = 256
# ------------------------ data cfg -------------------------- #
self.train_dataset = dict(
name="coco",
root="train2017",
ann_file="annotations/instances_train2017.json",
remove_images_without_annotations=True,
)
self.test_dataset = dict(
name="coco",
root="val2017",
ann_file="annotations/instances_val2017.json",
remove_images_without_annotations=False,
)
self.num_classes = 5
self.img_mean = [103.530, 116.280, 123.675] # BGR
self.img_std = [57.375, 57.120, 58.395]
# ----------------------- rpn cfg ------------------------- #
self.rpn_stride = [4, 8, 16, 32, 64]
# self.rpn_stride = [1, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
self.rpn_in_features = ["p2", "p3", "p4", "p5", "p6"]
self.rpn_channel = 256
self.rpn_reg_mean = [0.0, 0.0, 0.0, 0.0]
self.rpn_reg_std = [1.0, 1.0, 1.0, 1.0]
self.anchor_scales = [[x] for x in [32, 64, 128, 256, 512]]
self.anchor_ratios = [[0.5, 1, 2]]
self.anchor_offset = 0.5
self.match_thresholds = [0.3, 0.7]
self.match_labels = [0, -1, 1]
self.match_allow_low_quality = True
self.rpn_nms_threshold = 0.7
self.num_sample_anchors = 256
self.positive_anchor_ratio = 0.5
# ----------------------- rcnn cfg ------------------------- #
self.loss_rcnn_cls = partial(F.loss.cross_entropy, axis=1)
self.rcnn_stride = [4, 8, 16, 32]
self.rcnn_in_features = ["p2", "p3", "p4", "p5"]
self.cascade_num_stages = 3
self.rcnn_reg_mean = [
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
]
self.rcnn_reg_std = [
[0.1, 0.1, 0.2, 0.2],
[0.05, 0.05, 0.1, 0.1],
[0.033, 0.033, 0.067, 0.067],
]
self.pooling_method = "roi_align"
self.pooling_size = (7, 7)
self.num_rois = 512
self.fg_ratio = [0.25, 0.25, 0.25]
self.fg_threshold = [0.5, 0.6, 0.7]
self.bg_threshold_high = [0.5, 0.6, 0.7]
self.bg_threshold_low = [0., 0., 0.]
self.class_aware_box = False
# ------------------------ loss cfg -------------------------- #
self.rpn_smooth_l1_beta = 0 # use L1 loss
self.rcnn_smooth_l1_beta = 0.0 # use L1 loss
self.num_losses = self.cascade_num_stages * 2 + 3
# ------------------------ training cfg ---------------------- #
self.train_image_short_size = (640, 672, 704, 736, 768, 800)
self.train_image_max_size = 1333
# self.train_image_short_size = (1184)
# self.train_image_max_size = 1600
self.train_prev_nms_top_n = 2000
self.train_post_nms_top_n = 1000
self.basic_lr = 0.02 / 16 # The basic learning rate for single-image
self.momentum = 0.9
self.weight_decay = 1e-4
self.log_interval = 20
self.nr_images_epoch = 80000
self.max_epoch = 24
self.warm_iters = 500
self.lr_decay_rate = 0.1
self.lr_decay_stages = [16, 24]
# ------------------------ testing cfg ----------------------- #
self.test_image_short_size = 800
self.test_image_max_size = 1333
self.test_prev_nms_top_n = 1000
self.test_post_nms_top_n = 1000
self.test_max_boxes_per_image = 100
self.test_vis_threshold = 0.3
self.test_cls_threshold = 0.05
self.test_nms = 0.5
self.final_nms_method = 'normal'