File size: 788 Bytes
3691d6c
 
 
facffc1
3691d6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torchvision
from torch import nn

def create_effnet_b1(num_classes:int=5, seed:int=42):
    # 1. Get the base mdoel with pretrained weights and send to target device
    weights = torchvision.models.EfficientNet_B1_Weights.DEFAULT
    transforms = weights.transforms()
    model = torchvision.models.efficientnet_b1(weights=weights)#.to(device)

    # 2. Freeze the base model layers
    for param in model.features.parameters():
        param.requires_grad = False

    # 3. Change the classifier head
    model.classifier = nn.Sequential(
        nn.Dropout(p=0.2),
        nn.Linear(in_features=1280, out_features=5)
    )#.to(device)

    # 5. Give the model a name
    model.name = "effnet_b1"
    print(f"[INFO] Created new {model.name} model.")
    return model, transforms