Skip to content

Commit

Permalink
refine fcos codes (PaddlePaddle#6949)
Browse files Browse the repository at this point in the history
* refine fcos codes

* refine fcos postprocess

* fix fcos deploy

* fix fcos deploy
  • Loading branch information
nemonameless authored Sep 17, 2022
1 parent 9207871 commit 80b2627
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 259 deletions.
35 changes: 15 additions & 20 deletions configs/fcos/_base_/fcos_r50_fpn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ FCOS:
backbone: ResNet
neck: FPN
fcos_head: FCOSHead
fcos_post_process: FCOSPostProcess

ResNet:
# index 0 stands for res2
depth: 50
variant: 'b'
norm_type: bn
freeze_at: 0
return_idx: [1,2,3]
freeze_at: 0 # res2
return_idx: [1, 2, 3]
num_stages: 4

FPN:
out_channel: 256
spatial_scales: [0.125, 0.0625, 0.03125]
extra_stage: 2
has_extra_convs: true
use_c5: false
has_extra_convs: True
use_c5: False

FCOSHead:
fcos_feat:
Expand All @@ -29,22 +28,18 @@ FCOSHead:
feat_out: 256
num_convs: 4
norm_type: "gn"
use_dcn: false
use_dcn: False
fpn_stride: [8, 16, 32, 64, 128]
prior_prob: 0.01
fcos_loss: FCOSLoss
norm_reg_targets: true
centerness_on_reg: true

FCOSLoss:
loss_alpha: 0.25
loss_gamma: 2.0
iou_loss_type: "giou"
reg_weights: 1.0

FCOSPostProcess:
decode:
name: FCOSBox
norm_reg_targets: True
centerness_on_reg: True
num_shift: 0.5
fcos_loss:
name: FCOSLoss
loss_alpha: 0.25
loss_gamma: 2.0
iou_loss_type: "giou"
reg_weights: 1.0
nms:
name: MultiClassNMS
nms_top_k: 1000
Expand Down
49 changes: 24 additions & 25 deletions configs/fcos/_base_/fcos_reader.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,41 @@
worker_num: 2
TrainReader:
sample_transforms:
- Decode: {}
- RandomFlip: {prob: 0.5}
- NormalizeImage: {is_scale: true, mean: [0.485,0.456,0.406], std: [0.229, 0.224,0.225]}
- Resize: {target_size: [800, 1333], keep_ratio: true, interp: 1}
- Permute: {}
- Decode: {}
- Resize: {target_size: [800, 1333], keep_ratio: True, interp: 1}
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
- RandomFlip: {}
batch_transforms:
- PadBatch: {pad_to_stride: 128}
- Gt2FCOSTarget:
object_sizes_boundary: [64, 128, 256, 512]
center_sampling_radius: 1.5
downsample_ratios: [8, 16, 32, 64, 128]
norm_reg_targets: True
- Permute: {}
- PadBatch: {pad_to_stride: 128}
- Gt2FCOSTarget:
object_sizes_boundary: [64, 128, 256, 512]
center_sampling_radius: 1.5
downsample_ratios: [8, 16, 32, 64, 128]
norm_reg_targets: True
batch_size: 2
shuffle: true
drop_last: true
shuffle: True
drop_last: True


EvalReader:
sample_transforms:
- Decode: {}
- NormalizeImage: {is_scale: true, mean: [0.485,0.456,0.406], std: [0.229, 0.224,0.225]}
- Resize: {interp: 1, target_size: [800, 1333], keep_ratio: True}
- Permute: {}
- Decode: {}
- Resize: {target_size: [800, 1333], keep_ratio: True, interp: 1}
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
- Permute: {}
batch_transforms:
- PadBatch: {pad_to_stride: 128}
- PadBatch: {pad_to_stride: 128}
batch_size: 1
shuffle: false


TestReader:
sample_transforms:
- Decode: {}
- NormalizeImage: {is_scale: true, mean: [0.485,0.456,0.406], std: [0.229, 0.224,0.225]}
- Resize: {interp: 1, target_size: [800, 1333], keep_ratio: True}
- Permute: {}
- Decode: {}
- Resize: {target_size: [800, 1333], keep_ratio: True, interp: 1}
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
- Permute: {}
batch_transforms:
- PadBatch: {pad_to_stride: 128}
- PadBatch: {pad_to_stride: 128}
batch_size: 1
shuffle: false
fuse_normalize: True
20 changes: 2 additions & 18 deletions configs/fcos/fcos_dcn_r50_fpn_1x_coco.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,8 @@ _BASE_: [
weights: output/fcos_dcn_r50_fpn_1x_coco/model_final

ResNet:
depth: 50
norm_type: bn
freeze_at: 0
return_idx: [1,2,3]
num_stages: 4
dcn_v2_stages: [1,2,3]
dcn_v2_stages: [1, 2, 3]

FCOSHead:
fcos_feat:
name: FCOSFeat
feat_in: 256
feat_out: 256
num_convs: 4
norm_type: "gn"
use_dcn: true
num_classes: 80
fpn_stride: [8, 16, 32, 64, 128]
prior_prob: 0.01
fcos_loss: FCOSLoss
norm_reg_targets: true
centerness_on_reg: true
use_dcn: True
26 changes: 13 additions & 13 deletions configs/fcos/fcos_r50_fpn_multiscale_2x_coco.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ weights: output/fcos_r50_fpn_multiscale_2x_coco/model_final

TrainReader:
sample_transforms:
- Decode: {}
- RandomFlip: {prob: 0.5}
- NormalizeImage: {is_scale: true, mean: [0.485,0.456,0.406], std: [0.229, 0.224,0.225]}
- RandomResize: {target_size: [[640, 1333], [672, 1333], [704, 1333], [736, 1333], [768, 1333], [800, 1333]], keep_ratio: true, interp: 1}
- Permute: {}
- Decode: {}
- RandomResize: {target_size: [[640, 1333], [672, 1333], [704, 1333], [736, 1333], [768, 1333], [800, 1333]], keep_ratio: True, interp: 1}
- NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
- RandomFlip: {}
batch_transforms:
- PadBatch: {pad_to_stride: 128}
- Gt2FCOSTarget:
object_sizes_boundary: [64, 128, 256, 512]
center_sampling_radius: 1.5
downsample_ratios: [8, 16, 32, 64, 128]
norm_reg_targets: True
- Permute: {}
- PadBatch: {pad_to_stride: 128}
- Gt2FCOSTarget:
object_sizes_boundary: [64, 128, 256, 512]
center_sampling_radius: 1.5
downsample_ratios: [8, 16, 32, 64, 128]
norm_reg_targets: True
batch_size: 2
shuffle: true
drop_last: true
shuffle: True
drop_last: True

epoch: 24

Expand Down
53 changes: 13 additions & 40 deletions ppdet/modeling/architectures/fcos.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,15 @@ class FCOS(BaseArch):
backbone (object): backbone instance
neck (object): 'FPN' instance
fcos_head (object): 'FCOSHead' instance
post_process (object): 'FCOSPostProcess' instance
"""

__category__ = 'architecture'
__inject__ = ['fcos_post_process']

def __init__(self,
backbone,
neck,
fcos_head='FCOSHead',
fcos_post_process='FCOSPostProcess'):
def __init__(self, backbone, neck='FPN', fcos_head='FCOSHead'):
super(FCOS, self).__init__()
self.backbone = backbone
self.neck = neck
self.fcos_head = fcos_head
self.fcos_post_process = fcos_post_process

@classmethod
def from_config(cls, cfg, *args, **kwargs):
Expand All @@ -68,38 +61,18 @@ def from_config(cls, cfg, *args, **kwargs):
def _forward(self):
body_feats = self.backbone(self.inputs)
fpn_feats = self.neck(body_feats)
fcos_head_outs = self.fcos_head(fpn_feats, self.training)
if not self.training:
scale_factor = self.inputs['scale_factor']
bboxes = self.fcos_post_process(fcos_head_outs, scale_factor)
return bboxes

if self.training:
losses = self.fcos_head(fpn_feats, self.inputs)
return losses
else:
return fcos_head_outs

def get_loss(self, ):
loss = {}
tag_labels, tag_bboxes, tag_centerness = [], [], []
for i in range(len(self.fcos_head.fpn_stride)):
# labels, reg_target, centerness
k_lbl = 'labels{}'.format(i)
if k_lbl in self.inputs:
tag_labels.append(self.inputs[k_lbl])
k_box = 'reg_target{}'.format(i)
if k_box in self.inputs:
tag_bboxes.append(self.inputs[k_box])
k_ctn = 'centerness{}'.format(i)
if k_ctn in self.inputs:
tag_centerness.append(self.inputs[k_ctn])

fcos_head_outs = self._forward()
loss_fcos = self.fcos_head.get_loss(fcos_head_outs, tag_labels,
tag_bboxes, tag_centerness)
loss.update(loss_fcos)
total_loss = paddle.add_n(list(loss.values()))
loss.update({'loss': total_loss})
return loss
fcos_head_outs = self.fcos_head(fpn_feats)
bbox_pred, bbox_num = self.fcos_head.post_process(
fcos_head_outs, self.inputs['scale_factor'])
return {'bbox': bbox_pred, 'bbox_num': bbox_num}

def get_loss(self):
return self._forward()

def get_pred(self):
bbox_pred, bbox_num = self._forward()
output = {'bbox': bbox_pred, 'bbox_num': bbox_num}
return output
return self._forward()
Loading

0 comments on commit 80b2627

Please sign in to comment.