-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
122 lines (110 loc) · 4.37 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
import argparse
from cmath import log10, sqrt
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import utils, datasets, transforms
import time
import scipy
from numpy.linalg import inv
import sys
import matplotlib.pyplot as plt
from PIL import Image
import cv2
from skimage.metrics import structural_similarity as ssim
import os
def plot(data):
fig, axs = plt.subplots(3, 3)
for id in range(3*3):
if id<data.shape[0]:
axs[id//3, id%3].imshow(data[id].cpu().data.numpy())
axs[id // 3, id % 3].set_xticks([])
axs[id // 3, id % 3].set_yticks([])
plt.show()
def view_image(tensor): # input: (C, H, W)
img = utils.make_grid(tensor)
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy() # convert from tensor
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
'''
PSNR is most commonly used to estimate the efficiency of compressors, filters, etc.
The larger the value of PSNR,
the more efficient is a corresponding compression or filter method.
'''
def PSNR(original_path, compressed_path):
original = cv2.imread(original_path)
compressed = cv2.imread(compressed_path, 1)
mse = np.mean((original - compressed) ** 2)
if(mse == 0): # MSE is zero means no noise is present in the signal .
# Therefore PSNR have no importance.
return 100
max_pixel = 255.0
psnr = 20 * log10(max_pixel / sqrt(mse))
return psnr
def mse(original_path, compressed_path):
# the MSE between the two images is the sum of the squared difference between the two images
original = cv2.imread(original_path)
compressed = cv2.imread(compressed_path, 1)
mse = np.mean((original - compressed) ** 2)
return mse
# define function that combines all three image quality metrics
def compare_images(original_path, compressed_path):
scores = []
scores.append(PSNR(original_path, compressed_path))
scores.append(mse(original_path, compressed_path))
original = cv2.imread(original_path)
compressed = cv2.imread(compressed_path, 1)
scores.append(ssim(compressed, original, multichannel =True)) # target, ref (original)
return scores
'''
show image size
'''
def show_image_size(image_path):
# get image
img = Image.open(image_path)
# get width and height
width = img.width
height = img.height
# display width and height
print("The height of the image is: ", height)
print("The width of the image is: ", width)
def Display_images_as_subplots(original,compressed,restored):
plt.rcParams.update({'figure.max_open_warning': 0})
# display images as subplots
original = cv2.imread(original)
compressed = cv2.imread(compressed)
restored = cv2.imread(restored) # 1 means read as color image
fig, axs = plt.subplots(1, 3, figsize=(20, 8))
axs[0].imshow(cv2.cvtColor(original, cv2.COLOR_BGR2RGB))
axs[0].set_title('Original')
axs[1].imshow(cv2.cvtColor(compressed, cv2.COLOR_BGR2RGB))
axs[1].set_title('Compressed')
axs[2].imshow(cv2.cvtColor(restored, cv2.COLOR_BGR2RGB))
axs[2].set_title('DeepPCA Restored')
# remove the x and y ticks
for ax in axs:
ax.set_xticks([])
ax.set_yticks([])
plt.show()
def plot_one_image(img):
# plt.imshow('image window', img)
# plt.imshow(img.cpu().data.numpy())
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
def show_three_dataset():
# plt.rcParams.update({'figure.max_open_warning': 0})
original_dir = os.path.join('datasets', 'att_faces')
compressed_dir = os.path.join('datasets', 'att_faces_compress')
restored_dir = os.path.join('datasets', 'att_faces_restore')
for face_id in range(1, 40 + 1):
for test_id in range(1, 11):
# if (test_id in self.training_ids[face_id-1]) == False: # we skip the image if it is part of the training set
path_to_img_original = os.path.join(original_dir,
's' + str(face_id), str(test_id) + '.pgm')
path_to_img_compressed = os.path.join(compressed_dir,
's' + str(face_id), str(test_id) + '.pgm')
path_to_img_restore = os.path.join(restored_dir,
's' + str(face_id), str(test_id) + '.pgm')
Display_images_as_subplots(path_to_img_original,path_to_img_compressed,path_to_img_restore)