-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_network.py
171 lines (152 loc) · 7.06 KB
/
test_network.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
# coding: utf-8
'''
test and prediction results
'''
import argparse, os, math,glob
import numpy
from PIL import Image
import piexif
import cv2
import chainer
from chainer import cuda
from chainer import serializers
import network
import scipy.io as scio
import datetime
parser = argparse.ArgumentParser(description='')
parser.add_argument('-i', help='File path of input image.', default='./testing_samples')
parser.add_argument('-o', help='Output directory.', default='./testing_samples')
parser.add_argument('-gpu', help='GPU device specifier. Two GPU devices must be specified, such as 0,1.', default='0')
parser.add_argument('-dm', help='File path of a downexposure model.', default='./models/downexposure_model_2.chainer')
parser.add_argument('-um', help='File path of a upexposure model.', default='./models/upexposure_model_2.chainer')
parser.add_argument('-al', help='Output directory.', default='0.6')
args = parser.parse_args()
start = datetime.datetime.now()
alpha = numpy.array(args.al).astype(numpy.float32)
dir_path_list = glob.glob(args.i+'/*')
dir_path_list = dir_path_list[:]
dir_outpath = glob.glob(args.o)
model_path_list = [args.dm, args.um]
base_outdir_path = args.o
gpu_list = []
if args.gpu != '-1':
for gpu_num in (args.gpu).split(','):
gpu_list.append(int(gpu_num))
'Estimate up-/donwn-exposed images'
model_list = [network.CNNAE3D512(), network.CNNAE3D512()]
xp = cuda.cupy if len(gpu_list) > 0 else numpy
if len(gpu_list) > 0:
cuda.check_cuda_available()
cuda.get_device().use()
for i in range(2):
model_list[i].to_gpu()
serializers.load_npz(model_path_list[i], model_list[i])
else:
for i in range(2):
serializers.load_npz(model_path_list[i], model_list[i])
def estimate_images(input_img, model):
#
model.train_dropout = False
input_img_ = (input_img.astype(numpy.float32)/255.).transpose(2,0,1)
input_img_ = chainer.Variable(xp.array([input_img_]))
res = model(input_img_).data[0]
if len(gpu_list)>0:
res = cuda.to_cpu(res)
out_img_list = list()
for i in range(res.shape[1]):
if i ==0:
out_img = (res[:,i,:,:].transpose(1,2,0)).astype(numpy.float)
else:
out_img = (255.*res[:,i,:,:].transpose(1,2,0)).astype(numpy.uint8)
out_img_list.append(out_img)
return out_img_list
print('\nStarting prediction...\n\n')
N = len(dir_path_list)
for i in range (N):
dir_path = dir_path_list[i]
frames = [glob.glob(dir_path + '/LDR/1.png')[0], glob.glob(dir_path + '/LDR/4.png')[0], glob.glob(dir_path + '/LDR/7.png')[0]]
frame_H = [glob.glob(dir_path + '/HDR/1.hdr')[0]]
HDR_Ground = cv2.imread(frame_H[0], flags=cv2.IMREAD_ANYDEPTH)
filename_root = os.path.basename(dir_path)
print('filename',filename_root)
save_path = dir_outpath[0] + '/' + filename_root + '/result'
if not os.path.exists(save_path):
os.makedirs(save_path)
cv2.imwrite(save_path + '/HDR_Ground.hdr', HDR_Ground)
print('\tReading...')
for ii in range (len(frames)):
img = cv2.imread(frames[ii])
img_zeros = numpy.zeros(numpy.shape(img)).astype(numpy.float)
out_img_list = list()
if len(gpu_list)>0:
cuda.get_device().use()
for i in range(2):
out_img_list.extend(estimate_images(img, model_list[i]))
if i == 0:
out_img_list.reverse()
out_img_list.append(img)
else:
for i in range(2):
out_img_list.extend(estimate_images(img, model_list[i]))
if i == 0:
out_img_list.reverse()
out_img_list.append(img)
out_img_list[8] = ((numpy.array(out_img_list[6], dtype=float)+numpy.array(out_img_list[10], dtype=float))/2).astype(numpy.uint8)
prev_img_log_mean = (out_img_list[7].astype(numpy.float32)+out_img_list[9].astype(numpy.float32))*3-5
pre_img_hdr = numpy.power(10, prev_img_log_mean)
'Select and Merge'
del out_img_list[9]
del out_img_list[7]
threshold = 32
stid = 0
prev_img = out_img_list[7].astype(numpy.float32)
out_img_list.reverse()
for out_img in out_img_list[8:]:
img = out_img.astype(numpy.float32)
if (img>(prev_img+threshold)).sum() > 0:
break
prev_img = img[:,:,:]
stid+=1
edid = 0
prev_img = out_img_list[7].astype(numpy.float32)
out_img_list.reverse()
for out_img in out_img_list[8:]:
img = out_img.astype(numpy.float32)
if (img<(prev_img-threshold)).sum() > 0:
break
prev_img = img[:,:,:]
edid+=1
out_img_list_ = out_img_list[7-stid:8+edid]
exposure_times = list()
lowest_exp_time = 1/32.
for i in range(len(out_img_list_)):
exposure_times.append(lowest_exp_time*math.pow(math.sqrt(2.),i))
exposure_times = numpy.array(exposure_times).astype(numpy.float32)
print('exposure_times.len',len(exposure_times))
merge_debvec = cv2.createMergeDebevec()
hdr_debvec = merge_debvec.process(out_img_list_, times=exposure_times.copy())
merge_final_debvec = alpha*pre_img_hdr+(1-alpha)*hdr_debvec/numpy.max(hdr_debvec)*numpy.max(pre_img_hdr)
if ii == 0:
cv2.imwrite(save_path+'/HDR_Log_1.hdr', pre_img_hdr)
scio.savemat(save_path+'/HDR_Log_1.mat',{'HDR_Log_1':pre_img_hdr})
cv2.imwrite(save_path+'/HDR_Debvec_1.hdr', hdr_debvec)
scio.savemat(save_path+'/HDR_Debvec_1.mat',{'HDR_Debvec_1':hdr_debvec})
cv2.imwrite(save_path + '/HDR_HybridNet_1.hdr', merge_final_debvec)
scio.savemat(save_path+'/HDR_HybridNet_1.mat',{'HDR_HybridNet_1':merge_final_debvec})
elif ii == 1:
cv2.imwrite(save_path+'/HDR_Log_4.hdr', pre_img_hdr)
scio.savemat(save_path+'/HDR_Log_4.mat',{'HDR_Log_4':pre_img_hdr})
cv2.imwrite(save_path+'/HDR_Debvec_4.hdr', hdr_debvec)
scio.savemat(save_path+'/HDR_Debvec_4.mat',{'HDR_Debvec_4':hdr_debvec})
cv2.imwrite(save_path + '/HDR_HybridNet_4.hdr', merge_final_debvec)
scio.savemat(save_path+'/HDR_HybridNet_4.mat',{'HDR_HybridNet_4':merge_final_debvec})
elif ii == 2:
cv2.imwrite(save_path+'/HDR_Log_7.hdr', pre_img_hdr)
scio.savemat(save_path+'/HDR_Log_7.mat',{'HDR_Log_7':pre_img_hdr})
cv2.imwrite(save_path+'/HDR_Debvec_7.hdr', hdr_debvec)
scio.savemat(save_path+'/HDR_Debvec_7.mat',{'HDR_Debvec_7':hdr_debvec})
cv2.imwrite(save_path + '/HDR_HybridNet_7.hdr', merge_final_debvec)
scio.savemat(save_path+'/HDR_HybridNet_7.mat',{'HDR_HybridNet_7':merge_final_debvec})
# print('\tDone\n')
del out_img_list