-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
60 lines (53 loc) · 1.65 KB
/
main.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
import lpips
class util_of_lpips():
def __init__(self, net, use_gpu=False):
'''
Parameters
----------
net: str
抽取特征的网络,['alex', 'vgg']
use_gpu: bool
是否使用GPU,默认不使用
Returns
-------
References
-------
https://github.com/richzhang/PerceptualSimilarity/blob/master/lpips_2imgs.py
'''
## Initializing the model
self.loss_fn = lpips.LPIPS(net=net)
self.use_gpu = use_gpu
if use_gpu:
self.loss_fn.cuda()
def calc_lpips(self, img1_path, img2_path):
'''
Parameters
----------
img1_path : str
图像1的路径.
img2_path : str
图像2的路径.
Returns
-------
dist01 : torch.Tensor
学习的感知图像块相似度(Learned Perceptual Image Patch Similarity, LPIPS).
References
-------
https://github.com/richzhang/PerceptualSimilarity/blob/master/lpips_2imgs.py
'''
# Load images
img0 = lpips.im2tensor(lpips.load_image(img1_path)) # RGB image from [-1,1]
img1 = lpips.im2tensor(lpips.load_image(img2_path))
if self.use_gpu:
img0 = img0.cuda()
img1 = img1.cuda()
dist01 = self.loss_fn.forward(img0, img1)
return dist01
a = util_of_lpips('vgg')
sum1 = float(0)
for i in range(60):
file1 = "D:/Project/ImageEncipher/thumbnail/body/"+str(i+1)+".png"
file2 = "D:/Project/ImageEncipher/result2/body/"+str(i+1)+".png"
d = a.calc_lpips(file1,file2)
sum1 += float(d)
print(sum1/60)