-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.py
195 lines (158 loc) · 7.08 KB
/
module.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
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
# @Author : Peizhao Li
# @Contact : [email protected]
import numpy as np
import torch
from torch import nn
from torch.nn import Parameter
from utils import init_weights
def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha * iter_num / max_iter)) - (high - low) + low)
def grl_hook(coeff):
def fun1(grad):
return -coeff * grad.clone()
return fun1
def adv_loss(features, ad_net, device='cpu'):
ad_out = ad_net(features)
batch_size = ad_out.size(0) // 2
dc_target = torch.from_numpy(np.array([[1]] * batch_size + [[0]] * batch_size)).float().to(device)
return nn.BCELoss()(ad_out, dc_target)
class Encoder(nn.Module):
def __init__(self):
super(Encoder, self).__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(16)
self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=2, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(32)
self.conv3 = nn.Conv2d(32, 32, kernel_size=3, stride=1, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(32)
self.conv4 = nn.Conv2d(32, 16, kernel_size=3, stride=2, padding=1, bias=False)
self.bn4 = nn.BatchNorm2d(16)
self.fc1 = nn.Linear(8 * 8 * 16, 512)
self.fc_bn1 = nn.BatchNorm1d(512)
self.fc21 = nn.Linear(512, 64)
self.fc22 = nn.Linear(512, 64)
self.relu = nn.ReLU()
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_uniform_(m.weight)
if isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 0.1)
if isinstance(m, nn.BatchNorm2d):
nn.init.normal_(m.weight, 1.0, 0.02)
def reparameterize(self, mu, logvar):
if self.training:
std = logvar.mul(0.5).exp_()
eps = std.data.new(std.size()).normal_()
return eps.mul(std).add_(mu)
else:
return mu
def forward(self, x):
conv1 = self.relu(self.bn1(self.conv1(x)))
conv2 = self.relu(self.bn2(self.conv2(conv1)))
conv3 = self.relu(self.bn3(self.conv3(conv2)))
conv4 = self.relu(self.bn4(self.conv4(conv3))).view(-1, 8 * 8 * 16)
fc1 = self.relu(self.fc_bn1(self.fc1(conv4)))
mu, logvar = self.fc21(fc1), self.fc22(fc1)
z = self.reparameterize(mu, logvar)
return z, mu, logvar
def get_parameters(self):
return [{"params": self.parameters(), "lr_mult": 1}]
class ClusterAssignment(nn.Module):
def __init__(self, cluster_number, embedding_dimension, alpha, cluster_centers):
"""
Module to handle the soft assignment, for a description see in 3.1.1. in Xie/Girshick/Farhadi,
where the Student's t-distribution is used measure similarity between feature vector and each
cluster centroid.
:param cluster_number: number of clusters
:param embedding_dimension: embedding dimension of feature vectors
:param alpha: parameter representing the degrees of freedom in the t-distribution, default 1.0
:param cluster_centers: clusters centers to initialise, if None then use Xavier uniform
"""
super(ClusterAssignment, self).__init__()
self.embedding_dimension = embedding_dimension
self.cluster_number = cluster_number
self.alpha = alpha
if cluster_centers is None:
initial_cluster_centers = torch.zeros(
self.cluster_number,
self.embedding_dimension,
dtype=torch.float
)
nn.init.xavier_uniform_(initial_cluster_centers)
else:
initial_cluster_centers = cluster_centers
self.cluster_centers = Parameter(initial_cluster_centers)
def forward(self, batch):
"""
Compute the soft assignment for a batch of feature vectors, returning a batch of assignments
for each cluster.
:param batch: FloatTensor of [batch size, embedding dimension]
:return: FloatTensor [batch size, number of clusters]
"""
norm_squared = torch.sum((batch.unsqueeze(1) - self.cluster_centers) ** 2, 2)
numerator = 1.0 / (1.0 + (norm_squared / self.alpha))
# power = float(self.alpha + 1) / 2
# numerator = numerator ** power
return numerator / torch.sum(numerator, dim=1, keepdim=True)
class DFC(nn.Module):
def __init__(self, cluster_number, hidden_dimension, alpha=1):
"""
Module which holds all the moving parts of the DEC algorithm, as described in
Xie/Girshick/Farhadi; this includes the AutoEncoder stage and the ClusterAssignment stage.
:param cluster_number: number of clusters
:param embedding_dimension: embedding dimension, entropy_input to the encoder
:param hidden_dimension: hidden dimension, output of the encoder
:param encoder: encoder to use
:param alpha: parameter representing the degrees of freedom in the t-distribution, default 1.0
"""
super(DFC, self).__init__()
self.hidden_dimension = hidden_dimension
self.cluster_number = cluster_number
self.alpha = alpha
self.assignment = ClusterAssignment(cluster_number, self.hidden_dimension, alpha, cluster_centers=None)
def forward(self, batch):
"""
Compute the cluster assignment using the ClusterAssignment after running the batch
through the encoder part of the associated AutoEncoder module.
:param batch: [batch size, embedding dimension] FloatTensor
:return: [batch size, number of clusters] FloatTensor
"""
return self.assignment(batch)
def get_parameters(self):
return [{"params": self.parameters(), "lr_mult": 1}]
class AdversarialNetwork(nn.Module):
def __init__(self, in_feature, hidden_size, max_iter, lr_mult):
super(AdversarialNetwork, self).__init__()
self.ad_layer1 = nn.Linear(in_feature, hidden_size)
self.ad_layer2 = nn.Linear(hidden_size, hidden_size)
self.ad_layer3 = nn.Linear(hidden_size, 1)
self.relu1 = nn.ReLU()
self.relu2 = nn.ReLU()
self.dropout1 = nn.Dropout(0.5)
self.dropout2 = nn.Dropout(0.5)
self.sigmoid = nn.Sigmoid()
self.apply(init_weights)
self.iter_num = 0
self.alpha = 10
self.low = 0.0
self.high = 1.0
self.max_iter = float(max_iter)
self.lr_mult = lr_mult
def forward(self, x):
if self.training:
self.iter_num += 1
coeff = calc_coeff(self.iter_num, self.high, self.low, self.alpha, self.max_iter)
x = x * 1.0
x.register_hook(grl_hook(coeff))
x = self.ad_layer1(x)
x = self.relu1(x)
x = self.dropout1(x)
x = self.ad_layer2(x)
x = self.relu2(x)
x = self.dropout2(x)
y = self.ad_layer3(x)
y = self.sigmoid(y)
return y
def get_parameters(self):
return [{"params": self.parameters(), "lr_mult": self.lr_mult}]