Example #1
0
 def __init__(self, num_classes, head_ps=0.5, pretrained=True, in_channels=3, **kwargs):
     self.effnet = EfficientNet.from_pretrained(self.arch)
     if in_channels == 1:
         w = self.effnet._conv_stem.weight.data[:,0].unsqueeze(1)
         out_channels = self.effnet._conv_stem.out_channels
         self.effnet._conv_stem = Conv2dStaticSamePadding(1, out_channels, kernel_size=(3,3), stride=(2,2),
                                                          bias=False, image_size=(128,128))
         self.effnet._conv_stem.weight.data = w
     num_features = self.effnet._bn1.num_features
     self.head = create_head(num_features*2, num_classes, ps=head_ps)
Example #2
0
 def __init__(self, in_channels, out_channels, image_size=256):
     super().__init__()
     self.net = EfficientNetOri.from_pretrained('efficientnet-b0',
                                                num_classes=out_channels)
     if (in_channels is not 3):
         self.net._conv_stem = Conv2dStaticSamePadding(
             in_channels,
             32,
             image_size=image_size,
             kernel_size=(3, 3),
             stride=(2, 2),
             bias=False)
Example #3
0
 def __init__(self, in_channels, out_channels, image_size=512):
     super().__init__()
     self.net = EfficientNetOri.from_name('efficientnet-b0')
     if (in_channels != 3):
         self.net._conv_stem = Conv2dStaticSamePadding(
             in_channels,
             32,
             image_size=image_size,
             kernel_size=(3, 3),
             stride=(2, 2),
             bias=False)
     feature = self.net._fc.in_features
     self.net._fc = nn.Linear(in_features=feature,
                              out_features=out_channels,
                              bias=True)
Example #4
0
 def __init__(self, coeff=0):
     super(AttnCNN, self).__init__()
     self.model = EfficientNet.from_pretrained(
         model_name='efficientnet-b%d' % coeff)
     self.model._conv_stem = Conv2dStaticSamePadding(
         1,
         self.model._conv_stem.out_channels,
         kernel_size=(3, 3),
         stride=(2, 2),
         bias=False,
         image_size=128)
     self.out = AttnBiLSTM(num_classes=7,
                           input_size=self.model._fc.in_features,
                           hidden_size=512)
     self.feature_dim = self.out.feature_dim
     del self.model._fc
Example #5
0
 def __init__(self, pretrain: str, n_classes: int = 1):
     """
     Args:
         pretrain (str): one of:
             - 'efficientnet-b0'
             - 'efficientnet-b1'
             - 'efficientnet-b2'
             - 'efficientnet-b3'
             - 'efficientnet-b4'
             - 'efficientnet-b5'
             - 'efficientnet-b6'
             - 'efficientnet-b7'
     """
     super().__init__(pretrain, n_classes)
     self.backbone._conv_stem = Conv2dStaticSamePadding(
         self.backbone._conv_stem.in_channels,
         self.backbone._conv_stem.out_channels,
         kernel_size=3,
         stride=2,  # lower stride ?
         image_size=224,
         dilation=2,
         bias=False)
Example #6
0
elif MODEL == "EFFICIENT":
  if PRETRAIN:
    model = EfficientNet.from_pretrained('efficientnet-b6', num_classes=555)
    checkpoint = torch.load("experiment/efficient_pretrain_nabirds_model_1.pth")
    model.load_state_dict(checkpoint) 
    model._fc = nn.Linear(2048, args.num_classes)
  else:
    # model = EFFICIENT(args.num_classes)
    model = EfficientNet.from_pretrained('efficientnet-b6', num_classes=args.num_classes)
    if args.model:
        print("loading pretrained model")
        checkpoint = torch.load(args.model)
        model.load_state_dict(checkpoint) 

    if CHANNELS == "DOUBLE":
      model._conv_stem = Conv2dStaticSamePadding(in_channels=3*2, out_channels=56, kernel_size=(3, 3), stride=2, image_size=(456, 456))
    if CHANNELS == "TRIPLE":
      model._conv_stem = Conv2dStaticSamePadding(in_channels=3*3, out_channels=56, kernel_size=(3, 3), stride=2, image_size=(456, 456))


elif MODEL == "RESNEXT":
  model = torchvision.models.resnext50_32x4d(pretrained=True)
  model.fc = nn.Sequential(
      nn.Linear(2048, 256),
      nn.ReLU(),
      nn.Linear(256, 128),
      nn.ReLU(),
      nn.Linear(128, args.num_classes),
    )
  if FREEZE:
    for name, param in model.named_parameters():