-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
239 lines (207 loc) · 8.35 KB
/
Copy pathmodels.py
File metadata and controls
239 lines (207 loc) · 8.35 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import copy
# https://xifengguo.github.io/papers/ICONIP17-DCEC.pdf - paper
# https://github.com/XifengGuo/DCEC/blob/master/DCEC.py - TF code
class clusteringLayer(nn.Module):
def __init__(self, n_clusters, dim, alpha=1.0) -> None:
super(clusteringLayer, self).__init__()
self.dim = dim
self.n_clusters = n_clusters
self.alpha = alpha
self.mu = nn.Parameter(torch.Tensor(self.n_clusters, self.dim))
self.mu = nn.init.xavier_uniform_(self.mu)
pass
def forward(self, x):
# q1 = 1.0 / (1.0 + torch.norm(x.unsqueeze(1) - self.mu, p=2, dim=-1)**2)
# q2 = 1.0 / torch.sum((1.0 + torch.norm(x.unsqueeze(1) - self.mu, p=2, dim=-1)**2), dim=-1)
# q = q1 / q2.unsqueeze(1)
# return q
x = x.unsqueeze(1) - self.mu
x = torch.mul(x, x)
x = torch.sum(x, dim=2)
x = 1.0 + (x / self.alpha)
x = 1.0 / x
x = x ** ((self.alpha + 1.0) / 2.0)
x = torch.t(x) / torch.sum(x, dim=1)
x = torch.t(x)
return x
def set_mu(self, tensor):
self.mu = nn.Parameter(tensor)
class ClusterAutoEncoder(nn.Module):
def __init__(self, input_shape, n_clusters, dim) -> None:
super(ClusterAutoEncoder, self).__init__()
self.input_shape = input_shape
self.dim = dim
self.n_clusters = n_clusters
self.encoder2 = nn.Sequential(
nn.Conv2d(1, 16, 3, stride=3, padding=1),
nn.ReLU(True),
nn.MaxPool2d(2, stride=2),
nn.Conv2d(16, 8, 3, stride=2, padding=1),
nn.ReLU(True),
nn.MaxPool2d(2, stride=1)
)
self.embedding2 = nn.Linear(8*4, self.dim)
self.deembedding2 = nn.Linear(self.dim, 64*64)
self.embedding = nn.Linear(64, self.dim)
self.deembedding = nn.Linear(self.dim, 64)
self.cluster_layer = clusteringLayer(
self.n_clusters, dim=self.dim)
self.decoder2 = nn.Sequential(
nn.ConvTranspose2d(8, 16, 3, stride=2),
nn.ReLU(True),
nn.ConvTranspose2d(16, 8, 5, stride=3, padding=1),
nn.ReLU(True),
nn.ConvTranspose2d(8, 1, 2, stride=2, padding=1),
nn.Tanh()
)
self.encoder = nn.Sequential( # like the Composition layer you built
nn.Conv2d(1, 16, 3, stride=2, padding=1),
nn.ReLU(),
nn.Conv2d(16, 32, 3, stride=2, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, 7)
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(64, 32, 7),
nn.ReLU(),
nn.ConvTranspose2d(32, 16, 3, stride=2, padding=1, output_padding=1),
nn.ReLU(),
nn.ConvTranspose2d(16, 1, 3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
)
def calc_p(self, q):
p1 = q**2 / (torch.sum(q, dim=0))
p2 = torch.sum((q**2 / (torch.sum(q, dim=0))), dim=1)
return p1 / p2.unsqueeze(1)
# def forward(self, x):
# x = self.encoder(x)
# x = torch.flatten(x, start_dim=1)
# latent = self.embedding(x)
# q = self.cluster_layer(latent)
# x = self.deembedding(latent)
# x = x.view(-1, 64, 1, 1)
# x = self.decoder(x)
# # clac p
# p = self.calc_p(q)
# return x, q, latent, p
def forward(self, x):
x = self.encoder(x)
x = torch.flatten(x, start_dim=1)
latent = self.embedding(x)
q = self.cluster_layer(latent)
x = self.deembedding(latent)
x = x.view(-1, 64, 1, 1)
x = self.decoder(x)
# clac p
# p = self.calc_p(q)
return x, q, latent
class Autoencoder(nn.Module):
def __init__(self):
super(Autoencoder, self).__init__()
self.encoder = nn.Sequential( # like the Composition layer you built
nn.Conv2d(1, 16, 3, stride=2, padding=1),
nn.ReLU(),
nn.Conv2d(16, 32, 3, stride=2, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, 7)
)
self.decoder = nn.Sequential(
nn.ConvTranspose2d(64, 32, 7),
nn.ReLU(),
nn.ConvTranspose2d(32, 16, 3, stride=2, padding=1, output_padding=1),
nn.ReLU(),
nn.ConvTranspose2d(16, 1, 3, stride=2, padding=1, output_padding=1),
nn.Sigmoid()
)
def forward(self, x):
x = self.encoder(x)
x = self.decoder(x)
return x
class ClusterlingLayer(nn.Module):
def __init__(self, in_features=10, out_features=10, alpha=1.0):
super(ClusterlingLayer, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.alpha = alpha
self.weight = nn.Parameter(torch.Tensor(self.out_features, self.in_features))
self.weight = nn.init.xavier_uniform_(self.weight)
def forward(self, x):
x = x.unsqueeze(1) - self.weight
x = torch.mul(x, x)
x = torch.sum(x, dim=2)
x = 1.0 + (x / self.alpha)
x = 1.0 / x
x = x ** ((self.alpha +1.0) / 2.0)
x = torch.t(x) / torch.sum(x, dim=1)
x = torch.t(x)
return x
def extra_repr(self):
return 'in_features={}, out_features={}, alpha={}'.format(
self.in_features, self.out_features, self.alpha
)
def set_mu(self, tensor):
self.weight = nn.Parameter(tensor)
class CAE_3(nn.Module):
def __init__(self, input_shape=[128,128,3], num_clusters=10, filters=[32, 64, 128], leaky=True, neg_slope=0.01, activations=False, bias=True):
super(CAE_3, self).__init__()
self.activations = activations
# bias = True
self.pretrained = False
self.num_clusters = num_clusters
self.input_shape = input_shape
self.filters = filters
self.conv1 = nn.Conv2d(input_shape[2], filters[0], 5, stride=2, padding=2, bias=bias)
if leaky:
self.relu = nn.LeakyReLU(negative_slope=neg_slope)
else:
self.relu = nn.ReLU(inplace=False)
self.conv2 = nn.Conv2d(filters[0], filters[1], 5, stride=2, padding=2, bias=bias)
self.conv3 = nn.Conv2d(filters[1], filters[2], 3, stride=2, padding=0, bias=bias)
lin_features_len = ((input_shape[0]//2//2-1) // 2) * ((input_shape[0]//2//2-1) // 2) * filters[2]
self.embedding = nn.Linear(lin_features_len, num_clusters, bias=bias)
self.deembedding = nn.Linear(num_clusters, lin_features_len, bias=bias)
out_pad = 1 if input_shape[0] // 2 // 2 % 2 == 0 else 0
self.deconv3 = nn.ConvTranspose2d(filters[2], filters[1], 3, stride=2, padding=0, output_padding=out_pad, bias=bias)
out_pad = 1 if input_shape[0] // 2 % 2 == 0 else 0
self.deconv2 = nn.ConvTranspose2d(filters[1], filters[0], 5, stride=2, padding=2, output_padding=out_pad, bias=bias)
out_pad = 1 if input_shape[0] % 2 == 0 else 0
self.deconv1 = nn.ConvTranspose2d(filters[0], input_shape[2], 5, stride=2, padding=2, output_padding=out_pad, bias=bias)
self.clustering = ClusterlingLayer(num_clusters, num_clusters)
# ReLU copies for graph representation in tensorboard
self.relu1_1 = copy.deepcopy(self.relu)
self.relu2_1 = copy.deepcopy(self.relu)
self.relu3_1 = copy.deepcopy(self.relu)
self.relu1_2 = copy.deepcopy(self.relu)
self.relu2_2 = copy.deepcopy(self.relu)
self.relu3_2 = copy.deepcopy(self.relu)
self.sig = nn.Sigmoid()
self.tanh = nn.Tanh()
def forward(self, x):
x = self.conv1(x)
x = self.relu1_1(x)
x = self.conv2(x)
x = self.relu2_1(x)
x = self.conv3(x)
if self.activations:
x = self.sig(x)
else:
x = self.relu3_1(x)
x = x.view(x.size(0), -1)
x = self.embedding(x)
extra_out = x
clustering_out = self.clustering(x)
x = self.deembedding(x)
x = self.relu1_2(x)
x = x.view(x.size(0), self.filters[2], ((self.input_shape[0]//2//2-1) // 2), ((self.input_shape[0]//2//2-1) // 2))
x = self.deconv3(x)
x = self.relu2_2(x)
x = self.deconv2(x)
x = self.relu3_2(x)
x = self.deconv1(x)
if self.activations:
x = self.tanh(x)
return x, clustering_out, extra_out