ranggaaldosas commited on
Commit
cc65108
1 Parent(s): 1b1bada

Upload model.py

Browse files
Files changed (1) hide show
  1. model.py +35 -0
model.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+
4
+ from torch import nn
5
+
6
+
7
+ def create_effnetb2_model(num_classes: int = 101, seed: int = 42):
8
+ """Creates an EfficientNetB2 feature extractor model and transforms.
9
+
10
+ Args:
11
+ num_classes (int, optional): number of classes in the classifier head.
12
+ Defaults to 3.
13
+ seed (int, optional): random seed value. Defaults to 42.
14
+
15
+ Returns:
16
+ model (torch.nn.Module): EffNetB2 feature extractor model.
17
+ transforms (torchvision.transforms): EffNetB2 image transforms.
18
+ """
19
+ # Create EffNetB2 pretrained weights, transforms and model
20
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
21
+ transforms = weights.transforms()
22
+ model = torchvision.models.efficientnet_b2(weights=weights)
23
+
24
+ # Freeze all layers in base model
25
+ for param in model.parameters():
26
+ param.requires_grad = False
27
+
28
+ # Change classifier head with random seed for reproducibility
29
+ torch.manual_seed(seed)
30
+ model.classifier = nn.Sequential(
31
+ nn.Dropout(p=0.3, inplace=True),
32
+ nn.Linear(in_features=1408, out_features=num_classes),
33
+ )
34
+
35
+ return model, transforms