-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathutils.py
97 lines (75 loc) · 2.83 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
from __future__ import division
import tensorflow as tf
import tensorflow.contrib.slim as slim
import imageio
import numpy as np
import os
import h5py
def check_folder(log_dir):
if not os.path.exists(log_dir):
os.makedirs(log_dir)
return log_dir
def show_all_variables():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
def str2bool(x):
return x.lower() in ('true')
def compute_psnr(img_orig, img_out, peak):
mse = np.mean(np.square(img_orig - img_out))
psnr = 10 * np.log10(peak*peak / mse)
return psnr
def read_mat_file(data_fname, label_fname, data_name, label_name):
# read training data (.mat file)
data_file = h5py.File(data_fname, 'r')
label_file = h5py.File(label_fname, 'r')
data = data_file[data_name][()]
label = label_file[label_name][()]
# change type & reorder
data = np.array(data, dtype=np.float32) / 255.
label = np.array(label, dtype=np.float32) / 1023.
data = np.swapaxes(data, 1, 3)
label = np.swapaxes(label, 1, 3)
print('[*] Success to read .mat file')
return data, label
def get_HW_boundary(patch_boundary, h, w, pH, sH, pW, sW):
H_low_ind = max(pH * sH - patch_boundary, 0)
H_high_ind = min((pH + 1) * sH + patch_boundary, h)
W_low_ind = max(pW * sW - patch_boundary, 0)
W_high_ind = min((pW + 1) * sW + patch_boundary, w)
return H_low_ind, H_high_ind, W_low_ind, W_high_ind
def trim_patch_boundary(img, patch_boundary, h, w, pH, sH, pW, sW, sf):
if patch_boundary == 0:
img = img
else:
if pH * sH < patch_boundary:
img = img
else:
img = img[:, patch_boundary*sf:, :, :]
if (pH + 1) * sH + patch_boundary > h:
img = img
else:
img = img[:, :-patch_boundary*sf, :, :]
if pW * sW < patch_boundary:
img = img
else:
img = img[:, :, patch_boundary*sf:, :]
if (pW + 1) * sW + patch_boundary > w:
img = img
else:
img = img[:, :, :-patch_boundary*sf, :]
return img
def save_results_yuv(pred, index, test_img_dir):
test_pred = np.squeeze(pred)
test_pred = np.clip(test_pred, 0, 1) * 1023
test_pred = np.uint16(test_pred)
# split image
pred_y = test_pred[:, :, 0]
pred_u = test_pred[:, :, 1]
pred_v = test_pred[:, :, 2]
# save prediction - must be saved in separate channels due to 16-bit pixel depth
imageio.imwrite(os.path.join(test_img_dir, "{}-y_pred.png".format(str(int(index) + 1).zfill(2))),
pred_y)
imageio.imwrite(os.path.join(test_img_dir, "{}-u_pred.png".format(str(int(index) + 1).zfill(2))),
pred_u)
imageio.imwrite(os.path.join(test_img_dir, "{}-v_pred.png".format(str(int(index) + 1).zfill(2))),
pred_v)