-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
86 lines (77 loc) · 3.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Model ideas implementation
"""
import torch.nn as nn
import torch.nn.functional as F
class EmbeddingNet(nn.Module):
def __init__(self):
super(EmbeddingNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(in_features=64 * 4 * 4, out_features=256)
self.fc2 = nn.Linear(in_features=256, out_features=256)
self.fc3 = nn.Linear(in_features=256, out_features=2)
def forward(self, x):
output = self.pool(F.relu(self.conv1(x)))
output = self.pool(F.relu(self.conv2(output)))
output = output.view(-1, 64 * 4 * 4)
output = F.relu(self.fc1(output))
output = F.relu(self.fc2(output))
output = self.fc3(output)
return output
#def get_embedding(self, x):
# return self.forward(x)
class TripletNet(nn.Module):
def __init__(self, embedding_net):
super(TripletNet, self).__init__()
self.embedding_net = embedding_net
def forward(self, x1, x2, x3):
output1 = self.embedding_net(x1)
output2 = self.embedding_net(x2)
output3 = self.embedding_net(x3)
return output1, output2, output3
#def get_embedding(self, x):
# return self.embedding_net(x)
class mini_vgg(nn.Module):
def __init__(self):
super(mini_vgg, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, padding=3)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=32, kernel_size=3, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
# 16*16
self.conv3 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, padding=1)
self.fc1 = nn.Linear(in_features=64 * 8 * 8, out_features=512)
self.fc2 = nn.Linear(in_features=512, out_features=10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool(F.relu(self.conv2(x)))
x = F.relu(self.conv3(x))
x = self.pool(F.relu(self.conv4(x)))
x = x.view(-1, 64 * 8 * 8)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x
class CNNModel5(nn.Module):
def __init__(self):
super(CNNModel5, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2)
self.bn1 = nn.BatchNorm2d(6)
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)
self.bn2 = nn.BatchNorm2d(16)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(in_features=16 * 5 * 5, out_features=128)
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(in_features=128, out_features=64)
self.out = nn.Linear(in_features=64, out_features=10)
def forward(self, x):
x = self.pool(F.elu(self.bn1(self.conv1(x))))
x = self.pool(F.elu(self.bn2(self.conv2(x))))
x = x.view(-1, 16 * 5 * 5)
x = F.elu(self.fc1(x))
x = self.dropout(x)
x = F.elu(self.fc2(x))
x = self.dropout(x)
x = self.out(x)
return x