-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
83 lines (64 loc) · 2.55 KB
/
models.py
File metadata and controls
83 lines (64 loc) · 2.55 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from torch import nn
import torchvision
class NeuralNetwork(nn.Module):
"""
Generic fully-connected network class
The first argument of the constructor should be the number of input features, and the last should be the number of outputs
"""
# layers = int layer sizes, starting with the input layer
def __init__(self, *layers):
super(NeuralNetwork, self).__init__()
self.flatten = nn.Flatten()
stack = []
for i in range(len(layers) - 1):
stack.extend([nn.Linear(layers[i], layers[i + 1]), nn.ReLU()])
self.linear_relu_stack = nn.Sequential(*stack[:-1])
def forward(self, x):
x = self.flatten(x)
logits = self.linear_relu_stack(x)
return logits
class TransferModel18(nn.Module):
"""
ResNet18 transfer learning model
By default we set the fully-connected classifier layer to a single 512x2 layer
"""
def __init__(self, pretrained=True, freeze=True, device='cpu'):
super(TransferModel18, self).__init__()
if pretrained:
self.model = torchvision.models.resnet18(weights='DEFAULT').to(device)
else:
self.model = torchvision.models.resnet18().to(device)
if freeze:
for param in self.model.parameters():
param.requires_grad = False
# Fully-connected layer
self.model.fc = nn.Sequential(
nn.Linear(in_features=512, out_features=2, bias=True, device=device),
)
for layer in self.model.fc:
if hasattr(layer, 'weight'):
nn.init.xavier_uniform_(layer.weight)
def forward(self, x):
return self.model(x).squeeze()
class TransferModel50(nn.Module):
"""
ResNet50 transfer learning model
"""
def __init__(self, pretrained=True, freeze=True, device='cpu'):
super(TransferModel50, self).__init__()
if pretrained:
self.model = torchvision.models.resnet50(weights=torchvision.models.ResNet50_Weights.default).to(device)
else:
self.model = torchvision.models.resnet50().to(device)
if freeze:
for param in self.model.parameters():
param.requires_grad = False
# Fully-connected layer
self.model.fc = nn.Sequential(
nn.Linear(in_features=2048, out_features=2, bias=True, device=device),
)
for layer in self.model.fc:
if hasattr(layer, 'weight'):
nn.init.xavier_uniform_(layer.weight)
def forward(self, x):
return self.model(x).squeeze()