File size: 844 Bytes
c17bef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
import torch
import torch.nn as nn
from torchvision import models

class EfficientNetB4Classifier(nn.Module):
    def __init__(self, train_base=False):
        super().__init__()
        self.base_model = models.efficientnet_b4(weights=models.EfficientNet_B4_Weights.DEFAULT)

        for param in self.base_model.features.parameters():
            param.requires_grad = train_base

        self.classifier = nn.Sequential(
            nn.BatchNorm1d(1792),
            nn.Dropout(0.5),
            nn.Linear(1792, 256),
            nn.ReLU(),
            nn.BatchNorm1d(256),
            nn.Dropout(0.5),
            nn.Linear(256, 1),
            nn.Sigmoid()
        )

    def forward(self, x):
        x = self.base_model.features(x)
        x = self.base_model.avgpool(x)
        x = torch.flatten(x, 1)
        return self.classifier(x)