backbone_factory.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from collections import OrderedDict
  2. from libs.vision_libs import models
  3. from libs.vision_libs.models import mobilenet_v3_large, EfficientNet_V2_S_Weights, efficientnet_v2_s, \
  4. EfficientNet_V2_M_Weights, efficientnet_v2_m, EfficientNet_V2_L_Weights, efficientnet_v2_l, ConvNeXt_Base_Weights
  5. from libs.vision_libs.models._utils import _ovewrite_value_param, handle_legacy_interface
  6. from libs.vision_libs.models.detection import FasterRCNN
  7. from libs.vision_libs.models.detection.anchor_utils import AnchorGenerator
  8. from libs.vision_libs.models.detection.ssdlite import _mobilenet_extractor
  9. from libs.vision_libs.models.resnet import resnet50, ResNet50_Weights, resnet18
  10. from libs.vision_libs.models.detection.backbone_utils import _resnet_fpn_extractor, _validate_trainable_layers
  11. from libs.vision_libs.ops import misc as misc_nn_ops, MultiScaleRoIAlign
  12. from torch import nn
  13. import torch
  14. from libs.vision_libs.models.detection.backbone_utils import BackboneWithFPN
  15. def get_resnet50_fpn():
  16. is_trained = False
  17. trainable_backbone_layers = _validate_trainable_layers(is_trained, None, 5, 3)
  18. norm_layer = misc_nn_ops.FrozenBatchNorm2d if is_trained else nn.BatchNorm2d
  19. backbone = resnet50(weights=None, progress=True, norm_layer=norm_layer)
  20. backbone = _resnet_fpn_extractor(backbone, trainable_backbone_layers)
  21. return backbone
  22. def get_resnet18_fpn():
  23. is_trained = False
  24. trainable_backbone_layers = _validate_trainable_layers(is_trained, None, 5, 3)
  25. norm_layer = misc_nn_ops.FrozenBatchNorm2d if is_trained else nn.BatchNorm2d
  26. backbone = resnet18(weights=None, progress=True, norm_layer=norm_layer)
  27. backbone = _resnet_fpn_extractor(backbone, trainable_backbone_layers)
  28. return backbone
  29. def get_mobilenet_v3_large_fpn():
  30. is_trained = False
  31. trainable_backbone_layers = _validate_trainable_layers(is_trained, None, 6, 3)
  32. norm_layer = misc_nn_ops.FrozenBatchNorm2d if is_trained else nn.BatchNorm2d
  33. backbone = mobilenet_v3_large(weights=None, progress=True, norm_layer=norm_layer)
  34. backbone = _mobilenet_extractor(backbone, True, trainable_backbone_layers)
  35. return backbone
  36. def get_convnext_fpn():
  37. convnext = models.convnext_base(weights=ConvNeXt_Base_Weights.DEFAULT)
  38. # convnext = models.convnext_small(pretrained=True)
  39. # convnext = models.convnext_large(pretrained=True)
  40. in_channels_list = [128, 256, 512, 1024]
  41. backbone_with_fpn = BackboneWithFPN(
  42. convnext.features,
  43. return_layers={'1': '0', '3': '1', '5': '2', '7': '3'}, # 确保这些键对应到实际的层
  44. in_channels_list=in_channels_list,
  45. out_channels=256
  46. )
  47. return backbone_with_fpn
  48. def get_efficientnetv2_fpn(name='efficientnet_v2_m', pretrained=True):
  49. # 加载EfficientNetV2模型
  50. if name == 'efficientnet_v2_s':
  51. weights = EfficientNet_V2_S_Weights.IMAGENET1K_V1 if pretrained else None
  52. backbone = efficientnet_v2_s(weights=weights).features
  53. if name == 'efficientnet_v2_m':
  54. weights = EfficientNet_V2_M_Weights.IMAGENET1K_V1 if pretrained else None
  55. backbone = efficientnet_v2_m(weights=weights).features
  56. if name == 'efficientnet_v2_l':
  57. weights = EfficientNet_V2_L_Weights.IMAGENET1K_V1 if pretrained else None
  58. backbone = efficientnet_v2_l(weights=weights).features
  59. # 定义返回的层索引和名称
  60. return_layers = {"2": "0", "3": "1", "4": "2", "5": "3"}
  61. # 获取每个层输出通道数
  62. in_channels_list = []
  63. for layer_idx in [2, 3, 4, 5]:
  64. module = backbone[layer_idx]
  65. if hasattr(module, 'out_channels'):
  66. in_channels_list.append(module.out_channels)
  67. elif hasattr(module[-1], 'out_channels'):
  68. # 如果module本身没有out_channels,检查最后一个子模块
  69. in_channels_list.append(module[-1].out_channels)
  70. else:
  71. raise ValueError(f"Cannot determine out_channels for layer {layer_idx}")
  72. # 使用BackboneWithFPN包装backbone
  73. backbone_with_fpn = BackboneWithFPN(
  74. backbone=backbone,
  75. return_layers=return_layers,
  76. in_channels_list=in_channels_list,
  77. out_channels=256
  78. )
  79. return backbone_with_fpn
  80. # 加载 ConvNeXt 模型
  81. # convnext = models.convnext_base(pretrained=True)
  82. # convnext = models.convnext_tiny(pretrained=True)
  83. # convnext = models.convnext_small(pretrained=True)
  84. # print(convnext)
  85. # # 打印模型的所有命名层
  86. # for name, _ in convnext.features[5].named_children():
  87. # print(name)
  88. # 修改 ConvNeXt 以适应 Faster R-CNN
  89. # 修改 ConvNeXt 以适应 Faster R-CNN
  90. def get_anchor_generator(backbone, test_input):
  91. features = backbone(test_input) # 获取 backbone 输出的所有特征图
  92. featmap_names = list(features.keys())
  93. print(f'featmap_names:{featmap_names}')
  94. num_features = len(features) # 特征图数量
  95. print(f'num_features:{num_features}')
  96. # num_features=num_features-1
  97. # # 定义每层的 anchor 尺寸和比例
  98. # base_sizes = [32, 64, 128] # 支持最多 4 层
  99. # sizes = tuple((size,) for size in base_sizes[:num_features])
  100. anchor_sizes = tuple((int(16 * 2 ** i),) for i in range(num_features)) # 自动生成不同大小
  101. print(f'anchor_sizes:{anchor_sizes }')
  102. aspect_ratios = ((0.5, 1.0, 2.0),) * num_features
  103. print(f'aspect_ratios:{aspect_ratios}')
  104. return AnchorGenerator(sizes=anchor_sizes , aspect_ratios=aspect_ratios)
  105. class MaxVitBackbone(torch.nn.Module):
  106. def __init__(self):
  107. super(MaxVitBackbone, self).__init__()
  108. # 提取MaxVit的部分层作为特征提取器
  109. maxvit_model = models.maxvit_t(pretrained=True)
  110. self.stem = maxvit_model.stem # Stem层
  111. self.block0= maxvit_model.blocks[0]
  112. self.block1 = maxvit_model.blocks[1]
  113. self.block2 = maxvit_model.blocks[2]
  114. self.block3 = maxvit_model.blocks[3]
  115. def forward(self, x):
  116. # features = {}
  117. x = self.stem(x)
  118. x=self.block0(x)
  119. x = self.block1(x)
  120. x = self.block2(x)
  121. x = self.block3(x)
  122. return x
  123. if __name__ == '__main__':
  124. # maxvit = models.maxvit_t(pretrained=True)
  125. maxvit=MaxVitBackbone()
  126. # print(maxvit.named_children())
  127. for i,layer in enumerate(maxvit.named_children()):
  128. print(f'layer:{i}:{layer}')
  129. in_channels_list = [64,64,128, 256, 512]
  130. backbone_with_fpn = BackboneWithFPN(
  131. maxvit,
  132. return_layers={'stem': '0','block0':'1','block1':'2','block2':'3','block3':'4'}, # 确保这些键对应到实际的层
  133. in_channels_list=in_channels_list,
  134. out_channels=256
  135. )
  136. model = FasterRCNN(
  137. backbone=backbone_with_fpn,
  138. num_classes=91, # COCO 数据集有 91 类
  139. # rpn_anchor_generator=anchor_generator,
  140. # box_roi_pool=roi_pooler
  141. )
  142. test_input = torch.randn(1, 3, 896, 896)
  143. with torch.no_grad():
  144. output = backbone_with_fpn(test_input)
  145. print("Output feature maps:")
  146. for k, v in output.items():
  147. print(f"{k}: {v.shape}")
  148. model.eval()
  149. output=model(test_input)