def build_fcos_resnet_fpn_backbone(cfg, input_shape: ShapeSpec): """ Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS top_levels = cfg.MODEL.FCOS.TOP_LEVELS in_channels_top = out_channels if top_levels == 2: top_block = LastLevelP6P7(in_channels_top, out_channels, "p5") if top_levels == 1: top_block = LastLevelP6(in_channels_top, out_channels, "p5") elif top_levels == 0: top_block = None backbone = FPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=top_block, fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone
def build_resnest_fpn_backbone(cfg, input_shape: ShapeSpec): if cfg.MODEL.RESNEST: bottom_up = build_resnest_backbone(cfg, input_shape) else: bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS top_levels = cfg.MODEL.FCOS.TOP_LEVELS in_channels_top = out_channels if top_levels == 2: top_block = LastLevelP6P7(in_channels_top, out_channels, "p5") if top_levels == 1: top_block = LastLevelP6(in_channels_top, out_channels, "p5") elif top_levels == 0: top_block = None backbone = FPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=top_block, fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone #BiFPN #https://github.com/sxhxliang/detectron2_backbone/blob/master/detectron2_backbone/backbone/bifpn.py
def test_resnet_scriptability(self): cfg = get_cfg() resnet = build_resnet_backbone(cfg, ShapeSpec(channels=3)) scripted_resnet = torch.jit.script(resnet) inp = torch.rand(2, 3, 100, 100) out1 = resnet(inp)["res4"] out2 = scripted_resnet(inp)["res4"] self.assertTrue(torch.allclose(out1, out2))
def __init__(self, cfg, input_shape): bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS super().__init__( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=LastLevelMaxPool(), fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) input_shapes = bottom_up.output_shape() self.quantizer = build_quantizer(cfg, input_shapes)
def build_resnet_fpn_p5_backbone(cfg, input_shape: ShapeSpec): """ Build ResNet-FPN backbone with P6 and P7 from P5 feature. Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS in_channels_p6p7 = out_channels backbone = FPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=LastLevelP6P7fromP5(in_channels_p6p7, out_channels), fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone
def build_resnet_pafpn_backbone(cfg, input_shape: ShapeSpec): """ Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS backbone = PAFPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=LastLevelMaxPool(), fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone
def build_p35_fcos_resnet_bifpn_backbone(cfg, input_shape: ShapeSpec): """ Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.BIFPN.OUT_CHANNELS num_repeats = cfg.MODEL.BIFPN.NUM_BIFPN top_levels = 0 backbone = BiFPN(bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, num_top_levels=top_levels, num_repeats=num_repeats, norm=cfg.MODEL.BIFPN.NORM) return backbone
def build_retinanet_resnet_vt_fpn_backbone_use_p5(cfg, input_shape: ShapeSpec): """ Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.FPN.IN_FEATURES out_channels = cfg.MODEL.FPN.OUT_CHANNELS in_channels_p6p7 = out_channels backbone = VT_FPN( bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, norm=cfg.MODEL.FPN.NORM, top_block=LastLevelP6P7(in_channels_p6p7, out_channels, in_feature="p5"), fuse_type=cfg.MODEL.FPN.FUSE_TYPE, ) return backbone
def __init__(self, cfg=None, model_path=None, depth=101, vec_dim=128, max_pool=False): super(ResNetbasedNet, self).__init__() if cfg is not None: model = build_resnet_backbone( cfg, ShapeSpec(channels=len(cfg.MODEL.PIXEL_MEAN))) self.backbone = nn.Sequential(*list(model.children())) else: model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet{}'.format(depth), pretrained=True) self.backbone = nn.Sequential(*list(model.children())[:-2]) self.max_pool = nn.AdaptiveMaxPool2d( (1, 1)) if max_pool else nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(2048, vec_dim) weight_init.c2_xavier_fill(self.fc) if model_path is not None: model.load_state_dict(torch.load(model_path))
def build_fcos_resnet_bifpn_backbone(cfg, input_shape: ShapeSpec): """ Args: cfg: a detectron2 CfgNode Returns: backbone (Backbone): backbone module, must be a subclass of :class:`Backbone`. """ if cfg.MODEL.MOBILENET: bottom_up = build_mnv2_backbone(cfg, input_shape) else: bottom_up = build_resnet_backbone(cfg, input_shape) in_features = cfg.MODEL.BiFPN.IN_FEATURES out_channels = cfg.MODEL.BiFPN.OUT_CHANNELS num_repeats = cfg.MODEL.BiFPN.NUM_REPEATS top_levels = cfg.MODEL.FCOS.TOP_LEVELS backbone = BiFPN(bottom_up=bottom_up, in_features=in_features, out_channels=out_channels, num_top_levels=top_levels, num_repeats=num_repeats, norm=cfg.MODEL.BiFPN.NORM) return backbone
import torch from detectron2.config import get_cfg from detectron2 import model_zoo from detectron2.layers import ShapeSpec from detectron2.modeling.backbone import build_resnet_backbone cfg = get_cfg() cfg.merge_from_file( model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")) cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 512 cfg.MODEL.ROI_HEADS.NUM_CLASSES = 2 cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.85 net = build_resnet_backbone(cfg, ShapeSpec(channels=3)) temp = torch.load("weight.pt") net.load_state_dict({k: temp[k] for k in net.state_dict()}) net.eval() with torch.no_grad(): torch.save(net(torch.load("data.pt")), "res1.pt")
def __init__(self, cfg=None, load_path=None, depth=101, vec_dim=128, max_pool=False, clf1_num=None, clf2_num=None, adv_eta=None): super(ResNetbasedNet, self).__init__() self.load = True if load_path is not None else False self.clf1 = True if clf1_num is not None else False self.clf2 = True if clf2_num is not None else False self.adv_eta = Variable( torch.tensor(adv_eta).type(torch.float), requires_grad=False) if adv_eta is not None else None if cfg is not None: model = build_resnet_backbone( cfg, ShapeSpec(channels=len(cfg.MODEL.PIXEL_MEAN))) pretrained_model = torch.load(cfg.MODEL.WEIGHTS) cur_state = model.state_dict() mapped_dict = {} for name, param in pretrained_model.items(): if name == 'model': for p in param: if p.replace('backbone.bottom_up.', '') in cur_state: mapped_dict[p.replace('backbone.bottom_up.', '')] = param[p] model.load_state_dict(mapped_dict) self.backbone = nn.Sequential(*list(model.children())) else: model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet{}'.format(depth), pretrained=not self.load) self.backbone = nn.Sequential(*list(model.children())[:-2]) self.max_pool = nn.AdaptiveMaxPool2d( (1, 1)) if max_pool else nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(2048, vec_dim) if self.clf1: self.clf1_layer = nn.Sequential(nn.Linear(vec_dim, vec_dim), nn.BatchNorm1d(vec_dim), nn.ReLU(), nn.Linear(vec_dim, clf1_num)) if self.clf2: self.clf2_layer = nn.Sequential(nn.Linear(vec_dim, vec_dim), nn.BatchNorm1d(vec_dim), nn.ReLU(), nn.Linear(vec_dim, clf2_num)) if self.load: load_model = torch.load(load_path) mapped_dict = { 'backbone': (self.backbone, {}), 'fc': (self.fc, {}) } if self.clf1: mapped_dict['clf1_layer'] = (self.clf1_layer, {}) if self.clf2: # print(self.clf2_layer.state_dict()) mapped_dict['clf2_layer'] = (self.clf2_layer, {}) for name, param in load_model.items(): if name.split('.')[0] in mapped_dict.keys(): mapped_dict[name.split('.')[0]][1]['.'.join( name.split('.')[1:])] = param for layers in mapped_dict.keys(): mapped_dict[layers][0].load_state_dict(mapped_dict[layers][1])