-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
215 lines (154 loc) · 5.72 KB
/
utils.py
File metadata and controls
215 lines (154 loc) · 5.72 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
# -*- coding: utf-8 -*-
import os
import math
import datetime
import struct
import time
from pathlib import Path
import logging
import matplotlib.pyplot as plt
from PIL import Image
from pytorch_msssim import ms_ssim
import torch.nn.functional as F
import torch.nn as nn
import torch
from torchvision import transforms
from torchvision.transforms import ToPILImage, ToTensor
def setup_logger(logger_name, root, phase, level=logging.INFO, screen=False, tofile=False):
lg = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s.%(msecs)03d - %(levelname)s: %(message)s',
datefmt='%y-%m-%d %H:%M:%S')
lg.setLevel(level)
if tofile:
log_file = os.path.join(root, phase + '_{}.log'.format(get_timestamp()))
fh = logging.FileHandler(log_file, mode='w')
fh.setFormatter(formatter)
lg.addHandler(fh)
if screen:
sh = logging.StreamHandler()
sh.setFormatter(formatter)
lg.addHandler(sh)
def get_timestamp():
return datetime.datetime.now().strftime('%y%m%d-%H%M%S')
def cal_psnr(a: torch.Tensor, b: torch.Tensor) -> float:
mse = F.mse_loss(a, b).item()
return -10 * math.log10(mse)
def read_image(filepath: str) -> torch.Tensor:
assert os.path.isfile(filepath)
img = Image.open(filepath).convert("RGB")
return transforms.ToTensor()(img)
def rename_key(key):
# Deal with modules trained with DataParallel
if key.startswith("module."):
key = key[7:]
# ResidualBlockWithStride: 'downsample' -> 'skip'
if ".downsample." in key:
return key.replace("downsample", "skip")
# EntropyBottleneck: nn.ParameterList to nn.Parameters
if key.startswith("entropy_bottleneck."):
if key.startswith("entropy_bottleneck._biases."):
return f"entropy_bottleneck._bias{key[-1]}"
if key.startswith("entropy_bottleneck._matrices."):
return f"entropy_bottleneck._matrix{key[-1]}"
if key.startswith("entropy_bottleneck._factors."):
return f"entropy_bottleneck._factor{key[-1]}"
return key
def load_pretrained(state_dict):
state_dict = {rename_key(k): v for k, v in state_dict.items()}
return state_dict
def compute_psnr(a, b):
mse = torch.mean((a - b) ** 2).item()
return -10 * math.log10(mse)
def compute_msssim(a, b):
return ms_ssim(a, b, data_range=1.).item()
def compute_bpp(out_net):
size = out_net['x_hat'].size()
num_pixels = size[0] * size[2] * size[3]
return sum(torch.log(likelihoods).sum() / (-math.log(2) * num_pixels)
for likelihoods in out_net['likelihoods'].values()).item()
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 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 = 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 show_image(img: Image.Image):
fig, ax = plt.subplots()
ax.axis("off")
ax.title.set_text("Decoded image")
ax.imshow(img)
fig.tight_layout()
plt.show()
if __name__ == "__main__":
pass