File size: 910 Bytes
3a9d31e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60d40f7
3a9d31e
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch
import torch.nn.functional as F
from torch import nn
import timm
from torch.nn.parameter import Parameter

class Backbone(nn.Module) :
    def __init__(self,name,pretrained) :
        super(Backbone,self).__init__()
        self.net = timm.create_model(name,pretrained=pretrained)
        self.out_features = self.net.get_classifier().in_features
    def forward(self,x) :
        x = self.net.forward_features(x)
        return x

class CustomModel(nn.Module) :
    def __init__(self) :
        super(CustomModel,self).__init__()
        self.backbone = Backbone("tf_efficientnetv2_s",False)
        self.pooling = nn.AdaptiveAvgPool2d(1)
        self.head = nn.Linear(self.backbone.out_features,1)
    def forward(self,x) :
        x = self.backbone(x)
        x = self.pooling(x).squeeze()
        target = self.head(x)
        output = {}
        output['label'] = target
        return output