-
Notifications
You must be signed in to change notification settings - Fork 3
/
image_utils.py
240 lines (198 loc) · 9.16 KB
/
image_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
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
import torch
import numpy as np
from PIL import Image
import math
import cv2
class TVLoss(torch.nn.Module):
def __init__(self):
super(TVLoss,self).__init__()
def forward(self,x):
batch_size = x.size()[0]
h_x = x.size()[2]
w_x = x.size()[3]
count_h = self._tensor_size(x[:,:,1:,:])
count_w = self._tensor_size(x[:,:,:,1:])
h_tv = torch.pow((x[:,:,1:,:]-x[:,:,:h_x-1,:]),2).sum()
w_tv = torch.pow((x[:,:,:,1:]-x[:,:,:,:w_x-1]),2).sum()
# return 2*(h_tv/count_h+w_tv/count_w)/batch_size
return 2 * (h_tv + w_tv)
def _tensor_size(self,t):
return t.size()[1]*t.size()[2]*t.size()[3]
def log_Logistic_256(x, mean, logvar, average=False, reduce=True, dim=None):
x = x.view(x.size(0), -1)
mean = mean.view(x.size(0), -1)
logvar = logvar.view(x.size(0), -1)
bin_size = 1. / 256.
# implementation like https://github.com/openai/iaf/blob/master/tf_utils/distributions.py#L28
scale = torch.exp(logvar)
x = (torch.floor(x / bin_size) * bin_size - mean) / scale
cdf_plus = torch.sigmoid(x + bin_size/scale)
cdf_minus = torch.sigmoid(x)
# calculate final log-likelihood for an image
log_logist_256 = - torch.log(cdf_plus - cdf_minus + 1.e-7)
if reduce:
if average:
return torch.mean(log_logist_256, dim)
else:
return torch.sum(log_logist_256, dim)
else:
return log_logist_256
def reduce_image(img, scale):
batch, channels, height, width = img.size()
reduced_img = torch.zeros(batch, channels * scale * scale, height // scale, width // scale).cuda()
for x in range(scale):
for y in range(scale):
for c in range(channels):
reduced_img[:, c + channels * (y + scale * x), :, :] = img[:, c, x::scale, y::scale]
return reduced_img
def reconstruct_image(features, scale):
batch, channels, height, width = features.size()
img_channels = channels // (scale**2)
reconstructed_img = torch.zeros(batch, img_channels, height * scale, width * scale).cuda()
for x in range(scale):
for y in range(scale):
for c in range(img_channels):
f_channel = c + img_channels * (y + scale * x)
reconstructed_img[:, c, x::scale, y::scale] = features[:, f_channel, :, :]
return reconstructed_img
def patchify_tensor(features, patch_size, overlap=10):
batch_size, channels, height, width = features.size()
# side = min(height, width, patch_size)
# delta = patch_size - side
# Z = torch.zeros([batch_size, channels, height + delta, width + delta])
# Z[:, :, delta // 2:height + delta // 2, delta // 2:width + delta // 2] = features
# features = Z
# batch_size, channels, height, width = features.size()
effective_patch_size = patch_size - overlap
n_patches_height = (height // effective_patch_size)
n_patches_width = (width // effective_patch_size)
if n_patches_height * effective_patch_size < height:
n_patches_height += 1
if n_patches_width * effective_patch_size < width:
n_patches_width += 1
patches = []
for b in range(batch_size):
for h in range(n_patches_height):
for w in range(n_patches_width):
patch_start_height = min(h * effective_patch_size, height - patch_size)
patch_start_width = min(w * effective_patch_size, width - patch_size)
patches.append(features[b:b+1, :,
patch_start_height: patch_start_height + patch_size,
patch_start_width: patch_start_width + patch_size])
return torch.cat(patches, 0)
def recompose_tensor(patches, full_height, full_width, overlap=10):
batch_size, channels, patch_size, _ = patches.size()
effective_patch_size = patch_size - overlap
n_patches_height = (full_height // effective_patch_size)
n_patches_width = (full_width // effective_patch_size)
if n_patches_height * effective_patch_size < full_height:
n_patches_height += 1
if n_patches_width * effective_patch_size < full_width:
n_patches_width += 1
n_patches = n_patches_height * n_patches_width
if batch_size % n_patches != 0:
print("Error: The number of patches provided to the recompose function does not match the number of patches in each image.")
final_batch_size = batch_size // n_patches
blending_in = torch.linspace(0.1, 1.0, overlap)
blending_out = torch.linspace(1.0, 0.1, overlap)
middle_part = torch.ones(patch_size - 2 * overlap)
blending_profile = torch.cat([blending_in, middle_part, blending_out], 0)
horizontal_blending = blending_profile[None].repeat(patch_size, 1)
vertical_blending = blending_profile[:, None].repeat(1, patch_size)
blending_patch = horizontal_blending * vertical_blending
blending_image = torch.zeros(1, channels, full_height, full_width)
for h in range(n_patches_height):
for w in range(n_patches_width):
patch_start_height = min(h * effective_patch_size, full_height - patch_size)
patch_start_width = min(w * effective_patch_size, full_width - patch_size)
blending_image[0, :, patch_start_height: patch_start_height + patch_size, patch_start_width: patch_start_width + patch_size] += blending_patch[None]
recomposed_tensor = torch.zeros(final_batch_size, channels, full_height, full_width)
if patches.is_cuda:
blending_patch = blending_patch.cuda()
blending_image = blending_image.cuda()
recomposed_tensor = recomposed_tensor.cuda()
patch_index = 0
for b in range(final_batch_size):
for h in range(n_patches_height):
for w in range(n_patches_width):
patch_start_height = min(h * effective_patch_size, full_height - patch_size)
patch_start_width = min(w * effective_patch_size, full_width - patch_size)
recomposed_tensor[b, :, patch_start_height: patch_start_height + patch_size, patch_start_width: patch_start_width + patch_size] += patches[patch_index] * blending_patch
patch_index += 1
recomposed_tensor /= blending_image
return recomposed_tensor
def modcrop(img, modulo):
(ih, iw) = img.size
ih = ih - (ih % modulo)
iw = iw - (iw % modulo)
img = img.crop((0, 0, ih, iw))
#y, cb, cr = img.split()
return img
def rescale_img(img_in, scale):
(w, h) = img_in.size
new_size_in = (int(scale*w), int(scale*h))
img_in = img_in.resize(new_size_in, resample=Image.BICUBIC)
return img_in
def rgb2ycbcr(img, only_y=True):
'''same as matlab rgb2ycbcr
only_y: only return Y channel
Input:
float32, [0, 255]
float32, [0, 255]
'''
img.astype(np.float32)
# convert
if only_y:
rlt = np.dot(img, [65.481, 128.553, 24.966]) / 255.0 + 16.0
else:
rlt = np.matmul(img, [[65.481, -37.797, 112.0], [128.553, -74.203, -93.786],
[24.966, 112.0, -18.214]]) / 255.0 + [16, 128, 128]
rlt = rlt.round()
return rlt
def PSNR(pred, gt, shave_border):
pred = pred[shave_border:-shave_border, shave_border:-shave_border]
gt = gt[shave_border:-shave_border, shave_border:-shave_border]
imdff = pred - gt
rmse = math.sqrt(np.mean(imdff ** 2))
if rmse == 0:
return 100
return 20 * math.log10(255.0 / rmse)
def calculate_ssim(img1, img2):
C1 = (0.01 * 255)**2
C2 = (0.03 * 255)**2
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
kernel = cv2.getGaussianKernel(11, 1.5)
window = np.outer(kernel, kernel.transpose())
mu1 = cv2.filter2D(img1, -1, window)[5:-5, 5:-5] # valid
mu2 = cv2.filter2D(img2, -1, window)[5:-5, 5:-5]
mu1_sq = mu1**2
mu2_sq = mu2**2
mu1_mu2 = mu1 * mu2
sigma1_sq = cv2.filter2D(img1**2, -1, window)[5:-5, 5:-5] - mu1_sq
sigma2_sq = cv2.filter2D(img2**2, -1, window)[5:-5, 5:-5] - mu2_sq
sigma12 = cv2.filter2D(img1 * img2, -1, window)[5:-5, 5:-5] - mu1_mu2
ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) *
(sigma1_sq + sigma2_sq + C2))
return ssim_map.mean()
def SSIM(img1, img2, shave_border):
'''calculate SSIM
the same outputs as MATLAB's
img1, img2: [0, 255]
'''
img1 = img1[shave_border:-shave_border, shave_border:-shave_border]
img2 = img2[shave_border:-shave_border, shave_border:-shave_border]
if not img1.shape == img2.shape:
raise ValueError('Input images must have the same dimensions.')
if img1.ndim == 2:
return calculate_ssim(img1, img2)
elif img1.ndim == 3:
if img1.shape[2] == 3:
ssims = []
for i in range(3):
ssims.append(calculate_ssim(img1, img2))
return np.array(ssims).mean()
elif img1.shape[2] == 1:
return calculate_ssim(np.squeeze(img1), np.squeeze(img2))
else:
raise ValueError('Wrong input image dimensions.')