-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhelper.py
188 lines (132 loc) · 6.28 KB
/
helper.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
import numpy as np
import tensorflow as tf
import os
from scipy import misc
from ms_ssim_np import MultiScaleSSIM
import arithmeticcoding
def configure(args):
if args.l == 256:
I_level = 37
elif args.l == 512:
I_level = 32
elif args.l == 1024:
I_level = 27
elif args.l == 2048:
I_level = 22
elif args.l == 8:
I_level = 3
elif args.l == 16:
I_level = 3
elif args.l == 32:
I_level = 5
elif args.l == 64:
I_level = 7
path = args.path + '/'
path_com = args.path + '_' + args.mode + '_' + str(args.l) + '/frames/'
path_bin = args.path + '_' + args.mode + '_' + str(args.l) + '/bitstreams/'
path_lat = args.path + '_' + args.mode + '_' + str(args.l) + '/latents/'
os.makedirs(path_com, exist_ok=True)
os.makedirs(path_bin, exist_ok=True)
os.makedirs(path_lat, exist_ok=True)
F1 = misc.imread(path + 'f001.png')
Height = np.size(F1, 0)
Width = np.size(F1, 1)
batch_size = 1
Channel = 3
if (Height % 16 != 0) or (Width % 16 != 0):
raise ValueError('Height and Width must be a mutiple of 16.')
activation = tf.nn.relu
GOP_size = args.f_P + args.b_P + 1
GOP_num = int(np.floor((args.frame - 1)/GOP_size))
return I_level, Height, Width, batch_size, \
Channel, activation, GOP_size, GOP_num, \
path, path_com, path_bin, path_lat
def configure_decoder(args):
path = args.path + '/'
path_com = args.path + '_' + args.mode + '_' + str(args.l) + '/frames_dec/'
path_bin = args.path + '_' + args.mode + '_' + str(args.l) + '/bitstreams/'
path_lat = args.path + '_' + args.mode + '_' + str(args.l) + '/latents_dec/'
os.makedirs(path_com, exist_ok=True)
os.makedirs(path_lat, exist_ok=True)
activation = tf.nn.relu
GOP_size = args.f_P + args.b_P + 1
GOP_num = int(np.floor((args.frame - 1)/GOP_size))
return activation, GOP_size, GOP_num, \
path, path_com, path_bin, path_lat
def encode_I(args, frame_index, I_level, path, path_com, path_bin):
if args.mode == 'PSNR':
os.system('bpgenc -f 444 -m 9 ' + path + 'f' + str(frame_index).zfill(3) + '.png '
'-o ' + path_bin + 'f' + str(frame_index).zfill(3) + '.bin -q ' + str(I_level))
os.system('bpgdec ' + path_bin + 'f' + str(frame_index).zfill(3) + '.bin '
'-o ' + path_com + 'f' + str(frame_index).zfill(3) + '.png')
elif args.mode == 'MS-SSIM':
os.system(args.python_path + ' ' + args.CA_model_path + '/encode.py --model_type 1 '
'--input_path ' + path + 'f' + str(frame_index).zfill(3) + '.png' +
' --compressed_file_path ' + path_bin + 'f' + str(frame_index).zfill(3) + '.bin'
+ ' --quality_level ' + str(I_level))
os.system(args.python_path + ' ' + args.CA_model_path + '/decode.py --compressed_file_path '
+ path_bin + 'f' + str(frame_index).zfill(3) + '.bin'
+ ' --recon_path ' + path_com + 'f' + str(frame_index).zfill(3) + '.png')
# bits = os.path.getsize(path_bin + str(frame_index).zfill(3) + '.bin')
# bits = bits * 8
F0_com = misc.imread(path_com + 'f' + str(frame_index).zfill(3) + '.png')
F0_raw = misc.imread(path + 'f' + str(frame_index).zfill(3) + '.png')
F0_com = np.expand_dims(F0_com, axis=0)
F0_raw = np.expand_dims(F0_raw, axis=0)
if args.metric == 'PSNR':
mse = np.mean(np.power(np.subtract(F0_com / 255.0, F0_raw / 255.0), 2.0))
quality = 10 * np.log10(1.0 / mse)
elif args.metric == 'MS-SSIM':
quality = MultiScaleSSIM(F0_com, F0_raw, max_val=255)
print('Frame', frame_index, args.metric + ' =', quality)
return quality
def decode_I(args, frame_index, path_com, path_bin):
if args.mode == 'PSNR':
os.system('bpgdec ' + path_bin + 'f' + str(frame_index).zfill(3) + '.bin '
'-o ' + path_com + 'f' + str(frame_index).zfill(3) + '.png')
elif args.mode == 'MS-SSIM':
os.system(args.python_path + ' ' + args.CA_model_path + '/decode.py --compressed_file_path '
+ path_bin + 'f' + str(frame_index).zfill(3) + '.bin'
+ ' --recon_path ' + path_com + 'f' + str(frame_index).zfill(3) + '.png')
print('Decoded I-frame', frame_index)
def entropy_coding(frame_index, lat, path_bin, latent, sigma, mu):
if lat == 'mv':
bias = 50
else:
bias = 100
bin_name = 'f' + str(frame_index).zfill(3) + '_' + lat + '.bin'
bitout = arithmeticcoding.BitOutputStream(open(path_bin + bin_name, "wb"))
enc = arithmeticcoding.ArithmeticEncoder(32, bitout)
for h in range(latent.shape[1]):
for w in range(latent.shape[2]):
for ch in range(latent.shape[3]):
mu_val = mu[0, h, w, ch] + bias
sigma_val = sigma[0, h, w, ch]
symbol = latent[0, h, w, ch] + bias
freq = arithmeticcoding.logFrequencyTable_exp(mu_val, sigma_val, np.int(bias * 2 + 1))
enc.write(freq, symbol)
enc.finish()
bitout.close()
bits_value = os.path.getsize(path_bin + bin_name) * 8
return bits_value
def entropy_decoding(frame_index, lat, path_bin, path_lat, sigma, mu):
if lat == 'mv':
bias = 50
else:
bias = 100
bin_name = 'f' + str(frame_index).zfill(3) + '_' + lat + '.bin'
bitin = arithmeticcoding.BitInputStream(open(path_bin + bin_name, "rb"))
dec = arithmeticcoding.ArithmeticDecoder(32, bitin)
latent = np.zeros([1, mu.shape[1], mu.shape[2], mu.shape[3]])
for h in range(mu.shape[1]):
for w in range(mu.shape[2]):
for ch in range(mu.shape[3]):
mu_val = mu[0, h, w, ch] + bias
sigma_val = sigma[0, h, w, ch]
freq = arithmeticcoding.logFrequencyTable_exp(mu_val, sigma_val, np.int(bias * 2 + 1))
symbol = dec.read(freq)
latent[0, h, w, ch] = symbol - bias
bitin.close()
np.save(path_lat + '/f' + str(frame_index).zfill(3) + '_' + lat + '.npy', latent)
print('Decoded latent_' + lat + ' frame', frame_index)
return latent