-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmodel.py
37 lines (32 loc) · 1.22 KB
/
model.py
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
import torch.nn as nn
import torchvision.models as models
import torch
import config
class LULC_Model(nn.Module):
def __init__(self):
super().__init__()
self.network = models.wide_resnet50_2(pretrained=True)
n_inputs = self.network.fc.in_features
self.network.fc = nn.Sequential(
nn.Linear(n_inputs, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, config.NUM_CLASSES),
nn.LogSoftmax(dim=1)
)
def forward(self, xb):
return self.network(xb)
def freeze(self):
for param in self.network.parameters():
param.require_grad=False
for param in self.network.fc.parameters():
param.require_grad=True
def unfreeze(self):
for param in self.network.parameters():
param.require_grad=True
def get_model():
use_cuda = torch.cuda.is_available()
DEVICE = torch.device('cuda' if use_cuda else 'cpu')
model = LULC_Model()
model.load_state_dict(torch.load(config.MODEL_PATH, map_location=lambda storage, loc: storage))
return model