Spaces:
Build error
Build error
File size: 5,335 Bytes
9afbaed | 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 |
import os, sys
import tensorflow as tf
import tensorlayer as tl
import numpy as np
import network, img_io
import time
eps = 1e-5
def print_(str, color='', bold=False):
if color == 'w':
sys.stdout.write('\033[93m')
elif color == "e":
sys.stdout.write('\033[91m')
elif color == "m":
sys.stdout.write('\033[95m')
if bold:
sys.stdout.write('\033[1m')
sys.stdout.write(str)
sys.stdout.write('\033[0m')
sys.stdout.flush()
# Settings, using TensorFlow arguments
FLAGS = tf.flags.FLAGS
tf.flags.DEFINE_integer("width", "1024", "Reconstruction image width")
tf.flags.DEFINE_integer("height", "768", "Reconstruction image height")
tf.flags.DEFINE_string("im_dir", "Input_Dir", "Path to image directory or an individual image")
tf.flags.DEFINE_string("out_dir", "Output_Dir", "Path to output directory")
tf.flags.DEFINE_string("params", "hdrcnn_params.npz", "Path to trained CNN weights")
tf.flags.DEFINE_float("scaling", "1.0", "Pre-scaling, which is followed by clipping, in order to remove compression artifacts close to highlights")
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.")
# Round to be multiple of 32, so that autoencoder pooling+upsampling
# yields same size as input image
sx = int(np.maximum(32, np.round(FLAGS.width/32.0)*32))
sy = int(np.maximum(32, np.round(FLAGS.height/32.0)*32))
if sx != FLAGS.width or sy != FLAGS.height:
print_("Warning: ", 'w', True)
print_("prediction size has been changed from %dx%d pixels to %dx%d\n"%(FLAGS.width, FLAGS.height, sx, sy), 'w')
print_(" pixels, to comply with autoencoder pooling and up-sampling.\n\n", 'w')
# Info
print_("\n\n\t-------------------------------------------------------------------\n", 'm')
print_("\t HDR image reconstruction from a single exposure using deep CNNs\n\n", 'm')
print_("\t Prediction settings\n", 'm')
print_("\t -------------------\n", 'm')
print_("\t Input image directory/file: %s\n" % FLAGS.im_dir, 'm')
print_("\t Output directory: %s\n" % FLAGS.out_dir, 'm')
print_("\t CNN weights: %s\n" % FLAGS.params, 'm')
print_("\t Prediction resolution: %dx%d pixels\n" % (sx, sy), 'm')
if FLAGS.scaling > 1.0:
print_("\t Pre-scaling: %0.4f\n" % FLAGS.scaling, 'm')
if FLAGS.gamma > 1.0 + eps or FLAGS.gamma < 1.0 - eps:
print_("\t Gamma: %0.4f\n" % FLAGS.gamma, 'm')
print_("\t-------------------------------------------------------------------\n\n\n", 'm')
# Single frame
frames = [FLAGS.im_dir]
# If directory is supplied, get names of all files in the path
if os.path.isdir(FLAGS.im_dir):
frames = [os.path.join(FLAGS.im_dir, name)
for name in sorted(os.listdir(FLAGS.im_dir))
if os.path.isfile(os.path.join(FLAGS.im_dir, name))]
# Placeholder for image input
x = tf.placeholder(tf.float32, shape=[1, sy, sx, 3])
# HDR reconstruction autoencoder model
print_("Network setup:\n")
net = network.model(x)
# The CNN prediction (this also includes blending with input image x)
y = network.get_final(net, x)
# TensorFlow session for running inference
sess = tf.InteractiveSession()
# Load trained CNN weights
print_("\nLoading trained parameters from '%s'..."%FLAGS.params)
load_params = tl.files.load_npz(name=FLAGS.params)
tl.files.assign_params(sess, load_params, net)
print_("\tdone\n")
if not os.path.exists(FLAGS.out_dir):
os.makedirs(FLAGS.out_dir)
print_("\nStarting prediction...\n\n")
k = 0
for i in range(len(frames)):
print("Frame %d: '%s'"%(i,frames[i]))
try:
# Read frame
print_("\tReading...")
x_buffer = img_io.readLDR(frames[i], (sy,sx), True, FLAGS.scaling)
print_("\tdone")
print_("\t(Saturation: %0.2f%%)\n" % (100.0*(x_buffer>=1).sum()/x_buffer.size), 'm')
# Run prediction.
# The gamma value is used to allow for boosting/reducing the intensity of
# the reconstructed highlights. If y = f(x) is the reconstruction, the gamma
# g alters this according to y = f(x^(1/g))^g
print_("\tInference...")
feed_dict = {x: np.power(np.maximum(x_buffer, 0.0), 1.0/FLAGS.gamma)}
y_predict = sess.run([y], feed_dict=feed_dict)
y_predict = np.power(np.maximum(y_predict, 0.0), FLAGS.gamma)
print_("\tdone\n")
# Gamma corrected output
y_gamma = np.power(np.maximum(y_predict, 0.0), 0.5)
# Write to disc
print_("\tWriting...")
k += 1;
img_io.writeLDR(x_buffer, '%s/%06d_in.png' % (FLAGS.out_dir, k), -3)
img_io.writeLDR(y_gamma, '%s/%06d_out.png' % (FLAGS.out_dir, k), -3)
#img_io.writeEXR(y_predict, '%s/%06d_out.exr' % (FLAGS.out_dir, k))
print_("\tdone\n")
except img_io.IOException as e:
print_("\n\t\tWarning! ", 'w', True)
print_("%s\n"%e, 'w')
except Exception as e:
print_("\n\t\tError: ", 'e', True)
print_("%s\n"%e, 'e')
print_("Done!\n")
print_("-------------------------------------------------------------------\n\n\n", 'm')
print_("\tCalculating Confusion Matrix...\n")
time.sleep(10)
import Confusion_Matrix
sess.close()
|