-
Notifications
You must be signed in to change notification settings - Fork 29
/
tester_hybridmodel.py
282 lines (218 loc) · 11.8 KB
/
tester_hybridmodel.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
import tensorflow as tf
import arithmeticcoding
import numpy as np
import os
import math
from utils import printProgressBar
from PIL import Image
class Tester_hybridmodel(object):
def __init__(self, model_dir, model_type, quality_level):
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
self.model_dir = model_dir
self.M1 = 192
if model_type==0: #MSE optimized
if quality_level == 6:
self.M2 = 192
elif quality_level == 7:
self.M2 = 228
elif quality_level in [8,9]:
self.M2 = 408
elif model_type==1: #MS-SSIM optimized
if quality_level in [6,7]:
self.M2 = 192
elif quality_level in [8,9]:
self.M2 = 408
self.M = self.M1 + self.M2
self.build_model()
def load_graph(self, frozen_graph_filename):
with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name="prefix")
return graph
def build_model(self):
frozen_graph_filename = self.model_dir + "/" + "saved_model.pb"
graph = self.load_graph(frozen_graph_filename)
# for verifying
# for op in graph.get_operations():
# print(op.name)
self.recon_image = graph.get_tensor_by_name('prefix/clip_by_value:0')
self.y_hat = graph.get_tensor_by_name('prefix/transpose_3:0')
self.input_x = graph.get_tensor_by_name('prefix/Placeholder_2:0')
self.h_s_out = graph.get_tensor_by_name('prefix/transpose_8:0')
self.z_hat = graph.get_tensor_by_name('prefix/Round_1:0')
self.sigma_z = graph.get_tensor_by_name('prefix/H/Abs:0')
self.pred_mean = graph.get_tensor_by_name('prefix/H/P_15/strided_slice:0')
self.pred_sigma = graph.get_tensor_by_name('prefix/H/P_15/Exp:0')
self.concatenated_c_i = graph.get_tensor_by_name('prefix/H/concat_45:0')
config = tf.ConfigProto(
device_count={'GPU': 0}
)
self.sess = tf.Session(graph=graph, config=config)
def extractor_prime(self, padded_c_prime, h_idx, w_idx): # with TOP N dimensions
return padded_c_prime[:, :, h_idx:h_idx + 4, w_idx:w_idx + 4]
def extractor_doubleprime(self, padded_y_hat, h_idx, w_idx): # with TOP N dimensions
# Masking has no effect on decoding, because unknown variables are already set to zeros.
# Therefore, the masking can be skipped in the case of decoding.
# We just leave it here just to maintain a simple structure of code.
mask = [[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 0, 0]]
return np.multiply(padded_y_hat[:, :, h_idx:h_idx + 4, w_idx:w_idx + 4], mask)
def encode(self, model_type, input_path, compressed_file_path, quality_level): # with TOP N dimensions
img = Image.open(input_path)
w, h = img.size
fileobj = open(compressed_file_path, mode='wb')
buf = quality_level << 1
buf = buf + model_type
arr = np.array([0], dtype=np.uint8)
arr[0] = buf
arr.tofile(fileobj)
arr = np.array([w, h], dtype=np.uint16)
arr.tofile(fileobj)
fileobj.close()
new_w = int(math.ceil(w / 64) * 64)
new_h = int(math.ceil(h / 64) * 64)
pad_w = new_w - w
pad_h = new_h - h
input_x = np.asarray(img)
input_x = np.pad(input_x, ((pad_h,0), (pad_w,0), (0,0)), mode='reflect')
input_x = input_x.reshape(1, new_h, new_w, 3)
input_x = input_x.transpose([0, 3, 1, 2])
h_s_out, y_hat, z_hat, sigma_z = self.sess.run([self.h_s_out, self.y_hat, self.z_hat, self.sigma_z],
feed_dict={self.input_x: input_x}) # NCHW
############### encode z ####################################
bitout = arithmeticcoding.BitOutputStream(open(compressed_file_path, "ab+"))
enc = arithmeticcoding.ArithmeticEncoder(bitout)
printProgressBar(0, z_hat.shape[1], prefix='Encoding z_hat:', suffix='Complete', length=50)
for ch_idx in range(z_hat.shape[1]):
printProgressBar(ch_idx + 1, z_hat.shape[1], prefix='Encoding z_hat:', suffix='Complete', length=50)
mu_val = 255
sigma_val = sigma_z[ch_idx]
# exp_sigma_val = np.exp(sigma_val)
freq = arithmeticcoding.ModelFrequencyTable(mu_val, sigma_val)
for h_idx in range(z_hat.shape[2]):
for w_idx in range(z_hat.shape[3]):
symbol = np.rint(z_hat[0, ch_idx, h_idx, w_idx] + 255)
if symbol < 0 or symbol > 511:
print("symbol range error: " + str(symbol))
# print(symbol)
enc.write(freq, symbol)
# enc.write(freq, 512)
# enc.finish()
# bitout.close()
############### encode y ####################################
padded_y1_hat = np.pad(y_hat[:, :self.M1, :, :], ((0, 0), (0, 0), (3, 0), (2, 1)), 'constant',
constant_values=((0, 0), (0, 0), (0, 0), (0, 0)))
# bitout = arithmeticcoding.BitOutputStream(open(enc_outputfile, "wb"))
# enc = arithmeticcoding.ArithmeticEncoder(bitout)
c_prime = h_s_out[:, :self.M1, :, :]
sigma2 = h_s_out[:, self.M1:, :, :]
padded_c_prime = np.pad(c_prime, ((0, 0), (0, 0), (3, 0), (2, 1)), 'constant',
constant_values=((0, 0), (0, 0), (0, 0), (0, 0)))
printProgressBar(0, y_hat.shape[2], prefix='Encoding y_hat:', suffix='Complete', length=50)
for h_idx in range(y_hat.shape[2]):
printProgressBar(h_idx + 1, y_hat.shape[2], prefix='Encoding y_hat:', suffix='Complete', length=50)
for w_idx in range(y_hat.shape[3]):
c_prime_i = self.extractor_prime(padded_c_prime, h_idx, w_idx)
c_doubleprime_i = self.extractor_doubleprime(padded_y1_hat, h_idx, w_idx)
concatenated_c_i = np.concatenate([c_doubleprime_i, c_prime_i], axis=1)
pred_mean, pred_sigma = self.sess.run([self.pred_mean, self.pred_sigma],
feed_dict={self.concatenated_c_i: concatenated_c_i})
zero_means = np.zeros(
[pred_mean.shape[0], self.M2, pred_mean.shape[2],
pred_mean.shape[3]])
concat_pred_mean = np.concatenate([pred_mean, zero_means], axis=1)
concat_pred_sigma = np.concatenate([pred_sigma, sigma2[:, :, h_idx:h_idx + 1, w_idx:w_idx + 1]], axis=1)
for ch_idx in range(self.M):
mu_val = concat_pred_mean[0, ch_idx, 0, 0] + 255
sigma_val = concat_pred_sigma[0, ch_idx, 0, 0]
# exp_sigma_val = np.exp(sigma_val)
freq = arithmeticcoding.ModelFrequencyTable(mu_val, sigma_val)
symbol = np.rint(y_hat[0, ch_idx, h_idx, w_idx] + 255)
if symbol < 0 or symbol > 511:
print("symbol range error: " + str(symbol))
enc.write(freq, symbol)
enc.write(freq, 512)
enc.finish()
bitout.close()
return compressed_file_path
def decode(self, compressed_file, recon_path): # with TOP N dimensions
fileobj = open(compressed_file, mode='rb')
fileobj.read(1) #dummy
buf = fileobj.read(4)
arr = np.frombuffer(buf, dtype=np.uint16)
w = int(arr[0])
h = int(arr[1])
padded_w = int(math.ceil(w / 64) * 64)
padded_h = int(math.ceil(h / 64) * 64)
y_hat, z_hat, sigma_z = self.sess.run([self.y_hat, self.z_hat, self.sigma_z],
feed_dict={
self.input_x: np.zeros((1, 3, padded_h, padded_w))}) # NCHW
padded_y1_hat = np.pad(y_hat[:, :self.M1, :, :], ((0, 0), (0, 0), (3, 0), (2, 1)), 'constant',
constant_values=((0, 0), (0, 0), (0, 0), (0, 0)))
############### decode zhat ####################################
bitin = arithmeticcoding.BitInputStream(fileobj)
dec = arithmeticcoding.ArithmeticDecoder(bitin)
printProgressBar(0, z_hat.shape[1], prefix='Decoding z_hat:', suffix='Complete', length=50)
for ch_idx in range(z_hat.shape[1]):
printProgressBar(ch_idx + 1, z_hat.shape[1], prefix='Decoding z_hat:', suffix='Complete', length=50)
mu_val = 255
sigma_val = sigma_z[ch_idx]
# exp_sigma_val = np.exp(sigma_val)
freq = arithmeticcoding.ModelFrequencyTable(mu_val, sigma_val)
for h_idx in range(z_hat.shape[2]):
for w_idx in range(z_hat.shape[3]):
symbol = dec.read(freq)
if symbol == 512: # EOF symbol
print("EOF symbol")
break
z_hat[:, ch_idx, h_idx, w_idx] = symbol - 255
# bitin.close()
##################
#################################################
# Entropy decoding y
# padded_z = np.zeros_like(padded_z, dtype = np.float32)
h_s_out = self.sess.run(self.h_s_out, feed_dict={self.z_hat: z_hat})
c_prime = h_s_out[:, :self.M1, :, :]
sigma2 = h_s_out[:, self.M1:, :, :]
padded_c_prime = np.pad(c_prime, ((0, 0), (0, 0), (3, 0), (2, 1)), 'constant',
constant_values=((0, 0), (0, 0), (0, 0), (0, 0)))
padded_y1_hat[:, :, :, :] = 0.0
y_hat[:, :, :, :] = 0.0
# bitin = arithmeticcoding.BitInputStream(open(dec_inputfile, "rb"))
# dec = arithmeticcoding.ArithmeticDecoder(bitin)
printProgressBar(0, y_hat.shape[2], prefix='Decoding y_hat:', suffix='Complete', length=50)
for h_idx in range(y_hat.shape[2]):
printProgressBar(h_idx + 1, y_hat.shape[2], prefix='Decoding y_hat:', suffix='Complete', length=50)
for w_idx in range(y_hat.shape[3]):
c_prime_i = self.extractor_prime(padded_c_prime, h_idx, w_idx)
c_doubleprime_i = self.extractor_doubleprime(padded_y1_hat, h_idx, w_idx)
concatenated_c_i = np.concatenate([c_doubleprime_i, c_prime_i], axis=1)
pred_mean, pred_sigma = self.sess.run([self.pred_mean, self.pred_sigma],
feed_dict={self.concatenated_c_i: concatenated_c_i})
zero_means = np.zeros(
[pred_mean.shape[0], self.M2, pred_mean.shape[2],
pred_mean.shape[3]])
concat_pred_mean = np.concatenate([pred_mean, zero_means], axis=1)
concat_pred_sigma = np.concatenate([pred_sigma, sigma2[:, :, h_idx:h_idx + 1, w_idx:w_idx + 1]], axis=1)
for ch_idx in range(self.M):
mu_val = concat_pred_mean[0, ch_idx, 0, 0] + 255
sigma_val = concat_pred_sigma[0, ch_idx, 0, 0]
freq = arithmeticcoding.ModelFrequencyTable(mu_val, sigma_val)
symbol = dec.read(freq)
if symbol == 512: # EOF symbol
print("EOF symbol")
break
if ch_idx < self.M1:
padded_y1_hat[:, ch_idx, h_idx + 3, w_idx + 2] = symbol - 255
y_hat[:, ch_idx, h_idx, w_idx] = symbol - 255
bitin.close()
#################################################
recon = self.sess.run(self.recon_image, {self.y_hat: y_hat})
recon = recon[0, -h:, -w:, :]
im = Image.fromarray(recon.astype(np.uint8))
im.save(recon_path)
return