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|
| 5 |
+
import tensorlayer as tl
|
| 6 |
+
import numpy as np
|
| 7 |
+
import network, img_io
|
| 8 |
+
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
eps = 1e-5
|
| 13 |
+
|
| 14 |
+
def print_(str, color='', bold=False):
|
| 15 |
+
if color == 'w':
|
| 16 |
+
sys.stdout.write('\033[93m')
|
| 17 |
+
elif color == "e":
|
| 18 |
+
sys.stdout.write('\033[91m')
|
| 19 |
+
elif color == "m":
|
| 20 |
+
sys.stdout.write('\033[95m')
|
| 21 |
+
|
| 22 |
+
if bold:
|
| 23 |
+
sys.stdout.write('\033[1m')
|
| 24 |
+
|
| 25 |
+
sys.stdout.write(str)
|
| 26 |
+
sys.stdout.write('\033[0m')
|
| 27 |
+
sys.stdout.flush()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Settings, using TensorFlow arguments
|
| 31 |
+
FLAGS = tf.flags.FLAGS
|
| 32 |
+
tf.flags.DEFINE_integer("width", "1024", "Reconstruction image width")
|
| 33 |
+
tf.flags.DEFINE_integer("height", "768", "Reconstruction image height")
|
| 34 |
+
tf.flags.DEFINE_string("im_dir", "Input_Dir", "Path to image directory or an individual image")
|
| 35 |
+
tf.flags.DEFINE_string("out_dir", "Output_Dir", "Path to output directory")
|
| 36 |
+
tf.flags.DEFINE_string("params", "hdrcnn_params.npz", "Path to trained CNN weights")
|
| 37 |
+
tf.flags.DEFINE_float("scaling", "1.0", "Pre-scaling, which is followed by clipping, in order to remove compression artifacts close to highlights")
|
| 38 |
+
tf.flags.DEFINE_float("gamma", "1.0", "Gamma/exponential curve applied before, and inverted after, prediction. This can be used to control the boost of reconstructed pixels.")
|
| 39 |
+
|
| 40 |
+
# Round to be multiple of 32, so that autoencoder pooling+upsampling
|
| 41 |
+
# yields same size as input image
|
| 42 |
+
sx = int(np.maximum(32, np.round(FLAGS.width/32.0)*32))
|
| 43 |
+
sy = int(np.maximum(32, np.round(FLAGS.height/32.0)*32))
|
| 44 |
+
if sx != FLAGS.width or sy != FLAGS.height:
|
| 45 |
+
print_("Warning: ", 'w', True)
|
| 46 |
+
print_("prediction size has been changed from %dx%d pixels to %dx%d\n"%(FLAGS.width, FLAGS.height, sx, sy), 'w')
|
| 47 |
+
print_(" pixels, to comply with autoencoder pooling and up-sampling.\n\n", 'w')
|
| 48 |
+
|
| 49 |
+
# Info
|
| 50 |
+
print_("\n\n\t-------------------------------------------------------------------\n", 'm')
|
| 51 |
+
print_("\t HDR image reconstruction from a single exposure using deep CNNs\n\n", 'm')
|
| 52 |
+
print_("\t Prediction settings\n", 'm')
|
| 53 |
+
print_("\t -------------------\n", 'm')
|
| 54 |
+
print_("\t Input image directory/file: %s\n" % FLAGS.im_dir, 'm')
|
| 55 |
+
print_("\t Output directory: %s\n" % FLAGS.out_dir, 'm')
|
| 56 |
+
print_("\t CNN weights: %s\n" % FLAGS.params, 'm')
|
| 57 |
+
print_("\t Prediction resolution: %dx%d pixels\n" % (sx, sy), 'm')
|
| 58 |
+
if FLAGS.scaling > 1.0:
|
| 59 |
+
print_("\t Pre-scaling: %0.4f\n" % FLAGS.scaling, 'm')
|
| 60 |
+
if FLAGS.gamma > 1.0 + eps or FLAGS.gamma < 1.0 - eps:
|
| 61 |
+
print_("\t Gamma: %0.4f\n" % FLAGS.gamma, 'm')
|
| 62 |
+
print_("\t-------------------------------------------------------------------\n\n\n", 'm')
|
| 63 |
+
|
| 64 |
+
# Single frame
|
| 65 |
+
frames = [FLAGS.im_dir]
|
| 66 |
+
|
| 67 |
+
# If directory is supplied, get names of all files in the path
|
| 68 |
+
if os.path.isdir(FLAGS.im_dir):
|
| 69 |
+
frames = [os.path.join(FLAGS.im_dir, name)
|
| 70 |
+
for name in sorted(os.listdir(FLAGS.im_dir))
|
| 71 |
+
if os.path.isfile(os.path.join(FLAGS.im_dir, name))]
|
| 72 |
+
|
| 73 |
+
# Placeholder for image input
|
| 74 |
+
x = tf.placeholder(tf.float32, shape=[1, sy, sx, 3])
|
| 75 |
+
|
| 76 |
+
# HDR reconstruction autoencoder model
|
| 77 |
+
print_("Network setup:\n")
|
| 78 |
+
net = network.model(x)
|
| 79 |
+
|
| 80 |
+
# The CNN prediction (this also includes blending with input image x)
|
| 81 |
+
y = network.get_final(net, x)
|
| 82 |
+
|
| 83 |
+
# TensorFlow session for running inference
|
| 84 |
+
sess = tf.InteractiveSession()
|
| 85 |
+
|
| 86 |
+
# Load trained CNN weights
|
| 87 |
+
print_("\nLoading trained parameters from '%s'..."%FLAGS.params)
|
| 88 |
+
load_params = tl.files.load_npz(name=FLAGS.params)
|
| 89 |
+
tl.files.assign_params(sess, load_params, net)
|
| 90 |
+
print_("\tdone\n")
|
| 91 |
+
|
| 92 |
+
if not os.path.exists(FLAGS.out_dir):
|
| 93 |
+
os.makedirs(FLAGS.out_dir)
|
| 94 |
+
|
| 95 |
+
print_("\nStarting prediction...\n\n")
|
| 96 |
+
k = 0
|
| 97 |
+
for i in range(len(frames)):
|
| 98 |
+
print("Frame %d: '%s'"%(i,frames[i]))
|
| 99 |
+
|
| 100 |
+
try:
|
| 101 |
+
# Read frame
|
| 102 |
+
print_("\tReading...")
|
| 103 |
+
x_buffer = img_io.readLDR(frames[i], (sy,sx), True, FLAGS.scaling)
|
| 104 |
+
print_("\tdone")
|
| 105 |
+
|
| 106 |
+
print_("\t(Saturation: %0.2f%%)\n" % (100.0*(x_buffer>=1).sum()/x_buffer.size), 'm')
|
| 107 |
+
|
| 108 |
+
# Run prediction.
|
| 109 |
+
# The gamma value is used to allow for boosting/reducing the intensity of
|
| 110 |
+
# the reconstructed highlights. If y = f(x) is the reconstruction, the gamma
|
| 111 |
+
# g alters this according to y = f(x^(1/g))^g
|
| 112 |
+
print_("\tInference...")
|
| 113 |
+
feed_dict = {x: np.power(np.maximum(x_buffer, 0.0), 1.0/FLAGS.gamma)}
|
| 114 |
+
y_predict = sess.run([y], feed_dict=feed_dict)
|
| 115 |
+
y_predict = np.power(np.maximum(y_predict, 0.0), FLAGS.gamma)
|
| 116 |
+
print_("\tdone\n")
|
| 117 |
+
|
| 118 |
+
# Gamma corrected output
|
| 119 |
+
y_gamma = np.power(np.maximum(y_predict, 0.0), 0.5)
|
| 120 |
+
|
| 121 |
+
# Write to disc
|
| 122 |
+
print_("\tWriting...")
|
| 123 |
+
k += 1;
|
| 124 |
+
img_io.writeLDR(x_buffer, '%s/%06d_in.png' % (FLAGS.out_dir, k), -3)
|
| 125 |
+
img_io.writeLDR(y_gamma, '%s/%06d_out.png' % (FLAGS.out_dir, k), -3)
|
| 126 |
+
#img_io.writeEXR(y_predict, '%s/%06d_out.exr' % (FLAGS.out_dir, k))
|
| 127 |
+
print_("\tdone\n")
|
| 128 |
+
|
| 129 |
+
except img_io.IOException as e:
|
| 130 |
+
print_("\n\t\tWarning! ", 'w', True)
|
| 131 |
+
print_("%s\n"%e, 'w')
|
| 132 |
+
except Exception as e:
|
| 133 |
+
print_("\n\t\tError: ", 'e', True)
|
| 134 |
+
print_("%s\n"%e, 'e')
|
| 135 |
+
|
| 136 |
+
print_("Done!\n")
|
| 137 |
+
|
| 138 |
+
print_("-------------------------------------------------------------------\n\n\n", 'm')
|
| 139 |
+
|
| 140 |
+
print_("\tCalculating Confusion Matrix...\n")
|
| 141 |
+
time.sleep(10)
|
| 142 |
+
|
| 143 |
+
import Confusion_Matrix
|
| 144 |
+
|
| 145 |
+
sess.close()
|
| 146 |
+
|