VanLinLin commited on
Commit
63334c5
·
verified ·
1 Parent(s): 0907ba6

Upload model.py

Browse files
Files changed (1) hide show
  1. model.py +24 -0
model.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision
3
+ from torch import nn
4
+
5
+
6
+ def create_effnetb2_model(num_classese: int = 101, # default output classes = 3 (pizza, steak , sushi)
7
+ seed: int = 42):
8
+ # 1, 2, 3 Create EffNetB2 pretained weights, transforms and model
9
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
10
+ transforms = weights.transforms()
11
+ model = torchvision.models.efficientnet_b2(weights=weights)
12
+
13
+ # 4. Freeze all layers in the base model
14
+ for param in model.parameters():
15
+ param.requires_grad = False
16
+
17
+ # 5. Change classifier head with random seed for reproducibility
18
+ torch.manual_seed(seed)
19
+ model.classifier = nn.Sequential(
20
+ nn.Dropout(p=.3, inplace=True),
21
+ nn.Linear(in_features=1408, out_features=num_classese)
22
+ )
23
+
24
+ return model, transforms