参考:MMDetection issue #9479
查看 Backbone :
# 骨架网络构建
# from mmengine.registry import Registry
import torch
import numpy as np
from mmdet.registry import MODELSif __name__ == '__main__':backbone=dict(type='ResNet',depth=50,num_stages=4,out_indices=(0,1,2,3), # 返回四个特征图的索引frozen_stages=1, # 冻结stem和第一层# 表示全部的BN需要梯度更新norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, # 且全局网络的BN进入eval模式style='pytorch')backbone=MODELS.build(backbone)neck=MODELS.build(dict(type='GlobalAveragePooling',kernel_size=(8, 4), stride=1))head=MODELS.build(dict(type='LinearReIDHead',num_fcs=1,in_channels=2048, # 2048fc_channels=1024,out_channels=128,num_classes=380, ### 如何确定?? loss_cls=dict(type='mmpretrain.CrossEntropyLoss', loss_weight=1.0),loss_triplet=dict(type='TripletLoss', margin=0.3, loss_weight=1.0),norm_cfg=dict(type='BN1d'),act_cfg=dict(type='ReLU')))x = torch.randn(2,3,256,128)all_backbone_outputs = backbone(x)for out in all_backbone_outputs:print(out.shape)backbone_outputs = (all_backbone_outputs[-1],) # [2,2048,8,4]neck_outputs = neck(backbone_outputs) # [2,2048] 全部展开print(neck_outputs[0].shape)# 经过 Neck(GAP) 后输出的向量维度应与 head.in_channel 一致head_outputs = head.forward(neck_outputs)print(head_outputs.shape)