-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
59 lines (48 loc) · 1.72 KB
/
models.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import torch
import torch.nn as nn
from torchvision.models import alexnet, AlexNet_Weights, vgg16, VGG16_Weights
alexnet = alexnet(weights=AlexNet_Weights.IMAGENET1K_V1)
alexnet.__name__ = "alexnet"
vgg16 = vgg16(weights=VGG16_Weights.IMAGENET1K_V1)
vgg16.__name__ = "vgg16"
# LeNet5 network
class LeNet5(nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
self.conv1 = nn.Conv2d(1, 6, kernel_size=5)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self.conv2 = nn.Conv2d(6, 16, kernel_size=5)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = torch.relu(self.conv1(x))
x = torch.max_pool2d(x, 2)
x = torch.relu(self.conv2(x))
x = torch.max_pool2d(x, 2)
x = x.reshape(-1, 16 * 4 * 4)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# LeNet300 network
class LeNet300(nn.Module):
def __init__(self):
super(LeNet300, self).__init__()
self.fc1 = nn.Linear(28 * 28, 300)
self.fc2 = nn.Linear(300, 100)
self.fc3 = nn.Linear(100, 10)
self.relu = nn.ReLU()
def forward(self, x):
x = x.reshape(-1, 28 * 28) # Flatten the input images
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
leNet5 = LeNet5()
leNet5.load_state_dict(torch.load("trained_models/trainedModel5.p"))
leNet5.__name__ = "lenet5"
leNet300 = LeNet300()
leNet300.load_state_dict(torch.load("trained_models/trainedModel300.p"))
leNet300.__name__ = "lenet300"