-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
121 lines (99 loc) · 3.49 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
import gc
import os
import sys
import numpy as np
import tensorflow as tf
from PIL import Image
import scipy.io
def data_augmentation(image, mode):
if mode == 0:
# original
return image
elif mode == 1:
# flip up and down
return np.flipud(image)
elif mode == 2:
# rotate counterwise 90 degree
return np.rot90(image)
elif mode == 3:
# rotate 90 degree and flip up and down
image = np.rot90(image)
return np.flipud(image)
elif mode == 4:
# rotate 180 degree
return np.rot90(image, k=2)
elif mode == 5:
# rotate 180 degree and flip
image = np.rot90(image, k=2)
return np.flipud(image)
elif mode == 6:
# rotate 270 degree
return np.rot90(image, k=3)
elif mode == 7:
# rotate 270 degree and flip
image = np.rot90(image, k=3)
return np.flipud(image)
class train_data():
def __init__(self, filepath='./data/image_clean_pat.npy'):
self.filepath = filepath
assert '.npy' in filepath
if not os.path.exists(filepath):
print("[!] Data file not exists")
sys.exit(1)
def __enter__(self):
print("[*] Loading data...")
self.data = np.load(self.filepath)
#np.random.shuffle(self.data)
print("[*] Load successfully...")
return self.data
def __exit__(self, type, value, trace):
del self.data
gc.collect()
print("In __exit__()")
def load_data(filepath='./data/image_clean_pat.npy'):
return train_data(filepath=filepath)
def load_images(filelist):
# pixel value range 0-255
if not isinstance(filelist, list):
im = Image.open(filelist).convert('RGB')
return np.array(im).reshape(1, im.size[1], im.size[0], 3)
data = []
for file in filelist:
im = Image.open(file).convert('RGB')
data.append(np.array(im).reshape(1, im.size[1], im.size[0], 3))
return data
def save_map(filepath, map):
# assert the pixel value range is 0-255
map = np.squeeze(map)
im = Image.fromarray(map.astype('uint8'))
im.save(filepath, 'png')
def save_images(filepath, ground_truth, noisy_image=None, clean_image=None):
# assert the pixel value range is 0-255
ground_truth = np.squeeze(ground_truth)
noisy_image = np.squeeze(noisy_image)
clean_image = np.squeeze(clean_image)
if not clean_image.any():
cat_image = ground_truth
else:
cat_image = np.concatenate([ground_truth, noisy_image, clean_image], axis=1)
im = Image.fromarray(cat_image.astype('uint8')).convert('RGB')
im.save(filepath, 'png')
def save_mat(filepath, ground_truth, noisy_image=None, clean_image=None):
# assert the pixel value range is 0-255
ground_truth = np.squeeze(ground_truth)
noisy_image = np.squeeze(noisy_image)
clean_image = np.squeeze(clean_image)
if not clean_image.any():
cat_image = ground_truth
else:
cat_image = np.concatenate([ground_truth, noisy_image, clean_image], axis=1)
scipy.io.savemat(filepath+'.mat', mdict={'mask': cat_image})
def cal_psnr(im1, im2):
# assert pixel value range is 0-255 and type is uint8
mse = ((im1.astype(np.float) - im2.astype(np.float)) ** 2).mean()
psnr = 10 * np.log10(255 ** 2 / mse)
return psnr
def tf_psnr(im1, im2):
# assert pixel value range is 0-1
mse = tf.losses.mean_squared_error(labels=im2 * 255.0, predictions=im1 * 255.0)
return 10.0 * (tf.log(255.0 ** 2 / mse) / tf.log(10.0))