Closed haoran1062 closed 5 years ago
this is my config base on cascade_mask_rcnn_r50_fpn_1x.py
# model settings
model = dict(
type='CascadeRCNN',
num_stages=3,
pretrained='modelzoo://resnet50',
backbone=dict(
type='ResNet',
depth=50,
num_stages=4,
out_indices=(0, 1, 2, 3),
frozen_stages=1,
style='pytorch'),
neck=dict(
type='FPN',
in_channels=[256, 512, 1024, 2048],
out_channels=256,
num_outs=5),
rpn_head=dict(
type='RPNHead',
in_channels=256,
feat_channels=256,
anchor_scales=[8],
anchor_ratios=[0.5, 1.0, 2.0],
anchor_strides=[4, 8, 16, 32, 64],
target_means=[.0, .0, .0, .0],
target_stds=[1.0, 1.0, 1.0, 1.0],
use_sigmoid_cls=True),
bbox_roi_extractor=dict(
type='SingleRoIExtractor',
roi_layer=dict(type='RoIAlign', out_size=7, sample_num=2),
out_channels=256,
featmap_strides=[4, 8, 16, 32]),
bbox_head=[
dict(
type='SharedFCBBoxHead',
num_fcs=2,
in_channels=256,
fc_out_channels=1024,
roi_feat_size=7,
num_classes=1023,
target_means=[0., 0., 0., 0.],
target_stds=[0.1, 0.1, 0.2, 0.2],
reg_class_agnostic=True),
dict(
type='SharedFCBBoxHead',
num_fcs=2,
in_channels=256,
fc_out_channels=1024,
roi_feat_size=7,
num_classes=1023, #
target_means=[0., 0., 0., 0.],
target_stds=[0.05, 0.05, 0.1, 0.1],
reg_class_agnostic=True),
dict(
type='SharedFCBBoxHead',
num_fcs=2,
in_channels=256,
fc_out_channels=1024,
roi_feat_size=7,
num_classes=1023,
target_means=[0., 0., 0., 0.],
target_stds=[0.033, 0.033, 0.067, 0.067],
reg_class_agnostic=True)
],
mask_roi_extractor=dict(
type='SingleRoIExtractor',
roi_layer=dict(type='RoIAlign', out_size=14, sample_num=2),
out_channels=256,
featmap_strides=[4, 8, 16, 32]),
mask_head=dict(
type='FCNMaskHead',
num_convs=4,
in_channels=256,
conv_out_channels=256,
num_classes=1023))
# model training and testing settings
train_cfg = dict(
rpn=dict(
assigner=dict(
pos_iou_thr=0.7,
neg_iou_thr=0.3,
min_pos_iou=0.3,
ignore_iof_thr=-1),
sampler=dict(
num=256,
pos_fraction=0.5,
neg_pos_ub=-1,
add_gt_as_proposals=False,
pos_balance_sampling=False,
neg_balance_thr=0),
allowed_border=0,
pos_weight=-1,
smoothl1_beta=1 / 9.0,
debug=False),
rcnn=[
dict(
assigner=dict(
pos_iou_thr=0.5,
neg_iou_thr=0.5,
min_pos_iou=0.5,
ignore_iof_thr=-1),
sampler=dict(
num=512,
pos_fraction=0.25,
neg_pos_ub=-1,
add_gt_as_proposals=True,
pos_balance_sampling=False,
neg_balance_thr=0),
mask_size=28,
pos_weight=-1,
debug=False),
dict(
assigner=dict(
pos_iou_thr=0.6,
neg_iou_thr=0.6,
min_pos_iou=0.6,
ignore_iof_thr=-1),
sampler=dict(
num=512,
pos_fraction=0.25,
neg_pos_ub=-1,
add_gt_as_proposals=True,
pos_balance_sampling=False,
neg_balance_thr=0),
mask_size=28,
pos_weight=-1,
debug=False),
dict(
assigner=dict(
pos_iou_thr=0.7,
neg_iou_thr=0.7,
min_pos_iou=0.7,
ignore_iof_thr=-1),
sampler=dict(
num=512,
pos_fraction=0.25,
neg_pos_ub=-1,
add_gt_as_proposals=True,
pos_balance_sampling=False,
neg_balance_thr=0),
mask_size=28,
pos_weight=-1,
debug=False)
],
stage_loss_weights=[1, 0.5, 0.25])
test_cfg = dict(
rpn=dict(
nms_across_levels=False,
nms_pre=2000,
nms_post=2000,
max_num=2000,
nms_thr=0.7,
min_bbox_size=0),
rcnn=dict(
score_thr=0.05,
nms=dict(type='nms', iou_thr=0.5),
max_per_img=100,
mask_thr_binary=0.5),
keep_all_stages=False)
# dataset settings
dataset_type = 'MyDataset'# 'CustomDataset'
data_root = '/home/ubuntu/project/ConvertToCOCOData/'
img_norm_cfg = dict(
mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
data = dict(
imgs_per_gpu=2,
workers_per_gpu=2,
train=dict(
type=dataset_type,
ann_file=data_root + 'data/OutCOCOData/test.json',
img_prefix='/home/ubuntu/project/ConvertToCOCOData/',
img_scale=(1280, 720),
img_norm_cfg=img_norm_cfg,
size_divisor=32,
flip_ratio=0.5,
with_mask=True,
with_crowd=True,
with_label=True),
val=dict(
type=dataset_type,
ann_file=data_root + 'data/OutCOCOData/test.json',
img_prefix='/home/ubuntu/project/ConvertToCOCOData/',
img_scale=(1280, 720),
img_norm_cfg=img_norm_cfg,
size_divisor=32,
flip_ratio=0,
with_mask=True,
with_crowd=True,
with_label=True),
test=dict(
type=dataset_type,
ann_file=data_root + 'data/OutCOCOData/test.json',
img_prefix='/home/ubuntu/project/ConvertToCOCOData/',
img_scale=(1280, 720),
img_norm_cfg=img_norm_cfg,
size_divisor=32,
flip_ratio=0,
with_mask=True,
with_label=False,
test_mode=True))
# optimizer
optimizer = dict(type='SGD', lr=0.0025, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
# learning policy
lr_config = dict(
policy='step',
warmup='linear',
warmup_iters=500,
warmup_ratio=1.0 / 3,
step=[8, 11])
checkpoint_config = dict(interval=1)
# yapf:disable
log_config = dict(
interval=1,
hooks=[
dict(type='TextLoggerHook'),
# dict(type='TensorboardLoggerHook')
])
# yapf:enable
# runtime settings
total_epochs = 5
dist_params = dict(backend='nccl')
log_level = 'INFO'
work_dir = './work_dirs/cascade_mask_rcnn_r50_fpn_1x'
load_from = None
resume_from = None
workflow = [('train', 1)]
@hellock hi, could you please take a look about this problem😭 thank you
I guess that the model predict very low scores so you didn't get any output. The loss is still high according to your training log. You may first try training for more iterations and see if the loss can decrease to very small.
I guess that the model predict very low scores so you didn't get any output. The loss is still high according to your training log. You may first try training for more iterations and see if the loss can decrease to very small.
OK I try to train more iter, but I just change the score_thr
to 0.003
even 0
. but still output nothing and I try to train a faster-rcnn_r50
get same result to mask-rcnn.
show_mask_result(img,
result,
dataset='mydata',
score_thr=0.3):
BTW, the bboxes
in customDataset
is np <n, minX, minY, maxX, maxY>
dtype is np.float32
, masks
is ndarray <n, h, w>
, dtype is np.uint8
, labels
is np <n>
dtype is np.int64
, right?
The annotation format looks good. I suggest that you print more info during the model forwarding, such as the rpn bboxes, raw classification scores of bboxes, etc.
The annotation format looks good. I suggest that you print more info during the model forwarding, such as the rpn bboxes, raw classification scores of bboxes, etc.
For the training, you can refer to L106, and for testing, you can refer to L188.
hi, I try to print tensor when testing(without mask, only print bbox's), the code:
def simple_test(self, img, img_meta, proposals=None, rescale=False):
x = self.extract_feat(img)
for i in range(len(x)):
x_np = torch.Tensor.cpu(x[i]).numpy()
print('feature %d shape : '%(i), x_np.shape)
# print(x0_np[0][0].shape)
print('type of proposals: ', type(proposals))
proposal_list = self.simple_test_rpn(
x, img_meta, self.test_cfg.rpn) if proposals is None else proposals
print('type of proposal_list: ', type(proposal_list), 'len of prop list: %d'%(len(proposal_list)))
prop_np = torch.Tensor.cpu(proposal_list[0]).numpy()
print('proposal list shape : ', prop_np.shape)
print('proposal list first 10 : ')
for i in range(10):
print('%.2f, %.2f, %.2f, %.2f, %.2f'%(prop_np[i][0], prop_np[i][1], prop_np[i][2], prop_np[i][3], prop_np[i][4]))
img_shape = img_meta[0]['img_shape']
ori_shape = img_meta[0]['ori_shape']
scale_factor = img_meta[0]['scale_factor']
# "ms" in variable names means multi-stage
ms_bbox_result = {}
ms_segm_result = {}
ms_scores = []
rcnn_test_cfg = self.test_cfg.rcnn
rois = bbox2roi(proposal_list)
rois_np = torch.Tensor.cpu(rois).numpy()
print('rois list shape : ', prop_np.shape)
print('roi first 10: ')
for i in range(10):
print('%.2f, %.2f, %.2f, %.2f, %.2f'%(rois_np[i][0], rois_np[i][1], rois_np[i][2], rois_np[i][3], rois_np[i][4]))
for i in range(self.num_stages):
bbox_roi_extractor = self.bbox_roi_extractor[i]
print(type(bbox_roi_extractor))
bbox_head = self.bbox_head[i]
print(type(bbox_head))
bbox_feats = bbox_roi_extractor(x[:len(bbox_roi_extractor.featmap_strides)], rois)
print(type(bbox_feats), bbox_feats.shape)
cls_score, bbox_pred = bbox_head(bbox_feats)
print('cls score shape : ', cls_score.shape, 'bbox pred shape: ', bbox_pred.shape)
cls_score_np = torch.Tensor.cpu(cls_score).numpy()
bbox_pred_np = torch.Tensor.cpu(bbox_pred).numpy()
print('first score argmax : ', np.argmax(cls_score_np[0]), 'first bbox pred: ', bbox_pred_np[0])
# print(cls_score_np[0])
ms_scores.append(cls_score)
if self.test_cfg.keep_all_stages:
det_bboxes, det_labels = bbox_head.get_det_bboxes(
rois,
cls_score,
bbox_pred,
img_shape,
scale_factor,
rescale=rescale,
cfg=rcnn_test_cfg)
print(det_bboxes.shape, det_labels.shape)
bbox_result = bbox2result(det_bboxes, det_labels,
bbox_head.num_classes)
ms_bbox_result['stage{}'.format(i)] = bbox_result
if self.with_mask:
mask_roi_extractor = self.mask_roi_extractor[i]
mask_head = self.mask_head[i]
if det_bboxes.shape[0] == 0:
segm_result = [
[] for _ in range(mask_head.num_classes - 1)
]
else:
_bboxes = (det_bboxes[:, :4] * scale_factor
if rescale else det_bboxes)
mask_rois = bbox2roi([_bboxes])
mask_feats = mask_roi_extractor(
x[:len(mask_roi_extractor.featmap_strides)],
mask_rois)
mask_pred = mask_head(mask_feats)
segm_result = mask_head.get_seg_masks(
mask_pred, _bboxes, det_labels, rcnn_test_cfg,
ori_shape, scale_factor, rescale)
ms_segm_result['stage{}'.format(i)] = segm_result
if i < self.num_stages - 1:
bbox_label = cls_score.argmax(dim=1)
rois = bbox_head.regress_by_class(rois, bbox_label, bbox_pred,
img_meta[0])
# print(bbox_label.shape, rois.shape)
print('roi prop over')
cls_score = sum(ms_scores) / self.num_stages
print('cls_score shape: ', cls_score.shape)
det_bboxes, det_labels = self.bbox_head[-1].get_det_bboxes(
rois,
cls_score,
bbox_pred,
img_shape,
scale_factor,
rescale=rescale,
cfg=rcnn_test_cfg)
bbox_result = bbox2result(det_bboxes, det_labels,
self.bbox_head[-1].num_classes)
print('bbox result len: ', len(bbox_result))
cnt = 0
for j in bbox_result:
if j.shape[0] > 0:
cnt += 1
print('bbox not empty len: ', cnt )
print('bbox result first 10:')
for tbox in bbox_result[:10]:
print(tbox.shape, tbox)
# print() here all bbox []
ms_bbox_result['ensemble'] = bbox_result
and here is the result (my model):
feature 0 shape : (1, 256, 184, 320)
feature 1 shape : (1, 256, 92, 160)
feature 2 shape : (1, 256, 46, 80)
feature 3 shape : (1, 256, 23, 40)
feature 4 shape : (1, 256, 12, 20)
type of proposals: <class 'NoneType'>
type of proposal_list: <class 'list'> len of prop list: 1
proposal list shape : (2000, 5)
proposal list first 10 :
1043.42, 324.31, 1227.31, 617.22, 0.54
1003.49, 316.51, 1189.31, 609.10, 0.53
829.29, 130.20, 1230.14, 685.36, 0.53
927.52, 245.51, 1134.38, 524.43, 0.53
993.44, 274.77, 1192.89, 553.80, 0.53
167.99, 202.93, 361.99, 419.62, 0.53
11.98, 237.19, 475.66, 719.00, 0.53
374.18, 95.05, 854.00, 625.56, 0.52
126.01, 183.36, 333.22, 428.06, 0.52
928.76, 295.10, 1113.42, 582.58, 0.52
rois list shape : (2000, 5)
roi first 10:
0.00, 1043.42, 324.31, 1227.31, 617.22
0.00, 1003.49, 316.51, 1189.31, 609.10
0.00, 829.29, 130.20, 1230.14, 685.36
0.00, 927.52, 245.51, 1134.38, 524.43
0.00, 993.44, 274.77, 1192.89, 553.80
0.00, 167.99, 202.93, 361.99, 419.62
0.00, 11.98, 237.19, 475.66, 719.00
0.00, 374.18, 95.05, 854.00, 625.56
0.00, 126.01, 183.36, 333.22, 428.06
0.00, 928.76, 295.10, 1113.42, 582.58
<class 'mmdet.models.roi_extractors.single_level.SingleRoIExtractor'>
<class 'mmdet.models.bbox_heads.convfc_bbox_head.SharedFCBBoxHead'>
<class 'torch.Tensor'> torch.Size([2000, 256, 7, 7])
cls score shape : torch.Size([2000, 1023]) bbox pred shape: torch.Size([2000, 4])
first score argmax : 0 first bbox pred: [ 0.05841054 0.05216831 -0.40474734 -0.5283198 ]
<class 'mmdet.models.roi_extractors.single_level.SingleRoIExtractor'>
<class 'mmdet.models.bbox_heads.convfc_bbox_head.SharedFCBBoxHead'>
<class 'torch.Tensor'> torch.Size([2000, 256, 7, 7])
cls score shape : torch.Size([2000, 1023]) bbox pred shape: torch.Size([2000, 4])
first score argmax : 0 first bbox pred: [-0.09872378 0.24430281 0.02129897 -0.39382043]
<class 'mmdet.models.roi_extractors.single_level.SingleRoIExtractor'>
<class 'mmdet.models.bbox_heads.convfc_bbox_head.SharedFCBBoxHead'>
<class 'torch.Tensor'> torch.Size([2000, 256, 7, 7])
cls score shape : torch.Size([2000, 1023]) bbox pred shape: torch.Size([2000, 4])
first score argmax : 0 first bbox pred: [-0.08043834 0.03939664 0.05188651 -0.08744178]
roi prop over
cls_score shape: torch.Size([2000, 1023])
bbox result len: 1022
bbox not empty len: 0
bbox result first 10:
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
I also try the pre-trained model, here is the output:
feature 0 shape : (1, 256, 192, 336)
feature 1 shape : (1, 256, 96, 168)
feature 2 shape : (1, 256, 48, 84)
feature 3 shape : (1, 256, 24, 42)
feature 4 shape : (1, 256, 12, 21)
type of proposals: <class 'NoneType'>
type of proposal_list: <class 'list'> len of prop list: 1
proposal list shape : (2000, 5)
proposal list first 10 :
113.35, 153.32, 820.05, 749.00, 1.00
193.74, 689.14, 1332.00, 749.00, 1.00
0.00, 0.00, 498.83, 598.44, 1.00
259.92, 0.00, 1046.26, 591.30, 1.00
0.00, 48.25, 668.69, 749.00, 1.00
0.00, 0.00, 333.35, 605.46, 1.00
348.27, 145.80, 1195.93, 749.00, 1.00
0.00, 107.74, 425.09, 731.72, 1.00
450.58, 651.62, 1332.00, 749.00, 1.00
0.00, 0.00, 423.40, 489.42, 1.00
rois list shape : (2000, 5)
roi first 10:
0.00, 113.35, 153.32, 820.05, 749.00
0.00, 193.74, 689.14, 1332.00, 749.00
0.00, 0.00, 0.00, 498.83, 598.44
0.00, 259.92, 0.00, 1046.26, 591.30
0.00, 0.00, 48.25, 668.69, 749.00
0.00, 0.00, 0.00, 333.35, 605.46
0.00, 348.27, 145.80, 1195.93, 749.00
0.00, 0.00, 107.74, 425.09, 731.72
0.00, 450.58, 651.62, 1332.00, 749.00
0.00, 0.00, 0.00, 423.40, 489.42
<class 'mmdet.models.roi_extractors.single_level.SingleRoIExtractor'>
<class 'mmdet.models.bbox_heads.convfc_bbox_head.SharedFCBBoxHead'>
<class 'torch.Tensor'> torch.Size([2000, 256, 7, 7])
cls score shape : torch.Size([2000, 81]) bbox pred shape: torch.Size([2000, 4])
first score argmax : 61 first bbox pred: [ 1.1075324 -0.10280555 0.5707533 -0.5377309 ]
<class 'mmdet.models.roi_extractors.single_level.SingleRoIExtractor'>
<class 'mmdet.models.bbox_heads.convfc_bbox_head.SharedFCBBoxHead'>
<class 'torch.Tensor'> torch.Size([2000, 256, 7, 7])
cls score shape : torch.Size([2000, 81]) bbox pred shape: torch.Size([2000, 4])
first score argmax : 61 first bbox pred: [ 0.90631825 0.32633534 1.0231922 -0.44916588]
<class 'mmdet.models.roi_extractors.single_level.SingleRoIExtractor'>
<class 'mmdet.models.bbox_heads.convfc_bbox_head.SharedFCBBoxHead'>
<class 'torch.Tensor'> torch.Size([2000, 256, 7, 7])
cls score shape : torch.Size([2000, 81]) bbox pred shape: torch.Size([2000, 4])
first score argmax : 0 first bbox pred: [ 0.12026529 0.2829571 -0.5314679 -0.16688687]
roi prop over
cls_score shape: torch.Size([2000, 81])
bbox result len: 80
bbox not empty len: 17
bbox result first 10:
(1, 5) [[2.0345166e+02 4.8463275e+02 2.2859534e+02 5.3144836e+02 1.0939882e-01]]
(0, 5) []
(1, 5) [[0.0000000e+00 3.2478943e+02 1.1104912e+02 5.5315955e+02 2.0679985e-01]]
(0, 5) []
(0, 5) []
(0, 5) []
(0, 5) []
(1, 5) [[0.0000000e+00 2.0237658e+01 2.6698157e+02 6.6101172e+02 5.7585519e-02]]
(0, 5) []
(1, 5) [[1.00851160e+03 4.60482910e+02 1.01906506e+03 4.74585602e+02
5.50066531e-02]]
so, cmp the two model, pre-trained proposal list have many high confidence(even 1.00), but mine have less than the pre-train, and highest confidence only 0.54, and the bbox result list
which from the last bbox head, every class bbox output len is 0
from my model, but pre-trained model work fine.
@hellock hi, I print something like pred_label
, roi_label
etc, and show bbox use mmcv:
1.I show first ten bbox on the refine bboxes code block:
# refine bboxes
if i < self.num_stages - 1:
pos_is_gts = [res.pos_is_gt for res in sampling_results]
roi_labels = bbox_targets[0] # bbox_targets is a tuple
print('roi labels 10: ', roi_labels[:10], 'gt_label 10: ',t_labels.view(1, -1).expand_as(pred_label)[:10])
with torch.no_grad():
proposal_list = bbox_head.refine_bboxes(
rois, roi_labels, bbox_pred, pos_is_gts, img_meta)
print('type of proposal_list: ', type(proposal_list), 'len of prop list: %d'%(len(proposal_list)))
print(proposal_list[0].shape, proposal_list[1].shape)
print(proposal_list[0][0], proposal_list[1][0])
mm_imgs = tensor2imgs(img)
np_bbox_list0 = torch.Tensor.cpu(proposal_list[0][:10]).numpy()
print('img type: ' ,type(mm_imgs) ,type(mm_imgs[0]), mm_imgs[0].shape)
np_bbox_list1 = torch.Tensor.cpu(proposal_list[1][:10]).numpy()
# mmcv.imshow_bboxes(mm_imgs[0], np_bbox_list0, wait_time=10)
mmcv.imshow_det_bboxes(mm_imgs[0], np_bbox_list0, np.zeros(np_bbox_list0.shape[0], np.int32))
mmcv.imshow_det_bboxes(mm_imgs[1], np_bbox_list1, np.zeros(np_bbox_list1.shape[0], np.int32))
and the bbox is moving on to the gt box location , the picture is 15
epoch(only 2 imgs datasets)
I print other info on screen, here is the modify code(only nodify bbox, whitout mask):
def forward_train(self,
img,
img_meta,
gt_bboxes,
gt_bboxes_ignore,
gt_labels,
gt_masks=None,
proposals=None):
x = self.extract_feat(img)
losses = dict()
# torch.Tensor.cpu(x[i]).numpy()
if self.with_rpn:
rpn_outs = self.rpn_head(x)
print('rpn outs type: ', type(rpn_outs), ' len : ', len(rpn_outs))
a = rpn_outs[0]
print('rpn outs one type: ', type(a), ' len : ', len(a))
print('gt boxes : ')
print(gt_bboxes)
rpn_loss_inputs = rpn_outs + (gt_bboxes, img_meta, self.train_cfg.rpn)
rpn_losses = self.rpn_head.loss(*rpn_loss_inputs)
losses.update(rpn_losses)
proposal_inputs = rpn_outs + (img_meta, self.test_cfg.rpn)
proposal_list = self.rpn_head.get_proposals(*proposal_inputs)
else:
proposal_list = proposals
for i in range(self.num_stages):
rcnn_train_cfg = self.train_cfg.rcnn[i]
lw = self.train_cfg.stage_loss_weights[i]
# assign gts and sample proposals
assign_results, sampling_results = multi_apply(
assign_and_sample,
proposal_list,
gt_bboxes,
gt_bboxes_ignore,
gt_labels,
cfg=rcnn_train_cfg)
# bbox head forward and loss
bbox_roi_extractor = self.bbox_roi_extractor[i]
bbox_head = self.bbox_head[i]
rois = bbox2roi([res.bboxes for res in sampling_results])
bbox_feats = bbox_roi_extractor(x[:bbox_roi_extractor.num_inputs],
rois)
cls_score, bbox_pred = bbox_head(bbox_feats)
print('cls score shape : ', cls_score.shape, 'bbox pred shape : ', bbox_pred.shape)
bbox_targets = bbox_head.get_target(sampling_results, gt_bboxes,
gt_labels, rcnn_train_cfg)
# print(len(bbox_targets))
[t_labels, t_label_w, t_box_tar, t_box_w] = bbox_targets
# print(t_labels[:10])
print('*'*20)
print('~~~~~~~~now~num~stages~%d~~~~~~~~~~~'%(i))
topk = (1, )
maxk = max(topk)
_, pred_label = cls_score.topk(maxk, 1, True, True)
pred_label = pred_label.t()
print('pred_label: ', pred_label)
print('gt_label: ', t_labels.view(1, -1).expand_as(pred_label))
print('match: ', pred_label.eq(t_labels.view(1, -1).expand_as(pred_label)))
# print('*'*20)
print('cls acc: ', accuracy(cls_score, t_labels))
loss_bbox = bbox_head.loss(cls_score, bbox_pred, *bbox_targets)
for name, value in loss_bbox.items():
losses['s{}.{}'.format(i, name)] = (value * lw if
'loss' in name else value)
# mask head forward and loss
if self.with_mask:
mask_roi_extractor = self.mask_roi_extractor[i]
mask_head = self.mask_head[i]
pos_rois = bbox2roi(
[res.pos_bboxes for res in sampling_results])
mask_feats = mask_roi_extractor(
x[:mask_roi_extractor.num_inputs], pos_rois)
mask_pred = mask_head(mask_feats)
mask_targets = mask_head.get_target(sampling_results, gt_masks,
rcnn_train_cfg)
pos_labels = torch.cat(
[res.pos_gt_labels for res in sampling_results])
loss_mask = mask_head.loss(mask_pred, mask_targets, pos_labels)
for name, value in loss_mask.items():
losses['s{}.{}'.format(i, name)] = (value * lw
if 'loss' in name else
value)
# refine bboxes
if i < self.num_stages - 1:
pos_is_gts = [res.pos_is_gt for res in sampling_results]
roi_labels = bbox_targets[0] # bbox_targets is a tuple
print('roi labels 10: ', roi_labels[:10], 'gt_label 10: ',t_labels.view(1, -1).expand_as(pred_label)[:10])
with torch.no_grad():
proposal_list = bbox_head.refine_bboxes(
rois, roi_labels, bbox_pred, pos_is_gts, img_meta)
print('type of proposal_list: ', type(proposal_list), 'len of prop list: %d'%(len(proposal_list)))
print(proposal_list[0].shape, proposal_list[1].shape)
print(proposal_list[0][0], proposal_list[1][0])
mm_imgs = tensor2imgs(img)
np_bbox_list0 = torch.Tensor.cpu(proposal_list[0][:10]).numpy()
print('img type: ' ,type(mm_imgs) ,type(mm_imgs[0]), mm_imgs[0].shape)
np_bbox_list1 = torch.Tensor.cpu(proposal_list[1][:10]).numpy()
# mmcv.imshow_bboxes(mm_imgs[0], np_bbox_list0, wait_time=10)
mmcv.imshow_det_bboxes(mm_imgs[0], np_bbox_list0, np.zeros(np_bbox_list0.shape[0], np.int32))
mmcv.imshow_det_bboxes(mm_imgs[1], np_bbox_list1, np.zeros(np_bbox_list1.shape[0], np.int32))
ttt = input('continue? y/n: ')
is_continue = ttt != 'n'
if not is_continue:
exit()
return losses
and the info is here:
2018-12-13 15:36:59,746 - INFO - Epoch [14][1/1] lr: 0.00001, time: 10.306, data_time: 0.166, loss_rpn_cls: 0.4417, loss_rpn_reg: 0.0083, s0.loss_cls: 0.3783, s0.acc: 98.7305, s0.loss_reg: 0.0224, s0.loss_mask: 0.8002, s1.loss_cls: 0.0554, s1.acc: 99.4141, s1.loss_reg: 0.0027, s1.loss_mask: 0.4431, s2.loss_cls: 0.0183, s2.acc: 99.5117, s2.loss_reg: 0.0012, s2.loss_mask: 0.2118, loss: 2.3834
/home/ubuntu/project/ConvertToCOCOData/data/miniData/100class_mini/labelImgs/90.png
/home/ubuntu/project/ConvertToCOCOData/data/miniData/100class_mini/labelImgs/75.png
rpn outs type: <class 'tuple'> len : 2
rpn outs one type: <class 'list'> len : 5
gt boxes :
[tensor([[520., 169., 718., 379.],
[271., 318., 547., 549.]], device='cuda:0'), tensor([[541., 415., 746., 644.],
[493., 204., 674., 407.]], device='cuda:0')]
cls score shape : torch.Size([1024, 1023]) bbox pred shape : torch.Size([1024, 4])
********************
~~~~~~~~now~num~stages~0~~~~~~~~~~~
pred_label: tensor([[0, 0, 0, ..., 0, 0, 0]], device='cuda:0')
gt_label: tensor([[27, 67, 27, ..., 0, 0, 0]], device='cuda:0')
match: tensor([[0, 0, 0, ..., 1, 1, 1]], device='cuda:0', dtype=torch.uint8)
cls acc: tensor([98.9258], device='cuda:0')
roi labels 10: tensor([27, 67, 27, 27, 67, 67, 27, 0, 0, 0], device='cuda:0') gt_label 10: tensor([[27, 67, 27, ..., 0, 0, 0]], device='cuda:0')
type of proposal_list: <class 'list'> len of prop list: 2
torch.Size([510, 4]) torch.Size([510, 4])
tensor([524.8694, 161.3938, 733.2856, 403.1859], device='cuda:0') tensor([447.9930, 187.0480, 640.4958, 453.7739], device='cuda:0')
img type: <class 'list'> <class 'numpy.ndarray'> (736, 1280, 3)
cls score shape : torch.Size([1024, 1023]) bbox pred shape : torch.Size([1024, 4])
********************
~~~~~~~~now~num~stages~1~~~~~~~~~~~
pred_label: tensor([[0, 0, 0, ..., 0, 0, 0]], device='cuda:0')
gt_label: tensor([[27, 67, 27, ..., 0, 0, 0]], device='cuda:0')
match: tensor([[0, 0, 0, ..., 1, 1, 1]], device='cuda:0', dtype=torch.uint8)
cls acc: tensor([99.3164], device='cuda:0')
roi labels 10: tensor([27, 67, 27, 27, 0, 0, 0, 0, 0, 0], device='cuda:0') gt_label 10: tensor([[27, 67, 27, ..., 0, 0, 0]], device='cuda:0')
type of proposal_list: <class 'list'> len of prop list: 2
torch.Size([510, 4]) torch.Size([510, 4])
tensor([525.1940, 162.9855, 733.6262, 400.7294], device='cuda:0') tensor([570.6740, 394.9776, 789.5671, 660.1130], device='cuda:0')
img type: <class 'list'> <class 'numpy.ndarray'> (736, 1280, 3)
cls score shape : torch.Size([1024, 1023]) bbox pred shape : torch.Size([1024, 4])
********************
~~~~~~~~now~num~stages~2~~~~~~~~~~~
pred_label: tensor([[0, 0, 0, ..., 0, 0, 0]], device='cuda:0')
gt_label: tensor([[27, 67, 27, ..., 0, 0, 0]], device='cuda:0')
match: tensor([[0, 0, 0, ..., 1, 1, 1]], device='cuda:0', dtype=torch.uint8)
cls acc: tensor([99.5117], device='cuda:0')
continue? y/n:
2018-12-13 15:37:11,261 - INFO - Epoch [15][1/1] lr: 0.00001, time: 10.096, data_time: 0.162, loss_rpn_cls: 0.4434, loss_rpn_reg: 0.0110, s0.loss_cls: 0.3198, s0.acc: 98.9258, s0.loss_reg: 0.0145, s0.loss_mask: 0.8134, s1.loss_cls: 0.0631, s1.acc: 99.3164, s1.loss_reg: 0.0052, s1.loss_mask: 0.4241, s2.loss_cls: 0.0194, s2.acc: 99.5117, s2.loss_reg: 0.0008, s2.loss_mask: 0.2056, loss: 2.3200
/home/ubuntu/project/ConvertToCOCOData/data/miniData/100class_mini/labelImgs/90.png
/home/ubuntu/project/ConvertToCOCOData/data/miniData/100class_mini/labelImgs/75.png
rpn outs type: <class 'tuple'> len : 2
rpn outs one type: <class 'list'> len : 5
gt boxes :
[tensor([[ 561., 169., 759., 379.],
[ 732., 318., 1008., 549.]], device='cuda:0'), tensor([[541., 415., 746., 644.],
[493., 204., 674., 407.]], device='cuda:0')]
cls score shape : torch.Size([1024, 1023]) bbox pred shape : torch.Size([1024, 4])
********************
~~~~~~~~now~num~stages~0~~~~~~~~~~~
pred_label: tensor([[0, 0, 0, ..., 0, 0, 0]], device='cuda:0')
gt_label: tensor([[27, 67, 67, ..., 0, 0, 0]], device='cuda:0')
match: tensor([[0, 0, 0, ..., 1, 1, 1]], device='cuda:0', dtype=torch.uint8)
cls acc: tensor([98.9258], device='cuda:0')
roi labels 10: tensor([27, 67, 67, 67, 27, 27, 67, 0, 0, 0], device='cuda:0') gt_label 10: tensor([[27, 67, 67, ..., 0, 0, 0]], device='cuda:0')
type of proposal_list: <class 'list'> len of prop list: 2
torch.Size([510, 4]) torch.Size([510, 4])
tensor([713.9727, 312.0352, 900.2916, 531.5995], device='cuda:0') tensor([448.2678, 187.3840, 640.6703, 454.0936], device='cuda:0')
img type: <class 'list'> <class 'numpy.ndarray'> (736, 1280, 3)
cls score shape : torch.Size([1024, 1023]) bbox pred shape : torch.Size([1024, 4])
********************
~~~~~~~~now~num~stages~1~~~~~~~~~~~
pred_label: tensor([[0, 0, 0, ..., 0, 0, 0]], device='cuda:0')
gt_label: tensor([[27, 67, 0, ..., 0, 0, 0]], device='cuda:0')
match: tensor([[0, 0, 1, ..., 1, 1, 1]], device='cuda:0', dtype=torch.uint8)
cls acc: tensor([99.5117], device='cuda:0')
roi labels 10: tensor([27, 67, 0, 0, 0, 0, 0, 0, 0, 0], device='cuda:0') gt_label 10: tensor([[27, 67, 0, ..., 0, 0, 0]], device='cuda:0')
type of proposal_list: <class 'list'> len of prop list: 2
torch.Size([510, 4]) torch.Size([510, 4])
tensor([715.3549, 313.3972, 901.6011, 529.7083], device='cuda:0') tensor([570.5542, 395.3810, 789.4364, 660.3109], device='cuda:0')
img type: <class 'list'> <class 'numpy.ndarray'> (736, 1280, 3)
cls score shape : torch.Size([1024, 1023]) bbox pred shape : torch.Size([1024, 4])
********************
~~~~~~~~now~num~stages~2~~~~~~~~~~~
pred_label: tensor([[0, 0, 0, ..., 0, 0, 0]], device='cuda:0')
gt_label: tensor([[27, 67, 0, ..., 0, 0, 0]], device='cuda:0')
match: tensor([[0, 0, 1, ..., 1, 1, 1]], device='cuda:0', dtype=torch.uint8)
cls acc: tensor([99.6094], device='cuda:0')
continue? y/n: n
pred_label
from random tensor to a all zero tensor, but the gt_label is [label0, label1, ..., 0, 0], and I copy some code from losses.accuracy
:
topk = (1, )
maxk = max(topk)
_, pred_label = cls_score.topk(maxk, 1, True, True)
pred_label = pred_label.t()
print('pred_label: ', pred_label)
print('gt_label: ', t_labels.view(1, -1).expand_as(pred_label))
print('match: ', pred_label.eq(t_labels.view(1, -1).expand_as(pred_label)))
and here is the output:
pred_label: tensor([[0, 0, 0, ..., 0, 0, 0]], device='cuda:0')
gt_label: tensor([[27, 67, 0, ..., 0, 0, 0]], device='cuda:0')
match: tensor([[0, 0, 1, ..., 1, 1, 1]], device='cuda:0', dtype=torch.uint8)
cls acc: tensor([99.6094], device='cuda:0')
I get confused about that look like because of the most part of gt_label
value is 0
, so the pred_label
learn to change it self all to zero
so this two tensor can match much, but I also print the roi_label
in the refine bboxes code block, and them could match very well, and not all to zero like pred_label
:
roi labels 10: tensor([27, 67, 0, 0, 0, 0, 0, 0, 0, 0], device='cuda:0') gt_label 10: tensor([[27, 67, 0, ..., 0, 0, 0]], device='cuda:0')
BTW, here is the data json:
[
{
"filename": "data/miniData/100class_mini/imgs/90.jpg",
"width": 1280,
"height": 720,
"ann": {
"masks": "/home/ubuntu/project/ConvertToCOCOData/data/miniData/100class_mini/labelImgs/90.png",
"bboxes": [
[
520,
169,
718,
379
],
[
271,
318,
547,
549
]
],
"bboxes_ignore": [],
"labels_ignore": [],
"labels": [
27,
67
]
}
},
{
"filename": "data/miniData/100class_mini/imgs/75.jpg",
"width": 1280,
"height": 720,
"ann": {
"masks": "/home/ubuntu/project/ConvertToCOCOData/data/miniData/100class_mini/labelImgs/75.png",
"bboxes": [
[
541,
415,
746,
644
],
[
493,
204,
674,
407
]
],
"bboxes_ignore": [],
"labels_ignore": [],
"labels": [
3,
40
]
}
}
]
here is the proposal bbox top 10 visial when test, and it is very different output with last I post picture (betten train and test use same picture, same weight),
but I found yesterday My 100 epoch weight have better output:
0~20
epoch, and the 100 epoch loss :
2018-12-12 20:08:04,512 - INFO - Epoch [95][1/1] lr: 0.00001, time: 0.985, data_time: 0.153, loss_rpn_cls: 0.2005, loss_rpn_reg: 0.0057, s0.loss_cls: 0.4219, s0.acc: 96.8750, s0.loss_reg: 0.0632, s0.loss_mask: 0.6537, s1.loss_cls: 0.0894, s1.acc: 98.4375, s1.loss_reg: 0.0238, s1.loss_mask: 0.3512, s2.loss_cls: 0.0422, s2.acc: 99.4141, s2.loss_reg: 0.0025, s2.loss_mask: 0.1722, loss: 2.0263
2018-12-12 20:08:06,706 - INFO - Epoch [96][1/1] lr: 0.00001, time: 0.992, data_time: 0.152, loss_rpn_cls: 0.1917, loss_rpn_reg: 0.0050, s0.loss_cls: 0.3348, s0.acc: 97.4609, s0.loss_reg: 0.0492, s0.loss_mask: 0.6657, s1.loss_cls: 0.0881, s1.acc: 98.4375, s1.loss_reg: 0.0240, s1.loss_mask: 0.3541, s2.loss_cls: 0.0441, s2.acc: 99.3164, s2.loss_reg: 0.0030, s2.loss_mask: 0.1745, loss: 1.9343
2018-12-12 20:08:08,865 - INFO - Epoch [97][1/1] lr: 0.00001, time: 0.958, data_time: 0.154, loss_rpn_cls: 0.1973, loss_rpn_reg: 0.0051, s0.loss_cls: 0.3069, s0.acc: 97.6562, s0.loss_reg: 0.0436, s0.loss_mask: 0.6603, s1.loss_cls: 0.0777, s1.acc: 98.6328, s1.loss_reg: 0.0203, s1.loss_mask: 0.3509, s2.loss_cls: 0.0401, s2.acc: 99.3164, s2.loss_reg: 0.0030, s2.loss_mask: 0.1757, loss: 1.8809
2018-12-12 20:08:11,102 - INFO - Epoch [98][1/1] lr: 0.00001, time: 0.970, data_time: 0.151, loss_rpn_cls: 0.1883, loss_rpn_reg: 0.0051, s0.loss_cls: 0.3173, s0.acc: 97.5586, s0.loss_reg: 0.0463, s0.loss_mask: 0.6592, s1.loss_cls: 0.0780, s1.acc: 98.6328, s1.loss_reg: 0.0202, s1.loss_mask: 0.3507, s2.loss_cls: 0.0401, s2.acc: 99.3164, s2.loss_reg: 0.0030, s2.loss_mask: 0.1757, loss: 1.8840
2018-12-12 20:08:13,218 - INFO - Epoch [99][1/1] lr: 0.00001, time: 0.989, data_time: 0.154, loss_rpn_cls: 0.2072, loss_rpn_reg: 0.0051, s0.loss_cls: 0.3158, s0.acc: 97.5586, s0.loss_reg: 0.0462, s0.loss_mask: 0.6586, s1.loss_cls: 0.0798, s1.acc: 98.6328, s1.loss_reg: 0.0202, s1.loss_mask: 0.3506, s2.loss_cls: 0.0446, s2.acc: 99.3164, s2.loss_reg: 0.0030, s2.loss_mask: 0.1756, loss: 1.9068
2018-12-12 20:08:15,368 - INFO - Epoch [100][1/1] lr: 0.00001, time: 0.982, data_time: 0.154, loss_rpn_cls: 0.1913, loss_rpn_reg: 0.0049, s0.loss_cls: 0.3164, s0.acc: 97.5586, s0.loss_reg: 0.0452, s0.loss_mask: 0.6615, s1.loss_cls: 0.0925, s1.acc: 98.3398, s1.loss_reg: 0.0260, s1.loss_mask: 0.3539, s2.loss_cls: 0.0429, s2.acc: 99.2188, s2.loss_reg: 0.0045, s2.loss_mask: 0.1763, loss: 1.9154
should I change more small lr
and try to train more iter? now I use 1
GPU so I change lr
to 0.0025
:
optimizer = dict(type='SGD', lr=0.0025, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=dict(max_norm=35, norm_type=2))
# learning policy
lr_config = dict(
policy='step',
warmup='linear',
warmup_iters=500,
warmup_ratio=1.0 / 3,
step=[8, 11])
should it smaller than 0.0025
?
@hellock hi, I soved this problem, just use a bigger dataset, after train some epoch, get work. But I real confused, usually, a deep learning model should over-fitting after little epoch(on very little datasets), but why when datasets only have 2~10 samples
it does not learn(I change lr [0.00125, 0.0025], only 1 gpu),but when datasets have more than 100
samples, could have a good result.
10000
samples, and training only 1.x epoch, it hang on the second epoch, I found that others also encountered the same problem. My environment:cuda10.0 + nvidia driver 410.78 + pytorch0.4.1
I just overwrite CustomDatasets to eval my data, and for test, I try to use a very little datasets(only ten picture) to train base on
cascade_mask_rcnn_r50_fpn_1x.py
(change 81class to 1023 class, ),and the log isand I try to test the model by two ways:
python3 tools/test.py configs/my_cascade_mask_rcnn_r50.py work_dir/latest.pth --show
, Output Nothing, no bbox, no maskfrom mmdet.models import build_detector from mmdet.apis import inference_detector, show_result import numpy as np, pycocotools.mask as maskUtils, mmcv from mmdet.core import tensor2imgs, get_classes from glob import glob
config_path = 'configs/my_cascade_mask_rcnn_r50.py' model_path = 'work_dir/latest.pth'
config_path = 'configs/cascade_mask_rcnn_r50_fpn_1x.py' model_path = '/home/ubuntu/Downloads/cascade_mask_rcnn_r50_fpn_20e_20181123-6e0c9713.pth'
cfg = mmcv.Config.fromfile(config_path) cfg.model.pretrained = None
img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True)
def show_mask_result(img, result, dataset='mydata', score_thr=0.3): assert isinstance(result, tuple) bbox_result, segm_result = result
construct the model and load checkpoint
model = build_detector(cfg.model, test_cfg=cfg.testcfg) = load_checkpoint(model, model_path)
img_list = glob('/data/PycharmProjects/mask_rcnn/other_code/datasets/25class_v1_badcase/*.jpg')
img_list = glob('/home/ubuntu/project/ConvertToCOCOData/data/miniData/100class_mini/imgs/*.jpg')
for i, result in enumerate(inference_detector(model, img_list[:1], cfg, device='cuda:0')): print(i, img_list[i]) print(result[0]) show_mask_result(mmcv.imread(img_list[i]), result)
show_result(mmcv.imread(img_list[i]), result[0])
encoding:utf-8
import os.path as osp import mmcv, cv2, numpy as np
from .custom import CustomDataset
class MyDataset(CustomDataset): """Custom dataset for detection.