-
Notifications
You must be signed in to change notification settings - Fork 20
/
utils.py
180 lines (160 loc) · 4.9 KB
/
utils.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import math
import numpy as np
import datetime
import os
import sys
import cv2
from math import exp
from pytorch_msssim import ssim
import importlib
def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor:
r"""Convert an RGB image to YCbCr.
Args:
image (torch.Tensor): RGB Image to be converted to YCbCr.
Returns:
torch.Tensor: YCbCr version of the image.
"""
if not torch.is_tensor(image):
raise TypeError("Input type is not a torch.Tensor. Got {}".format(type(image)))
if len(image.shape) < 3 or image.shape[-3] != 3:
raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}".format(image.shape))
image = image / 255. ## image in range (0, 1)
r: torch.Tensor = image[..., 0, :, :]
g: torch.Tensor = image[..., 1, :, :]
b: torch.Tensor = image[..., 2, :, :]
y: torch.Tensor = 65.481 * r + 128.553 * g + 24.966 * b + 16.0
cb: torch.Tensor = -37.797 * r + -74.203 * g + 112.0 * b + 128.0
cr: torch.Tensor = 112.0 * r + -93.786 * g + -18.214 * b + 128.0
return torch.stack((y, cb, cr), -3)
def prepare_qat(model):
## fuse model
model.module.fuse_model()
## qconfig and qat-preparation & per-channel quantization
model.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')
# model.qconfig = torch.quantization.get_default_qat_qconfig('qnnpack')
# model.qconfig = torch.quantization.QConfig(
# activation=torch.quantization.FakeQuantize.with_args(
# observer=torch.quantization.MinMaxObserver,
# quant_min=-128,
# quant_max=127,
# qscheme=torch.per_tensor_symmetric,
# dtype=torch.qint8,
# reduce_range=False),
# weight=torch.quantization.FakeQuantize.with_args(
# observer=torch.quantization.MinMaxObserver,
# quant_min=-128,
# quant_max=+127,
# dtype=torch.qint8,
# qscheme=torch.per_tensor_symmetric,
# reduce_range=False)
# )
model = torch.quantization.prepare_qat(model, inplace=True)
return model
def import_module(name):
return importlib.import_module(name)
def calc_psnr(sr, hr):
sr, hr = sr.double(), hr.double()
diff = (sr - hr) / 255.00
mse = diff.pow(2).mean()
psnr = -10 * math.log10(mse)
return float(psnr)
def calc_ssim(sr, hr):
ssim_val = ssim(sr, hr, size_average=True)
return float(ssim_val)
def ndarray2tensor(ndarray_hwc):
ndarray_chw = np.ascontiguousarray(ndarray_hwc.transpose((2, 0, 1)))
tensor = torch.from_numpy(ndarray_chw).float()
return tensor
def cur_timestamp_str():
now = datetime.datetime.now()
year = str(now.year)
month = str(now.month).zfill(2)
day = str(now.day).zfill(2)
hour = str(now.hour).zfill(2)
minute = str(now.minute).zfill(2)
content = "{}-{}{}-{}{}".format(year, month, day, hour, minute)
return content
class ExperimentLogger(object):
def __init__(self, filename='default.log', stream=sys.stdout):
self.terminal = stream
self.log = open(filename, 'a')
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
self.terminal.flush()
self.log.flush()
def get_stat_dict():
stat_dict = {
'epochs': 0,
'losses': [],
'ema_loss': 0.0,
'set5': {
'psnrs': [],
'ssims': [],
'best_psnr': {
'value': 0.0,
'epoch': 0
},
'best_ssim': {
'value': 0.0,
'epoch': 0
}
},
'set14': {
'psnrs': [],
'ssims': [],
'best_psnr': {
'value': 0.0,
'epoch': 0
},
'best_ssim': {
'value': 0.0,
'epoch': 0
}
},
'b100': {
'psnrs': [],
'ssims': [],
'best_psnr': {
'value': 0.0,
'epoch': 0
},
'best_ssim': {
'value': 0.0,
'epoch': 0
}
},
'u100': {
'psnrs': [],
'ssims': [],
'best_psnr': {
'value': 0.0,
'epoch': 0
},
'best_ssim': {
'value': 0.0,
'epoch': 0
}
},
'manga109': {
'psnrs': [],
'ssims': [],
'best_psnr': {
'value': 0.0,
'epoch': 0
},
'best_ssim': {
'value': 0.0,
'epoch': 0
}
}
}
return stat_dict
if __name__ == '__main__':
timestamp = cur_timestamp_str()
print(timestamp)