forked from HobbitLong/RepDistiller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_compression.py
More file actions
168 lines (129 loc) · 5.42 KB
/
check_compression.py
File metadata and controls
168 lines (129 loc) · 5.42 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
import numpy as np
from PIL import Image
import torchvision.transforms as transforms
import torchvision.transforms.functional as TF
import torch.nn.functional as F
import random
from io import BytesIO
import torch
import copy
import torch
import torch.nn as nn
# Define the random model architecture
class RandomModel(nn.Module):
def __init__(self):
super(RandomModel, self).__init__()
self.fc1 = nn.Linear(3072, 10)
self.fc2 = nn.Linear(10, 5)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
# Instantiate the random model
model = RandomModel()
# Generate random input data
batch_size =1
width = 32
height = 32
channels = 3
input_data = torch.randn(batch_size, channels, height, width) # Input tensor of shape (batch_size, channels, height, width)
test_transform = transforms.Compose([
# transforms.Resize((32, 32)),
transforms.ToTensor(),
transforms.Normalize((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
])
def transform_apply(image, aug_parameters=None, compress=False):
resize = transforms.Resize(size=(32,32))
image = resize(image)
qf, i, j, h, w, rand_HorFlip = list(aug_parameters)
try:
if (aug_parameters is not None) and (not np.isnan(aug_parameters).any()) and aug_parameters[-1] != -2:
# print(list(aug_parameters))
image = TF.pad(image, (4, 4, 4, 4))
image = TF.resized_crop(image, i, j, h, w, size=(32,32), interpolation=TF.InterpolationMode.BILINEAR)
if rand_HorFlip > 0.5:
image = TF.hflip(image)
except:
print("Error in transform_apply")
# if compress:
# buffer = BytesIO()
# image.save(buffer, 'JPEG', quality=int(qf), subsampling=0)
# image_wo_transform = copy.copy(Image.open(buffer).convert("RGB"))
image_wo_transform = image
image = test_transform(image)
return image, image_wo_transform
def transform_new(image, quality):
resize = transforms.Resize(size=(32,32))
image = resize(image)
rand_HorFlip = random.random()
image_org = copy.copy(image)
# ONLY CIFAR 100
padding = 4
output_size = (32, 32)
image = TF.pad(image, (padding, padding, padding, padding))
crop = transforms.RandomCrop(output_size[0])
i, j, h, w = crop.get_params(image, output_size)
image = TF.resized_crop(image, i, j, h, w, output_size, interpolation=TF.InterpolationMode.BILINEAR)
# resize = transforms.Resize(size=(128,128))
# image = resize(image)
# print(i,j,h,w)
# image.save('./compressed_images/example0_%.2f.jpg'%(rand_HorFlip))
# image = TF.pad(image_org, (4, 4, 4, 4))
# image = TF.resized_crop(image, i, j, h, w, output_size)
# resize = transforms.Resize(size=(128,128))
# image = resize(image)
# image.save('./compressed_images/example1_%.2f.jpg'%(rand_HorFlip))
# image = transforms.RandomCrop(32, padding=4)(image_org)
# resize = transforms.Resize(size=(128,128))
# image = resize(image)
# image.save('./compressed_images/example2_%.2f.jpg'%(rand_HorFlip))
# resize = transforms.Resize(size=(32,32))
# image = resize(image)
# Random horizontal flipping
if rand_HorFlip > 0.5:
image = TF.hflip(image)
aug_parameters = [i, j, h, w, rand_HorFlip]
# image.save(buffer1, 'JPEG', quality=quality, subsampling=0)
# image_wo_transform = Image.open(buffer1).convert("RGB")
image_wo_transform = image
image = test_transform(image)
return image, aug_parameters, image_wo_transform
# Load the original image
img = Image.open('original_image.jpg')
# Convert the image to a numpy array
img_arr = np.array(img)
input = Image.fromarray(img_arr)
buffer1 = BytesIO()
buffer2 = BytesIO()
count = 0
# qf_range = range(100,-1,-1)
qf_range = range(0,101,20)
# Loop over different JPEG quality levels and compress the image
for quality in qf_range:
buffer = BytesIO()
input_org = copy.copy(input)
input_org.save(buffer1, 'JPEG', quality=quality, subsampling=0)
input_org = Image.open(buffer1).convert("RGB")
# Compress the image using JPEG with the specified quality level
input1 = copy.copy(input_org)
input1, aug_parameters, input1_wo_transform = transform_new(input1, quality)
aug_parameters.insert(0, quality)
input2 = copy.copy(input_org)
input2, input2_wo_transform = transform_apply(input2, aug_parameters=aug_parameters, compress=True)
input1 = input1.view(batch_size, -1) # Flatten the input1 tensor
output1 = model(input1)
input2 = input2.view(batch_size, -1) # Flatten the input2 tensor
output2 = model(input2)
# print(np.sum(np.subtract(input1_wo_transform, input2_wo_transform)))
# Check if the pixel values of the original and compressed images are the same
# if torch.sum(torch.subtract(input1, input2)) < 1 :
if torch.all(torch.eq(output1, output2)) and torch.sum(torch.subtract(input1, input2)) == 0 \
and np.sum(np.subtract(input1_wo_transform, input2_wo_transform)) == 0 :
# print(f"pixel values and Transform ==> QF {quality} has the same as the original image")
count+=1
else:
print(f"pixel values and Transform ==> QF {quality} is different from the original image")
print("Counts : ",(100* count/len(qf_range)))
# Delete the original image and the original numpy array to free memory
img.close()
del img_arr