-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_model.py
More file actions
42 lines (31 loc) · 1.25 KB
/
Copy pathcreate_model.py
File metadata and controls
42 lines (31 loc) · 1.25 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
from torchvision import models
import torch
import utils_nn
import torch.optim as optim
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
#%% Instantiate original VGG model WITH weights
vgg_base = models.vgg16(pretrained=True)
features = utils_nn.encoder()
classifier = utils_nn.decoder()
gazenet = utils_nn.VGG_homemade(features, preset=False)
#%% Initializing weights BEFORE adding new classifier
base_state_dict = vgg_base.state_dict()
gazenet.load_state_dict(base_state_dict, strict=False)
#%% Disable parameter tuning for original weights
# for param in gazenet.parameters():
# param.requires_grad = False
#%% Add fully convolutional classifier
gazenet.classifier = classifier
#%%
loss_fn = torch.nn.BCEWithLogitsLoss(pos_weight=torch.tensor([9.1],device=device), reduction='mean')
optimizer = optim.SGD(gazenet.parameters(), lr=0.01, momentum=0.9, weight_decay=0.0005)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=10)
epoch = 0
loss = 0
torch.save({
'epoch': epoch,
'model_state_dict': gazenet.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,
'scheduler': scheduler.state_dict(),
}, 'models/gazenet_dict')