-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
103 lines (84 loc) · 3.62 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
import numpy as np
import os
from skimage.measure import compare_ssim
from matplotlib import pyplot as plt
from skimage import color
import tensorflow as tf
from scipy.interpolate import interp2d
from functools import reduce
from operator import mul
import h5py
def load_h5_data(file):
with h5py.File(file, 'r') as hf:
data = np.array(hf.get('data'))
label = np.array(hf.get('label'))
data = np.transpose(data, (0, 3, 2, 1))
label = np.transpose(label, (0, 3, 2, 1))
return data, label
def get_num_params(name=None):
num_params = 0
for variable in tf.trainable_variables():
if variable.name.startswith(name):
shape = variable.get_shape()
num_params += reduce(mul, [dim.value for dim in shape], 1)
return num_params
def mkdir(path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
def load4DLF(file_name, s, t, ang_start, ang_ori, ang_in, tone_coef, pyramid):
scale = (ang_ori - 1) // (ang_in - 1)
try:
input_lf = plt.imread(file_name + ".png")
except:
input_lf = plt.imread(file_name + ".jpg")
input_lf = np.float32(input_lf[:, :, :3])
else:
input_lf = plt.imread(file_name + ".png")
input_lf = np.float32(input_lf[:, :, :3])
# Adjust tone
input_lf = np.power(input_lf, 1 / tone_coef)
# input_lf = cv2.cvtColor(input_lf, cv2.COLOR_RGB2HSV)
input_lf = color.rgb2hsv(input_lf)
input_lf[:, :, 1:2] = input_lf[:, :, 1:2] * tone_coef
# input_lf = cv2.cvtColor(input_lf, cv2.COLOR_HSV2RGB)
input_lf = color.hsv2rgb(input_lf)
input_lf = np.minimum(np.maximum(input_lf, 0), 1)
hei = input_lf.shape[0] // t
wid = input_lf.shape[1] // s
chn = input_lf.shape[2]
full_lf = np.zeros(shape=(hei, wid, chn, t, s))
for ax in range(0, s):
temp = input_lf[:, np.arange(ax, input_lf.shape[1], s)]
for ay in range(0, t):
full_lf[:, :, :, ay, ax] = temp[np.arange(ay, input_lf.shape[0], t)]
hei = hei // (np.power(2, pyramid - 1)) * (np.power(2, pyramid - 1))
wid = wid // (np.power(2, pyramid - 1)) * (np.power(2, pyramid - 1))
full_lf = full_lf[0:hei, 0:wid, :, ang_start - 1: ang_start + ang_ori - 1, ang_start - 1: ang_start + ang_ori - 1]
input_lf = full_lf[:, :, :, np.arange(0, ang_ori, scale)]
input_lf = input_lf[:, :, :, :, np.arange(0, ang_ori, scale)]
return full_lf, input_lf
def rgb2ycbcr(x):
y = (24.966 * x[:, :, :, 2] + 128.553 * x[:, :, :, 1] + 65.481 * x[:, :, :, 0] + 16) / 255
cb = (112 * x[:, :, :, 2] - 74.203 * x[:, :, :, 1] - 37.797 * x[:, :, :, 0] + 128) / 255
cr = (-18.214 * x[:, :, :, 2] - 93.789 * x[:, :, :, 1] + 112 * x[:, :, :, 0] + 128) / 255
y = np.stack([y, cb, cr], axis=3)
return y
def ycbcr2rgb(x):
r = 1.16438356 * (x[:, :, :, 0] - 16 / 255) + 1.59602715 * (x[:, :, :, 2] - 128 / 255)
g = 1.16438356 * (x[:, :, :, 0] - 16 / 255) - 0.3917616 * (x[:, :, :, 1] - 128 / 255) - 0.81296805 * (
x[:, :, :, 2] - 128 / 255)
b = 1.16438356 * (x[:, :, :, 0] - 16 / 255) + 2.01723105 * (x[:, :, :, 1] - 128 / 255)
y = np.stack([r, g, b], axis=3)
return y
def metric(x, y, border_cut):
if border_cut > 0:
x = x[border_cut:-border_cut, border_cut:-border_cut, :]
y = y[border_cut:-border_cut, border_cut:-border_cut, :]
else:
x = x
y = y
x = 0.256788 * x[:, :, 0] + 0.504129 * x[:, :, 1] + 0.097906 * x[:, :, 2] + 16 / 255
y = 0.256788 * y[:, :, 0] + 0.504129 * y[:, :, 1] + 0.097906 * y[:, :, 2] + 16 / 255
mse = np.mean((x - y) ** 2)
return 20 * np.log10(1 / np.sqrt(mse)), compare_ssim(x, y)