-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNet.py
179 lines (161 loc) · 9.2 KB
/
Net.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
import torch.optim as optim
from metric import *
import math
import timeit
class Conv2dSamePad(nn.Module):
"""
Implement Tensorflow's 'SAME' padding mode in Conv2d.
When an odd number, say `m`, of pixels are need to pad, Tensorflow will pad one more column at right or one more
row at bottom. But Pytorch will pad `m+1` pixels, i.e., Pytorch always pads in both sides.
So we can pad the tensor in the way of Tensorflow before call the Conv2d module.
"""
def __init__(self, kernel_size, stride):
super(Conv2dSamePad, self).__init__()
self.kernel_size = kernel_size if type(kernel_size) in [list, tuple] else [kernel_size, kernel_size]
self.stride = stride if type(stride) in [list, tuple] else [stride, stride]
def forward(self, x):
# print('shape', x.shape)
# in_height = x.size(2)
# in_width = x.size(3)
in_height = x.shape[2]
in_width = x.shape[3]
out_height = math.ceil(float(in_height) / float(self.stride[0]))
out_width = math.ceil(float(in_width) / float(self.stride[1]))
pad_along_height = ((out_height - 1) * self.stride[0] + self.kernel_size[0] - in_height)
pad_along_width = ((out_width - 1) * self.stride[1] + self.kernel_size[1] - in_width)
pad_top = math.floor(pad_along_height / 2)
pad_left = math.floor(pad_along_width / 2)
pad_bottom = pad_along_height - pad_top
pad_right = pad_along_width - pad_left
return F.pad(x, [pad_left, pad_right, pad_top, pad_bottom], 'constant', 0)
class ConvTranspose2dSamePad(nn.Module):
"""
This module implements the "SAME" padding mode for ConvTranspose2d as in Tensorflow.
A tensor with width w_in, feed it to ConvTranspose2d(ci, co, kernel, stride), the width of output tensor T_nopad:
w_nopad = (w_in - 1) * stride + kernel
If we use padding, i.e., ConvTranspose2d(ci, co, kernel, stride, padding, output_padding), the width of T_pad:
w_pad = (w_in - 1) * stride + kernel - (2*padding - output_padding) = w_nopad - (2*padding - output_padding)
Yes, in ConvTranspose2d, more padding, the resulting tensor is smaller, i.e., the padding is actually deleting row/col.
If `pad`=(2*padding - output_padding) is odd, Pytorch deletes more columns in the left, i.e., the first ceil(pad/2) and
last `pad - ceil(pad/2)` columns of T_nopad are deleted to get T_pad.
In contrast, Tensorflow deletes more columns in the right, i.e., the first floor(pad/2) and last `pad - floor(pad/2)`
columns are deleted.
For the height, Pytorch deletes more rows at top, while Tensorflow at bottom.
In practice, we usually want `w_pad = w_in * stride`, i.e., the "SAME" padding mode in Tensorflow,
so the number of columns to delete:
pad = 2*padding - output_padding = kernel - stride
We can solve the above equation and get:
padding = ceil((kernel - stride)/2), and
output_padding = 2*padding - (kernel - stride) which is either 1 or 0.
But to get the same result with Tensorflow, we should delete values by ourselves instead of using padding and
output_padding in ConvTranspose2d.
To get there, we check the following conditions:
If pad = kernel - stride is even, we can directly set padding=pad/2 and output_padding=0 in ConvTranspose2d.
If pad = kernel - stride is odd, we can use ConvTranspose2d to get T_nopad, and then delete `pad` rows/columns by
ourselves; or we can use ConvTranspose2d to delete `pad - 1` by setting `padding=(pad - 1) / 2` and `ouput_padding=0`
and then delete the last row/column of the resulting tensor by ourselves.
Here we implement the former case.
This module should be called after the ConvTranspose2d module with shared kernel_size and stride values.
And this module can only output a tensor with shape `stride * size_input`.
A more flexible module can be found in `yaleb.py` which can output arbitrary size as specified.
"""
def __init__(self, kernel_size, stride):
super(ConvTranspose2dSamePad, self).__init__()
self.kernel_size = kernel_size if type(kernel_size) in [list, tuple] else [kernel_size, kernel_size]
self.stride = stride if type(stride) in [list, tuple] else [stride, stride]
def forward(self, x):
in_height = x.size(2)
in_width = x.size(3)
pad_height = self.kernel_size[0] - self.stride[0]
pad_width = self.kernel_size[1] - self.stride[1]
pad_top = pad_height // 2
pad_bottom = pad_height - pad_top
pad_left = pad_width // 2
pad_right = pad_width - pad_left
return x[:, :, pad_top:in_height - pad_bottom, pad_left: in_width - pad_right]
class ConvAE(nn.Module):
def __init__(self, channels, kernels):
"""
:param channels: a list containing all channels including the input image channel (1 for gray, 3 for RGB)
:param kernels: a list containing all kernel sizes, it should satisfy: len(kernels) = len(channels) - 1.
"""
super(ConvAE, self).__init__()
assert isinstance(channels, list) and isinstance(kernels, list)
self.encoder = nn.Sequential()
for i in range(1, len(channels)):
# Each layer will divide the size of feature map by 2
self.encoder.add_module('pad%d' % i, Conv2dSamePad(kernels[i - 1], 2))
self.encoder.add_module('conv%d' % i,
nn.Conv2d(channels[i - 1], channels[i], kernel_size=kernels[i - 1], stride=2))
self.encoder.add_module('relu%d' % i, nn.ReLU(True))
self.decoder = nn.Sequential()
channels = list(reversed(channels))
kernels = list(reversed(kernels))
for i in range(len(channels) - 1):
# Each layer will double the size of feature map
self.decoder.add_module('deconv%d' % (i + 1),
nn.ConvTranspose2d(channels[i], channels[i + 1], kernel_size=kernels[i], stride=2))
self.decoder.add_module('padd%d' % i, ConvTranspose2dSamePad(kernels[i], 2))
self.decoder.add_module('relud%d' % i, nn.ReLU(True))
def forward(self, x):
h = self.encoder(x)
y = self.decoder(h)
return y
class SDSC(nn.Module):
def __init__(self, channels, kernels, num_sample, kmeansNum):
super(SDSC, self).__init__()
self.n = num_sample
self.kmeansNum = kmeansNum
self.ae = ConvAE(channels, kernels)
self.self_expression = SelfExpression(self.n, self.kmeansNum)
self.get_m = get_m(self.n, self.kmeansNum)
def forward(self, x): # shape=[n, c, w, h]
z = self.ae.encoder(x)
zz = z
shape = z.shape
z = z.view(self.n, -1) # shape=[n, d]
m = self.get_m(z)
z_recon= self.self_expression(m) # shape=[n, d]
z_recon_reshape = z_recon.view(shape)
# x_recon = self.ae.decoder(z_recon_reshape) # shape=[n, c, w, h]
x_recon = self.ae.decoder(zz)
return x_recon, z, z_recon
def loss_fn(self, x, x_recon, z, z_recon, weight_coef, weight_selfExp, weight_cc):
loss_ae = F.mse_loss(x_recon, x, reduction='sum')
loss_coef = torch.sum(torch.pow(self.self_expression.Coefficient, 2)) + torch.sum(torch.pow(self.get_m.Chat, 2))
loss_cc = F.mse_loss(torch.eye(x.shape[0]).to('cuda'), torch.matmul(self.self_expression.Coefficient, self.get_m.Chat))
loss_selfExp = F.mse_loss(z_recon, z, reduction='sum')
loss = loss_ae + weight_coef * loss_coef + weight_selfExp * loss_selfExp + weight_cc*loss_cc
return loss
def train(model, # type: DSCNet
x, y, epochs, lr=1e-3, weight_coef=1.0, weight_selfExp=150, weight_cc=1.0, device='cuda',
alpha=0.04, dim_subspace=12, ro=8, show=1, SC_method=True ):
optimizer = optim.Adam(model.parameters(), lr=lr)
# print('model.parameters():', model.parameters())
# 判断一个对象是不是一个已和类型(这里为判断是不是张量)
if not isinstance(x, torch.Tensor):
x = torch.tensor(x, dtype=torch.float32, device=device)
x = x.to(device)
if isinstance(y, torch.Tensor):
y = y.to('cpu').numpy()
# np.unique该函数是去除数组中的重复数字,并进行排序之后输出.
K = len(np.unique(y))
nettime = 0
for epoch in range(epochs):
starttime = timeit.default_timer()
# land = model.self_expression.land
x_recon, z, z_recon = model(x)
loss = model.loss_fn(x, x_recon, z, z_recon, weight_coef=weight_coef, weight_selfExp=weight_selfExp, weight_cc=weight_cc)
# zero the parameter gradients(该部分可以理解为随机梯度下降)
optimizer.zero_grad()
loss.backward()
optimizer.step()
midtime = timeit.default_timer()
delta = midtime - starttime
nettime = nettime + delta
if epoch % show == 0 or epoch == epochs - 1:
C = model.self_expression.Coefficient.detach().to('cpu').numpy()
Chat = model.get_m.Chat.detach().to('cpu').numpy()
y_pred = spectral_clustering(SC_method, C, Chat, K, dim_subspace, alpha, ro)
print('Epoch %02d: loss=%.4f, acc=%.4f, nmi=%.4f, nettime = %.4f' %
(epoch, loss.item() / y_pred.shape[0], acc(y, y_pred), nmi(y, y_pred), nettime))