Detectron2的Learning Rate和Schedules
1x and 3x schedules
1x = 16 images / iterations * 90,000 iterations in total with the LR reduced at 60k and 80k
3x = 16 images / iteration * 270,000 iterations in total with the LR reduced at 210k and 250k
在60k和80k迭代衰减*0.1,在90k次迭代终止。
cfg.SOLVER.BASE_LR = 0.001
cfg.SOLVER.STEPS = (300, 400)
cfg.SOLVER.MAX_ITER = 500
设置最大迭代次数为500次,前面300次按照线性增长,增长到BASE_LR,然后在300,400分别下降
因此,如果改小BatchSize,应该对应的修改上述参数。
Detectron2中的Faster RCNN
分为两个版本,C4的backbone是ResNet的Conv_1到Conv_4,而FPN则是FPN,其本质上是MaskRCNN如下图所示:
C4的整体框架如下图:
C4的ROI_HEAD是接ResNet的Conv_5,然后通过AveragePooling得到2048维特征,送入class和box分支。
FPN的ROI_HEAD是直接Flatten然后通过两个全连接层来embedding为1024维,然后再接class和box分支。
对应到Detectron2中:
C4的ROI_HEADS在Res5ROIHeads,而FPN的ROI_HEADS则在box_head.py的FastRCNNConvFCHead。两者共用fast_rcnn.py中的FastRCNNOutputLayers进行最后的分类和bbox回归。
Detectron2中的冻结参数进行训练
冻结除roi_heads.box_preditor之外的所有参数进行训练。
from tools.train_net import Trainer
trainer = Trainer(cfg)
model = trainer.model
layer = 'roi_heads.box_predictor'
for name, value in model.named_parameters():
if layer not in name:
value.requires_grad = False
trainer.resume_or_load(resume = False)
trainer.train()
# 考虑将VC RCNN改为目标检测模型,尝试。
def Causal(x):
length = x.size(0)
dic_z = self.dic.cuda()
prior = self.prior.cuda()
embeded_prior = prior.unsqueeze(0).repeat(length, 1)
attention = torch.mm(x, dic_z.t()) / (1024 ** 0.5)
attention = F.softmax(attention, 0)
attention = torch.mm(attention, dic_z)
attention = self.attention(attention)
attention = F.softmax(attention, 0)
xz = attention * x
xz = self.attention_relu(xz)
return xz