-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
108 lines (86 loc) · 3.34 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import torch
import torch.nn.functional as F
class Net(torch.nn.Module):
def __init__(self, inputsize):
super(Net, self).__init__()
ncha, size, _ = inputsize
self.relu = torch.nn.ReLU()
self.maxpool = torch.nn.MaxPool2d(2)
#self.drop1 = torch.nn.Dropout(0.2)
self.padding = torch.nn.ReplicationPad2d(1)
self.c1 = torch.nn.Conv2d(ncha, 64, kernel_size=2, stride=1, padding=0, bias=False)
self.c2 = torch.nn.Conv2d(64, 128, kernel_size=2, stride=1, padding=0, bias=False)
self.c3 = torch.nn.Conv2d(128, 256, kernel_size=2, stride=1, padding=0, bias=False)
self.fc1 = torch.nn.Linear(256 * 4 * 4, 1000, bias=False)
self.fc2 = torch.nn.Linear(1000, 1000, bias=False)
self.fc3 = torch.nn.Linear(1000, 10, bias=False)
self.Fr = torch.nn.Linear(1000, 4, bias=False)
torch.nn.init.xavier_normal_(self.fc1.weight)
torch.nn.init.xavier_normal_(self.fc2.weight)
torch.nn.init.xavier_normal_(self.fc3.weight)
return
def forward(self, x,):
h_list = []
x_list = []
# Gated
x = self.padding(x)
x_list.append(torch.mean(x, 0, True))
#con1 = self.drop1(self.relu(self.c1(x)))
con1 = self.relu(self.c1(x))
con1_p = self.maxpool(con1)
con1_p = self.padding(con1_p)
x_list.append(torch.mean(con1_p, 0, True))
#con2 = self.drop1(self.relu(self.c2(con1_p)))
con2 = self.relu(self.c2(con1_p))
con2_p = self.maxpool(con2)
con2_p = self.padding(con2_p)
x_list.append(torch.mean(con2_p, 0, True))
#con3 = self.drop1(self.relu(self.c3(con2_p)))
con3 = self.relu(self.c3(con2_p))
con3_p = self.maxpool(con3)
h = con3_p.view(x.size(0), -1)
h = F.normalize(h)
feature = h
h_list.append(torch.mean(h, 0, True))
h = self.relu(self.fc1(h))
h_list.append(torch.mean(h, 0, True))
h = self.relu(self.fc2(h))
h_list.append(torch.mean(h, 0, True))
y = self.fc3(h)
return y, h_list, x_list, feature
def feedforward(self, x):
x = self.padding(x)
#con1 = self.drop1(self.relu(self.c1(x)))
con1 = self.relu(self.c1(x))
con1_p = self.maxpool(con1)
con1_p = self.padding(con1_p)
#con2 = self.drop1(self.relu(self.c2(con1_p)))
con2 = self.relu(self.c2(con1_p))
con2_p = self.maxpool(con2)
con2_p = self.padding(con2_p)
#con3 = self.drop1(self.relu(self.c3(con2_p)))
con3 = self.relu(self.c3(con2_p))
con3_p = self.maxpool(con3)
h = con3_p.view(x.size(0), -1)
h = F.normalize(h)
return h
def train_classifier(self, x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.fc3(x)
return x
def SSL(self, x):
x = self.padding(x)
con1 = self.relu(self.c1(x))
con1_p = self.maxpool(con1)
con1_p = self.padding(con1_p)
con2 = self.relu(self.c2(con1_p))
con2_p = self.maxpool(con2)
con2_p = self.padding(con2_p)
con3 = self.relu(self.c3(con2_p))
con3_p = self.maxpool(con3)
h = con3_p.view(x.size(0), -1)
h = self.relu(self.fc1(h))
h = self.relu(self.fc2(h))
h = self.Fr(h)
return h