-
Notifications
You must be signed in to change notification settings - Fork 2
/
iqnn_scratch.py
370 lines (287 loc) · 12.5 KB
/
iqnn_scratch.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project :Quantum-Neural-Network
@File :iqnn_scratch.py
@Author :JackHCC
@Date :2022/6/20 10:54
@Desc :Ab initio implementation of classical neural networks and quantum heuristic compressed neural networks
'''
import cv2
import numpy as np
from tqdm import tqdm
from utils import get_runtime, f, f_grad, arg, arg_grad, sigmoid, sigmoid_grad
from skimage.metrics import mean_squared_error as compare_mse
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
from skimage.metrics import structural_similarity as compare_ssim
np.random.seed(42)
def block_divide(img, K):
W, H = img.shape
assert W % K == 0 and H % K == 0
r, c = W // K, H // K
P = np.zeros((K * K, r * c))
for i in range(r):
for j in range(c):
P[:, i * c + j] = img[K * i: K * (i + 1), K * j: K * (j + 1)].reshape((K * K,))
return P
def block_recon(array, K):
W, H = array.shape
m = np.sqrt(H)
R = int(m)
C = int(m)
I = np.zeros((R * K, C * K))
k = 0
for i in range(R):
for j in range(C):
t = array[:, k].reshape((K, K))
I[i * K: (i + 1) * K, j * K: (j + 1) * K] = t
k += 1
return I
def read_img(path):
raw_img = cv2.imread(path)
if len(raw_img.shape) > 2:
raw_img = cv2.cvtColor(raw_img, cv2.COLOR_BGR2YCrCb)
raw_img = raw_img[:, :, 0]
return raw_img
# 经典压缩神经网络从头实现
class ClassicalMLPCompress:
def __init__(self, image, init_obj=None, K=4, hidden_num=8, epochs=500, threshold=0.0005, lr=0.5):
self.image = image
self.init_obj = init_obj
self.K = K
self.hidden_num = hidden_num
self.epochs = epochs
self.threshold = threshold
self.lr = lr
self.train_x, self.train_y, self.test_x, self.test_y = self.build_dataset()
self.B1, self.B3, self.sample_size = None, None, None
# parameter
self.W_21 = None
self.W_32 = None
self.b_2 = None
self.b_3 = None
self.E = [] # 迭代的loss记录
self.iter_num = epochs
self.output = None
self.init_param()
self.train()
self.inference()
def build_dataset(self):
train_x = block_divide(self.image, self.K) / 255
train_y = train_x
test_x = train_x
test_y = train_x
return train_x, train_y, test_x, test_y
def init_param(self):
if not self.init_obj:
self.B1, self.sample_size = self.train_x.shape
self.B3, _ = self.train_y.shape
self.W_21 = np.random.rand(self.hidden_num, self.B1)
self.b_2 = np.random.rand(self.hidden_num, 1)
self.W_32 = np.random.rand(self.B3, self.hidden_num)
self.b_3 = np.random.rand(self.B3, 1)
else:
self.W_21 = self.init_obj.W_21
self.b_2 = self.init_obj.b_2
self.W_32 = self.init_obj.W_32
self.b_3 = self.init_obj.b_3
@get_runtime
def train(self):
for i in tqdm(range(self.epochs)):
iter_error = 0
for j in range(self.sample_size):
input_data = self.train_x[:, j].reshape(self.B1, 1)
output_data = self.train_y[:, j].reshape(self.B1, 1)
# FP
z2 = self.W_21 @ input_data + self.b_2
a2 = 1 / (1 + np.exp(-z2))
z3 = self.W_32 @ a2 + self.b_3
a3 = 1 / (1 + np.exp(-z3))
# BP
e = (a3 - output_data)
delta = (a3 * (1 - a3)) * e
e = self.W_32.T @ delta
self.W_32 = self.W_32 - self.lr * delta @ a2.T
self.b_3 = self.b_3 - self.lr * delta
delta = (a2 * (1 - a2)) * e
self.W_21 = self.W_21 - self.lr * delta @ input_data.T
self.b_2 = self.b_2 - self.lr * delta
# error
iter_error = iter_error + 0.5 * np.sum(np.power(a3 - output_data, 2))
iter_error = iter_error / (self.sample_size * self.B3)
self.E.append(iter_error)
if iter_error < self.threshold:
print("[BREAK] 迭代次数:", i, " error:", iter_error)
self.iter_num = i
break
def inference(self):
self.output = np.zeros(self.train_x.shape)
for i in range(self.sample_size):
input_data = self.train_x[:, i].reshape(self.B1, 1)
z2 = self.W_21 @ input_data + self.b_2
a2 = 1 / (1 + np.exp(-z2))
z3 = self.W_32 @ a2 + self.b_3
a3 = 1 / (1 + np.exp(-z3))
self.output[:, i] = np.squeeze(a3)
self.output = (block_recon(self.output, self.K) * 255).astype('uint8')
# 量子启发式压缩神经网络从头实现
class HeuristicQuantumMLPCompress:
def __init__(self, image, init_obj=None, K=4, hidden_num=4, epochs=500, threshold=0.0005, lr=0.5, init_num=0.2):
self.image = image
self.init_obj = init_obj
self.K = K
self.hidden_num = hidden_num
self.epochs = epochs
self.threshold = threshold
self.lr = lr
self.init_num = init_num
self.train_x, self.train_y, self.test_x, self.test_y = self.build_dataset()
self.B1, self.B3, self.sample_size = None, None, None
# parameter
self.theta_kl = None
self.lambda_k = None
self.delta_k = None
self.theta_nk = None
self.lambda_n = None
self.delta_n = None
self.E = [] # 迭代的loss记录
self.iter_num = epochs
self.output = None
self.init_param()
self.train()
self.inference()
def build_dataset(self):
train_x = block_divide(self.image, self.K) / 255
train_y = train_x
test_x = train_x
test_y = train_x
return train_x, train_y, test_x, test_y
def init_param(self):
if self.init_obj:
self.theta_kl = self.init_obj.theta1
self.lambda_k = self.init_obj.lambda1
self.delta_k = self.init_obj.delta1
self.theta_nk = self.init_obj.theta2
self.lambda_n = self.init_obj.lambda2
self.delta_n = self.init_obj.delta2
else:
self.B1, self.sample_size = self.train_x.shape
self.B3, _ = self.train_y.shape
self.theta_kl = np.random.rand(self.hidden_num, self.B1) * self.init_num
self.lambda_k = np.random.rand(self.hidden_num, 1) * self.init_num
self.delta_k = np.random.rand(self.hidden_num, 1)
self.theta_nk = np.random.rand(self.B3, self.hidden_num) * self.init_num
self.lambda_n = np.random.rand(self.B3, 1) * self.init_num
self.delta_n = np.random.rand(self.B3, 1)
@get_runtime
def train(self):
for i in tqdm(range(self.epochs)):
iter_error = 0
for j in range(self.sample_size):
input_data = self.train_x[:, j].reshape(self.B1, 1)
output_data = self.train_y[:, j].reshape(self.B1, 1)
# FP
y_l = input_data * np.pi / 2
IO = f(y_l)
u_k = f(self.theta_kl) @ IO - f(self.lambda_k)
y_k = (np.pi / 2) * sigmoid(self.delta_k) - arg(u_k)
HO = f(y_k)
u_n = f(self.theta_nk) @ HO - f(self.lambda_n)
y_n = (np.pi / 2) * sigmoid(self.delta_n) - arg(u_n)
OP = f(y_n)
output = np.imag(OP) * np.imag(OP)
# BP
# hidden - output layer
d_n = -1 * (output_data - output) * np.sin(2 * y_n) * arg_grad(u_n)
rep_y_k = np.repeat(y_k.T, self.B3, axis=0)
rep_u_n_real = np.repeat(np.real(u_n), self.hidden_num, axis=1)
rep_u_n_image = np.repeat(np.imag(u_n), self.hidden_num, axis=1)
rep_d_n = np.repeat(d_n, self.hidden_num, axis=1)
m_n = (np.cos(self.theta_nk + rep_y_k) * rep_u_n_real + np.sin(
self.theta_nk + rep_y_k) * rep_u_n_image) / np.power(rep_u_n_real, 2)
s_n = (np.cos(self.lambda_n) * np.real(u_n) + np.sin(self.lambda_n) * np.imag(u_n)) / np.power(
np.real(u_n), 2)
# E 对 delta_n 求梯度
delta_delta2 = - (np.pi / 2) * (output_data - output) * np.sin(2 * y_n) * sigmoid_grad(self.delta_n)
# 对 theta_n 求梯度
delta_theta2 = -1 * rep_d_n * m_n
# 对 lambda_n 求梯度
delta_lambda2 = d_n * s_n
# hidden layer - input layer
e_k = np.sum(-1 * rep_d_n * m_n, axis=0).T.reshape(self.hidden_num, 1)
d_k = e_k * arg_grad(u_k)
rep_y_l = np.repeat(y_l.T, self.hidden_num, axis=0)
rep_u_k_real = np.repeat(np.real(u_k), self.B1, axis=1)
rep_u_k_image = np.repeat(np.imag(u_k), self.B1, axis=1)
rep_d_k = np.repeat(d_k, self.B1, axis=1)
m_k = (np.cos(self.theta_kl + rep_y_l) * rep_u_k_real + np.sin(
self.theta_kl + rep_y_l) * rep_u_k_image) / np.power(rep_u_k_real, 2)
s_k = (np.cos(self.lambda_k) * np.real(u_k) + np.sin(self.lambda_k) * np.imag(u_k)) / np.power(
np.real(u_k), 2)
# 对 delta_k 求梯度
delta_delta1 = (np.pi / 2) * e_k * sigmoid_grad(self.delta_k)
# 对 theta_k 求梯度
delta_theta1 = -1 * rep_d_k * m_k
# 对 lambda_k 求梯度
delta_lambda1 = d_k * s_k
# update parameter
self.delta_n = self.delta_n - self.lr * delta_delta2
self.theta_nk = self.theta_nk - self.lr * delta_theta2
self.lambda_n = self.lambda_n - self.lr * delta_lambda2
self.delta_k = self.delta_k - self.lr * delta_delta1
self.theta_kl = self.theta_kl - self.lr * delta_theta1
self.lambda_k = self.lambda_k - self.lr * delta_lambda1
# all output error
iter_error = iter_error + 0.5 * np.sum(np.power(output_data - output, 2))
iter_error = iter_error / (self.sample_size * self.B3)
self.E.append(iter_error)
if iter_error < self.threshold:
print("[BREAK] 迭代次数:", i, " error:", iter_error)
self.iter_num = i
break
def inference(self):
self.output = np.zeros(self.train_x.shape)
for i in range(self.sample_size):
input_data = self.train_x[:, i].reshape(self.B1, 1)
y_l = input_data * np.pi / 2
IO = f(y_l)
u_k = f(self.theta_kl) @ IO - f(self.lambda_k)
y_k = (np.pi / 2) * sigmoid(self.delta_k) - arg(u_k)
HO = f(y_k)
u_n = f(self.theta_nk) @ HO - f(self.lambda_n)
y_n = (np.pi / 2) * sigmoid(self.delta_n) - arg(u_n)
OP = f(y_n)
Yi = np.imag(OP) * np.imag(OP)
self.output[:, i] = np.squeeze(Yi)
self.output = (block_recon(self.output, self.K) * 255).astype('uint8')
if __name__ == "__main__":
# img_path = "./data/Set5/GTmod12/butterfly.png"
# img_path = "./data/Set5/GTmod12/bird.png"
img_path = "./data/Set5/Set5_size_512/butterfly.bmp"
# img_path = "./data/lena.bmp"
raw_img = read_img(img_path)
H, W = raw_img.shape
cv2.imwrite("./result/gray_bird.png", raw_img)
# print(raw_img)
print(raw_img.shape)
K = 4
hide_num = 8
block_img = block_divide(raw_img, K)
print(block_img.shape)
img = block_recon(block_img, K)
print(img.shape)
MLP = ClassicalMLPCompress(raw_img, K=K, hidden_num=hide_num, epochs=1000)
out_img = MLP.output
# QuantumMLP = HeuristicQuantumMLPCompress(raw_img, K=K, hidden_num=hide_num, epochs=1000)
# out_img = QuantumMLP.output
p = compare_psnr(raw_img, out_img)
# 对于多通道图像(RGB、HSV等)关键词multichannel要设置为True
s = compare_ssim(raw_img, out_img, multichannel=False)
m = compare_mse(raw_img, out_img)
cr = H * W / (H * W * hide_num / (K * K) + hide_num * K * K)
print('CR:{},PSNR:{},SSIM:{},MSE:{}'.format(cr, p, s, m))
cv2.imwrite("./result/bird-" + str(K) + "-" + str(hide_num) + ".png", out_img)
cv2.namedWindow("Output", cv2.WINDOW_FREERATIO)
cv2.imshow("Output", out_img)
cv2.waitKey(0)
cv2.destroyAllWindows()