def create_efficientnet(model_name, pretrained=True, num_classes=1000, norm_layer=torch.nn.BatchNorm2d, expand_ratio=6, group_dim=1): """ Creates EfficientNet instance with the predefined parameters. Parameters: model_name: Name of the model. pretrained: if true the network is initialized with pretrained weights. norm_layer: The used normalization layer in the network. eg. torch.nn.Identity means no initialization. expand_ratio: The used expand ratio in the blocks. Official EfficientNet uses 6 group_dim: Dimensionality of the depthwise convolution. Official EfficientNet uses 1. Returns: The initialized EfficientNet model. """ EfficientNet._check_model_name_is_valid(model_name) blocks_args, global_params = get_model_params(model_name, {"num_classes": num_classes}) # Change expand ratio for idx in range(1, len(blocks_args)): blocks_args[idx] = blocks_args[idx]._replace(expand_ratio=expand_ratio) model = EfficientNet(blocks_args, global_params) model.set_swish(memory_efficient=False) if group_dim > 1: replace_en_depthwise_conv(model, group_dim) if not isinstance(norm_layer, torch.nn.BatchNorm2d): replace_bn(model, norm_layer) init_efficientnet(model) if pretrained: pretrained_model = EfficientNet.from_name(model_name) load_modified_model_from_state(model, pretrained_model.state_dict()) return model
def TwoWayFPNBackbone(): out_channels = 256 override_params = {'num_classes': 1000} paras = get_model_params('efficientnet-b5', override_params) model = EfficientNet(paras[0], paras[1]) arr = model.return_sub() fpn = FPN(arr, paras[0], paras[1], out_channels=out_channels) return fpn
def __init__( self, num_classes=40, neck='bnneck', neck_feat='after', model_path='/home/zhoumi/.cache/torch/checkpoints/efficientnet-b4-6ed6700e.pth' ): super(efficient_baseline, self).__init__() #1.4, 1.8, 380, 0.4 blocks_args, global_params = efficientnet(width_coefficient=1.4, depth_coefficient=1.8, dropout_rate=0.4, image_size=380) self.base = EfficientNet(blocks_args=blocks_args, global_params=global_params) self.base.load_param(model_path) print('Loading pretrained ImageNet model......') # self.gap = nn.AdaptiveAvgPool2d(1) # self.gap = nn.AdaptiveMaxPool2d(1) self.num_classes = num_classes self.neck = neck self.neck_feat = neck_feat if self.neck == 'no': self.classifier = nn.Linear(self.in_planes, self.num_classes) # self.classifier = nn.Linear(self.in_planes, self.num_classes, bias=False) # new add by luo # self.classifier.apply(weights_init_classifier) # new add by luo elif self.neck == 'bnneck': self.bottleneck = nn.BatchNorm1d(self.in_planes) self.bottleneck.bias.requires_grad_(False) # no shift self.classifier = nn.Linear(self.in_planes, self.num_classes, bias=False) self.bottleneck.apply(weights_init_kaiming) self.classifier.apply(weights_init_classifier)
def create_backbone(self, compound_coefficient): #todo : load diffirent EfficientNet based on compound_coefficient return EfficientNet()
def from_name(cls, model_name, override_params=None): cls._check_model_name_is_valid(model_name) blocks_args, global_params = get_model_params(model_name, override_params) return EfficientNet(blocks_args, global_params)