-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculo_ssim.py
133 lines (115 loc) · 4.3 KB
/
calculo_ssim.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
import argparse
import os
import sys
import random
import shutil
import time
import warnings
from srblib import abs_path
from PIL import ImageFilter, Image
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
import torchvision.transforms as transforms
import torchvision.datasets as datasets
import torchvision.models as models
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm, trange
import pytorch_ssim
from skimage.feature import hog
from scipy.stats import spearmanr, pearsonr
from skimage.metrics import structural_similarity as ssim
results_path1 = './output_v4_tv'
results_path2 = './output_v4_tv_0.1'
mask_path1 = os.listdir(results_path1)
mask_path2 = os.listdir(results_path2)
mask_list1 = [i.split('_mask')[0] for i in mask_path1]
mask_list2 = [i.split('_mask')[0] for i in mask_path2]
input_dir_path = 'images_list.txt'
text_file = abs_path(input_dir_path)
img_name_list = []
with open(text_file, 'r') as f:
for line in f:
img_name_list.append(line.split('\n')[0])
class DataProcessing:
def __init__(self, img_idxs=[0, 1]):
#mask_list_slice1 = mask_list1[img_idxs[0]:img_idxs[1]]
#mask_list_slice2 = mask_list2[img_idxs[0]:img_idxs[1]]
img_list = img_name_list[img_idxs[0]:img_idxs[1]]
self.mask_filenames1 = [os.path.join(results_path1, '{}_mask.npy'.format(i)) for i in img_list]
self.mask_filenames2 = [os.path.join(results_path2, '{}_mask.npy'.format(i)) for i in img_list]
def __getitem__(self, index):
mask1 = np.load(self.mask_filenames1[index])
mask2 = np.load(self.mask_filenames2[index])
hog1 = hog(mask1, pixels_per_cell=(16, 16))
hog2 = hog(mask2, pixels_per_cell=(16, 16))
hog_pear, _ = pearsonr(hog1, hog2)
ssim_resu = ssim(mask1, mask2, data_range=1, win_size=5)
return mask1, mask2, hog_pear, ssim_resu
def __len__(self):
return len(self.mask_filenames1)
# Plots image from tensor
def tensor_imshow(inp, title=None, **kwargs):
"""Imshow for Tensor."""
inp = inp.numpy().transpose((1, 2, 0))
# Mean and std for ImageNet
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
inp = std * inp + mean
inp = np.clip(inp, 0, 1)
plt.imshow(inp, **kwargs)
if title is not None:
plt.title(title)
plt.show()
batch_size = 5
idx_start = 0
idx_end = 5
#batch_size = 10
mask_dataset = DataProcessing(img_idxs=[idx_start, idx_end])
mask_loader = torch.utils.data.DataLoader(mask_dataset, batch_size=batch_size, shuffle=False, num_workers=24, pin_memory=True)
torch.cuda.set_device(0)
iterator = tqdm(enumerate(mask_loader), total=len(mask_loader), desc='batch')
for i, (mask1, mask2, hog_pear, ssim_resu) in iterator:
mask1 = mask1.cuda()
mask2 = mask2.cuda()
mask1 = mask1.reshape(mask1.size(0), 1, mask1.size(1), mask1.size(2))
mask2 = mask2.reshape(mask2.size(0), 1, mask2.size(1), mask2.size(2))
print(ssim_resu)
print(hog_pear)
print('SSIM = ', pytorch_ssim.ssim(mask1, mask2).cpu().numpy())
print('Pearson HOG = ', hog_pear.mean())
print('SSIM (individual) = ', ssim_resu.mean())
# print('SSIM = ', pytorch_ssim.ssim(mask1, mask2, size_average = False).cpu().numpy())
# fong0 = np.load('fong_0.0.npy')
# fong1 = np.load('fong_0.05.npy')
# fong2 = np.load('fong_0.1.npy')
#
# fong_A = torch.from_numpy(np.stack((fong0, fong0)).reshape(2, 1, 224, 224))
# fong_B = torch.from_numpy(np.stack((fong1, fong2)).reshape(2, 1, 224, 224))
#
# print('SSIM (Fong) = ', pytorch_ssim.ssim(fong_A, fong_B))
#
# fabio0 = np.load('v4_0.0.npy')
# fabio1 = np.load('v4_0.05.npy')
# fabio2 = np.load('v4_0.1.npy')
#
# fabio_A = torch.from_numpy(np.stack((fabio0, fabio0)).reshape(2, 1, 224, 224))
# fabio_B = torch.from_numpy(np.stack((fabio1, fabio2)).reshape(2, 1, 224, 224))
#
# print('SSIM (Fabio) = ', pytorch_ssim.ssim(fabio_A, fabio_B))
#
#hog0, hog_img0 = hog(mask1, pixels_per_cell=(16, 16), visualize=True)
# hog1, hog_img1 = hog(fabio2, pixels_per_cell=(16, 16), visualize=True)
# out, _ = pearsonr(hog0, hog1)
# print('pearson', out)
#
# out, _ = spearmanr(fabio0, fabio2, axis=None)
# print('spearman', out)
#
# out = ssim(fabio0, fabio2, data_range=1, win_size=5)
# print('ssim =', out)