-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.py
More file actions
355 lines (276 loc) · 10.4 KB
/
Copy pathcodec.py
File metadata and controls
355 lines (276 loc) · 10.4 KB
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
# Copyright (c) 2021-2022, InterDigital Communications, Inc
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted (subject to the limitations in the disclaimer
# below) provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of InterDigital Communications, Inc nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
# NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
# THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import struct
import sys
import time
import os
from pathlib import Path
import torch
import torch.nn.functional as F
from PIL import Image
from torchvision.transforms import ToPILImage, ToTensor
import compressai
from ckpts import models
torch.backends.cudnn.deterministic = True
model_ids = {k: i for i, k in enumerate(models.keys())}
metric_ids = {"mse": 0, "ms-ssim": 1}
def BoolConvert(a):
b = [False, True]
return b[int(a)]
def Average(lst):
return sum(lst) / len(lst)
def inverse_dict(d):
# We assume dict values are unique...
assert len(d.keys()) == len(set(d.keys()))
return {v: k for k, v in d.items()}
def filesize(filepath: str) -> int:
if not Path(filepath).is_file():
raise ValueError(f'Invalid file "{filepath}".')
return Path(filepath).stat().st_size
def load_image(filepath: str) -> Image.Image:
return Image.open(filepath).convert("RGB")
def img2torch(img: Image.Image) -> torch.Tensor:
return ToTensor()(img).unsqueeze(0)
def torch2img(x: torch.Tensor) -> Image.Image:
return ToPILImage()(x.clamp_(0, 1).squeeze())
def write_uints(fd, values, fmt=">{:d}I"):
fd.write(struct.pack(fmt.format(len(values)), *values))
return len(values) * 4
def write_uchars(fd, values, fmt=">{:d}B"):
fd.write(struct.pack(fmt.format(len(values)), *values))
return len(values) * 1
def read_uints(fd, n, fmt=">{:d}I"):
sz = struct.calcsize("I")
return struct.unpack(fmt.format(n), fd.read(n * sz))
def read_uchars(fd, n, fmt=">{:d}B"):
sz = struct.calcsize("B")
return struct.unpack(fmt.format(n), fd.read(n * sz))
def write_bytes(fd, values, fmt=">{:d}s"):
if len(values) == 0:
return
fd.write(struct.pack(fmt.format(len(values)), values))
return len(values) * 1
def read_bytes(fd, n, fmt=">{:d}s"):
sz = struct.calcsize("s")
return struct.unpack(fmt.format(n), fd.read(n * sz))[0]
def get_header(model_name, metric, quality):
"""Format header information:
- 1 byte for model id
- 4 bits for metric
- 4 bits for quality param
"""
metric = metric_ids[metric]
code = (metric << 4) | (quality - 1 & 0x0F)
return model_ids[model_name], code
def parse_header(header):
"""Read header information from 2 bytes:
- 1 byte for model id
- 4 bits for metric
- 4 bits for quality param
"""
model_id, code = header
quality = (code & 0x0F) + 1
metric = code >> 4
return (
inverse_dict(model_ids)[model_id],
inverse_dict(metric_ids)[metric],
quality,
)
def read_body(fd):
lstrings = []
shape = read_uints(fd, 2)
n_strings = read_uints(fd, 1)[0]
for _ in range(n_strings):
s = read_bytes(fd, read_uints(fd, 1)[0])
lstrings.append([s])
return lstrings, shape
def write_body(fd, shape, out_strings):
bytes_cnt = 0
bytes_cnt = write_uints(fd, (shape[0], shape[1], len(out_strings)))
for s in out_strings:
bytes_cnt += write_uints(fd, (len(s[0]),))
bytes_cnt += write_bytes(fd, s[0])
return bytes_cnt
def pad(x, p=2 ** 6):
h, w = x.size(2), x.size(3)
H = (h + p - 1) // p * p
W = (w + p - 1) // p * p
padding_left = (W - w) // 2
padding_right = W - w - padding_left
padding_top = (H - h) // 2
padding_bottom = H - h - padding_top
return F.pad(
x,
(padding_left, padding_right, padding_top, padding_bottom),
mode="constant",
value=0,
)
def crop(x, size):
H, W = x.size(2), x.size(3)
h, w = size
padding_left = (W - w) // 2
padding_right = W - w - padding_left
padding_top = (H - h) // 2
padding_bottom = H - h - padding_top
return F.pad(
x,
(-padding_left, -padding_right, -padding_top, -padding_bottom),
mode="constant",
value=0,
)
def _encode(image, model, metric, quality, coder, output):
compressai.set_entropy_coder(coder)
enc_start = time.time()
img = load_image(image)
start = time.time()
# net = models[model](quality=quality, metric=metric, pretrained=True).eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = models[model](quality=quality, metric=metric, pretrained=True).to(device).eval()
load_time = time.time() - start
x = img2torch(img)
h, w = x.size(2), x.size(3)
p = 256 # maximum 6 strides of 2, and window size 4 for the smallest latent fmap: 4*2^6=256
x = pad(x, p)
x = x.to(device)
# net = net.cuda()
with torch.no_grad():
out = net.compress(x)
shape = out["shape"]
header = get_header(model, metric, quality)
with Path(output).open("wb") as f:
write_uchars(f, header)
# write original image size
write_uints(f, (h, w))
# write shape and number of encoded latents
write_body(f, shape, out["strings"])
enc_time = time.time() - enc_start
size = filesize(output)
bpp = float(size) * 8 / (img.size[0] * img.size[1])
print(
f"{bpp:.3f} bpp |"
f" Encoded in {enc_time:.2f}s (model loading: {load_time:.2f}s)"
)
def _decode(inputpath, coder, show, output=None):
compressai.set_entropy_coder(coder)
with Path(inputpath).open("rb") as f:
model, metric, quality = parse_header(read_uchars(f, 2))
original_size = read_uints(f, 2)
strings, shape = read_body(f)
print(f"Model: {model:s}, metric: {metric:s}, quality: {quality:d}")
# net = models[model](quality=quality, metric=metric, pretrained=True).eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = models[model](quality=quality, metric=metric, pretrained=True).to(device).eval()
torch.cuda.synchronize()
start = time.time()
# net = net.cuda()
for decode_times in range(100):
with torch.no_grad():
out = net.decompress(strings, shape)
x_hat = crop(out["x_hat"], original_size)
img = torch2img(x_hat)
torch.cuda.synchronize()
end = time.time()
dec_time = end - start
print(f"Decoded in {dec_time:.2f}s")
if show:
show_image(img)
if output is not None:
img.save(output)
def show_image(img: Image.Image):
from matplotlib import pyplot as plt
fig, ax = plt.subplots()
ax.axis("off")
ax.title.set_text("Decoded image")
ax.imshow(img)
fig.tight_layout()
plt.show()
def encode(argv):
parser = argparse.ArgumentParser(description="Encode image to bit-stream")
parser.add_argument("--image", type=str, default='/workspace/Kodak/kodim05.png')
parser.add_argument(
"--model",
choices=models.keys(),
default=list(models.keys())[0],
help="NN model to use (default: %(default)s)",
)
parser.add_argument(
"-m",
"--metric",
choices=metric_ids.keys(),
default="mse",
help="metric trained against (default: %(default)s",
)
parser.add_argument(
"-q",
"--quality",
choices=list(range(1, 9)),
type=int,
default=3,
help="Quality setting (default: %(default)s)",
)
parser.add_argument(
"-c",
"--coder",
choices=compressai.available_entropy_coders(),
default=compressai.available_entropy_coders()[0],
help="Entropy coder (default: %(default)s)",
)
parser.add_argument("-o", "--output", help="Output path",default='out.bin')
args = parser.parse_args(argv)
if not args.output:
args.output = Path(Path(args.image).resolve().name).with_suffix(".bin")
_encode(args.image, args.model, args.metric, args.quality, args.coder, args.output)
def decode(argv):
parser = argparse.ArgumentParser(description="Decode bit-stream to imager")
parser.add_argument("--input", type=str, default='out.bin')
parser.add_argument(
"-c",
"--coder",
choices=compressai.available_entropy_coders(),
default=compressai.available_entropy_coders()[0],
help="Entropy coder (default: %(default)s)",
)
parser.add_argument("--show", action="store_true")
parser.add_argument("-o", "--output", help="Output path",default='out.png')
args = parser.parse_args(argv)
_decode(args.input, args.coder, args.show, args.output)
def parse_args(argv):
parser = argparse.ArgumentParser(description="")
parser.add_argument("--command", choices=["encode", "decode"],default="decode")
args = parser.parse_args(argv)
return args
def main(argv):
args = parse_args(argv[1:2])
argv = argv[2:]
torch.set_num_threads(1) # just to be sure
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
if args.command == "encode":
encode(argv)
elif args.command == "decode":
decode(argv)
if __name__ == "__main__":
main(sys.argv)