-
Notifications
You must be signed in to change notification settings - Fork 5
/
DiffJPEG.py
389 lines (321 loc) · 11.9 KB
/
DiffJPEG.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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#sources: https://github.com/yuqing-liu-dut/JPEG-Pytorch
import itertools
import numpy as np
import torch
import torch.nn as nn
class DiffJPEG(nn.Module):
def __init__(self, height, width, differentiable=True, quality=80):
''' Initialize the DiffJPEG layer
Inputs:
height(int): Original image hieght
width(int): Original image width
differentiable(bool): If true uses custom differentiable
rounding function, if false uses standrard torch.round
quality(float): Quality factor for jpeg compression scheme.
'''
super(DiffJPEG, self).__init__()
if differentiable:
rounding = diff_round
else:
rounding = torch.round
factor = quality_to_factor(quality)
self.compress = compress_jpeg(rounding=rounding, factor=factor)
self.decompress = decompress_jpeg(height, width, rounding=rounding, factor=factor)
def forward(self, x):
y, cb, cr = self.compress(x)
recovered = self.decompress(y, cb, cr)
return recovered
def set_quality(self, quality):
factor = quality_to_factor(quality)
self.compress.factor = factor
self.decompress.factor = factor
class rgb_to_ycbcr_jpeg(nn.Module):
""" Converts RGB image to YCbCr
Input:
image(tensor): batch x 3 x height x width
Output:
result(tensor): batch x height x width x 3
"""
def __init__(self):
super(rgb_to_ycbcr_jpeg, self).__init__()
matrix = np.array([
[0.299, 0.587, 0.114],
[-0.168736, -0.331264, 0.5],
[0.5, -0.418688, -0.081312]
], dtype=np.float32).T
self.shift = nn.Parameter(torch.tensor([0., 128., 128.]))
self.matrix = nn.Parameter(torch.from_numpy(matrix))
def forward(self, image):
image = image.permute(0, 2, 3, 1)
result = torch.tensordot(image, self.matrix, dims=1) + self.shift
result.view(image.shape)
return result
class chroma_subsampling(nn.Module):
""" Chroma subsampling on CbCv channels
Input:
image(tensor): batch x height x width x 3
Output:
y(tensor): batch x height x width
cb(tensor): batch x height/2 x width/2
cr(tensor): batch x height/2 x width/2
"""
def __init__(self):
super(chroma_subsampling, self).__init__()
def forward(self, image):
image_2 = image.permute(0, 3, 1, 2).clone()
avg_pool = nn.AvgPool2d(kernel_size=2, stride=(2, 2),
count_include_pad=False)
cb = avg_pool(image_2[:, 1, :, :].unsqueeze(1))
cr = avg_pool(image_2[:, 2, :, :].unsqueeze(1))
cb = cb.permute(0, 2, 3, 1)
cr = cr.permute(0, 2, 3, 1)
return image[:, :, :, 0], cb.squeeze(3), cr.squeeze(3)
class block_splitting(nn.Module):
""" Splitting image into patches
Input:
image(tensor): batch x height x width
Output:
patch(tensor): batch x h*w/64 x h x w
"""
def __init__(self):
super(block_splitting, self).__init__()
self.k = 8
def forward(self, image):
height, width = image.shape[1:3]
batch_size = image.shape[0]
image_reshaped = image.view(batch_size, height // self.k, self.k, -1, self.k)
image_transposed = image_reshaped.permute(0, 1, 3, 2, 4)
return image_transposed.contiguous().view(batch_size, -1, self.k, self.k)
class dct_8x8(nn.Module):
""" Discrete Cosine Transformation
Input:
image(tensor): batch x height x width
Output:
dcp(tensor): batch x height x width
"""
def __init__(self):
super(dct_8x8, self).__init__()
tensor = np.zeros((8, 8, 8, 8), dtype=np.float32)
for x, y, u, v in itertools.product(range(8), repeat=4):
tensor[x, y, u, v] = np.cos((2 * x + 1) * u * np.pi / 16) * np.cos((2 * y + 1) * v * np.pi / 16)
alpha = np.array([1. / np.sqrt(2)] + [1] * 7)
self.tensor = nn.Parameter(torch.from_numpy(tensor).float())
self.scale = nn.Parameter(torch.from_numpy(np.outer(alpha, alpha) * 0.25).float())
def forward(self, image):
image = image - 128
result = self.scale * torch.tensordot(image, self.tensor, dims=2)
result.view(image.shape)
return result
class compress_jpeg(nn.Module):
""" Full JPEG compression algorithm
Input:
rounding(function): rounding function to use
factor(float): Compression factor
Output:
compressed(dict(tensor)): batch x h*w/64 x 8 x 8
"""
def __init__(self, rounding=torch.round, factor=1):
super(compress_jpeg, self).__init__()
self.l1 = nn.Sequential(
rgb_to_ycbcr_jpeg(),
chroma_subsampling()
)
self.l2 = nn.Sequential(
block_splitting(),
dct_8x8()
)
self.factor = factor
self.rounding = rounding
self.c_table = c_table
self.y_table = y_table
def forward(self, image):
y, cb, cr = self.l1(image)
components = {
'y': y,
'cb': cb,
'cr': cr
}
for k in components.keys():
comp = self.l2(components[k])
if k in ('cb', 'cr'):
comp = comp.float() / (self.c_table * self.factor)
comp = self.rounding(comp)
else:
comp = comp.float() / (self.y_table * self.factor)
comp = self.rounding(comp)
components[k] = comp
return components['y'], components['cb'], components['cr']
class idct_8x8(nn.Module):
""" Inverse discrete Cosine Transformation
Input:
dcp(tensor): batch x height x width
Output:
image(tensor): batch x height x width
"""
def __init__(self):
super(idct_8x8, self).__init__()
alpha = np.array([1. / np.sqrt(2)] + [1] * 7)
self.alpha = nn.Parameter(torch.from_numpy(np.outer(alpha, alpha)).float())
tensor = np.zeros((8, 8, 8, 8), dtype=np.float32)
for x, y, u, v in itertools.product(range(8), repeat=4):
tensor[x, y, u, v] = np.cos((2 * u + 1) * x * np.pi / 16) * np.cos((2 * v + 1) * y * np.pi / 16)
self.tensor = nn.Parameter(torch.from_numpy(tensor).float())
def forward(self, image):
image = image * self.alpha
result = 0.25 * torch.tensordot(image, self.tensor, dims=2) + 128
result.view(image.shape)
return result
class block_merging(nn.Module):
""" Merge pathces into image
Inputs:
patches(tensor) batch x height*width/64, height x width
height(int)
width(int)
Output:
image(tensor): batch x height x width
"""
def __init__(self):
super(block_merging, self).__init__()
def forward(self, patches, height, width):
k = 8
batch_size = patches.shape[0]
image_reshaped = patches.view(batch_size, height // k, width // k, k, k)
image_transposed = image_reshaped.permute(0, 1, 3, 2, 4)
return image_transposed.contiguous().view(batch_size, height, width)
class chroma_upsampling(nn.Module):
""" Upsample chroma layers
Input:
y(tensor): y channel image
cb(tensor): cb channel
cr(tensor): cr channel
Output:
image(tensor): batch x height x width x 3
"""
def __init__(self):
super(chroma_upsampling, self).__init__()
def forward(self, y, cb, cr):
def repeat(x, k=2):
height, width = x.shape[1:3]
x = x.unsqueeze(-1)
x = x.repeat(1, 1, k, k)
x = x.view(-1, height * k, width * k)
return x
cb = repeat(cb)
cr = repeat(cr)
return torch.cat([y.unsqueeze(3), cb.unsqueeze(3), cr.unsqueeze(3)], dim=3)
class ycbcr_to_rgb_jpeg(nn.Module):
""" Converts YCbCr image to RGB JPEG
Input:
image(tensor): batch x height x width x 3
Output:
result(tensor): batch x 3 x height x width
"""
def __init__(self):
super(ycbcr_to_rgb_jpeg, self).__init__()
matrix = np.array([
[1., 0., 1.402],
[1, -0.344136, -0.714136],
[1, 1.772, 0]
], dtype=np.float32).T
self.shift = nn.Parameter(torch.tensor([0, -128., -128.]))
self.matrix = nn.Parameter(torch.from_numpy(matrix))
def forward(self, image):
result = torch.tensordot(image + self.shift, self.matrix, dims=1)
result.view(image.shape)
return result.permute(0, 3, 1, 2)
class decompress_jpeg(nn.Module):
""" Full JPEG decompression algorithm
Input:
compressed(dict(tensor)): batch x h*w/64 x 8 x 8
rounding(function): rounding function to use
factor(float): Compression factor
Output:
image(tensor): batch x 3 x height x width
"""
def __init__(self, height, width, rounding=torch.round, factor=1):
super(decompress_jpeg, self).__init__()
self.rounding = rounding
self.factor = factor
self.c_table = c_table
self.y_table = y_table
self.idct = idct_8x8()
self.merging = block_merging()
self.chroma = chroma_upsampling()
self.colors = ycbcr_to_rgb_jpeg()
self.height, self.width = height, width
def forward(self, y, cb, cr):
components = {'y': y, 'cb': cb, 'cr': cr}
for k in components.keys():
if k in ('cb', 'cr'):
comp = components[k]
comp = comp * (self.c_table * self.factor)
height, width = int(self.height / 2), int(self.width / 2)
else:
comp = components[k]
comp = comp * (self.y_table * self.factor)
height, width = self.height, self.width
comp = self.idct(comp)
components[k] = self.merging(comp, height, width)
image = self.chroma(components['y'], components['cb'], components['cr'])
image = self.colors(image)
image = torch.min(255 * torch.ones_like(image), torch.max(torch.zeros_like(image), image))
return image
y_table = np.array([
[16, 11, 10, 16, 24, 40, 51, 61],
[12, 12, 14, 19, 26, 58, 60, 55],
[14, 13, 16, 24, 40, 57, 69, 56],
[14, 17, 22, 29, 51, 87, 80, 62],
[18, 22, 37, 56, 68, 109, 103, 77],
[24, 35, 55, 64, 81, 104, 113, 92],
[49, 64, 78, 87, 103, 121, 120, 101],
[72, 92, 95, 98, 112, 100, 103, 99]
], dtype=np.float32).T
y_table = nn.Parameter(torch.from_numpy(y_table))
c_table = np.empty((8, 8), dtype=np.float32)
c_table.fill(99)
c_table[:4, :4] = np.array([
[17, 18, 24, 47],
[18, 21, 26, 66],
[24, 26, 56, 99],
[47, 66, 99, 99]]).T
c_table = nn.Parameter(torch.from_numpy(c_table))
def diff_round(x):
""" Differentiable rounding function
Input:
x(tensor)
Output:
x(tensor)
"""
sign = torch.ones_like(x)
sign[torch.floor(x) % 2 == 0] = -1
y = sign * torch.cos(x * torch.pi) / 2
out = torch.round(x) + y - y.detach()
return out
def quality_to_factor(quality):
""" Calculate factor corresponding to quality
Input:
quality(float): Quality for jpeg compression
Output:
factor(float): Compression factor
"""
if quality < 50:
quality = 5000. / quality
else:
quality = 200. - quality * 2
return quality / 100.
if __name__ == '__main__':
with torch.no_grad():
import cv2
import numpy as np
img = cv2.imread("JPEG-Pytorch-main/Lena.png")
inputs = np.transpose(img, (2, 0, 1))
inputs = inputs[np.newaxis, ...]
tensor = torch.FloatTensor(inputs).cuda()
jpeg = DiffJPEG(512, 512, differentiable=True).cuda()
quality = 80
jpeg.set_quality(quality)
outputs = jpeg(tensor)
outputs = outputs.detach().cpu().numpy()
outputs = np.transpose(outputs[0], (1, 2, 0))
cv2.imshow("QF:"+str(quality), outputs / 255.)
cv2.waitKey()