Commit f0017ad5 authored by Patrick Wieschollek's avatar Patrick Wieschollek Committed by Yuxin Wu

add max_p to psnr (#240)

* add max_p to psnr

* Update symbolic_functions.py

* fix liniting and remove name_scope
parent 757f5a39
...@@ -149,31 +149,38 @@ def get_scalar_var(name, init_value, summary=False, trainable=False): ...@@ -149,31 +149,38 @@ def get_scalar_var(name, init_value, summary=False, trainable=False):
return ret return ret
def psnr(prediction, ground_truth, name='psnr'): def psnr(prediction, ground_truth, maxp=None, name='psnr'):
"""`Peek Signal to Noise Ratio <https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio>`_. """`Peek Signal to Noise Ratio <https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio>`_.
.. math:: .. math::
PSNR = 20 \cdot \log_{10}(MAX_p) - 10 \cdot \log_{10}(MSE) PSNR = 20 \cdot \log_{10}(MAX_p) - 10 \cdot \log_{10}(MSE)
This function assumes the maximum possible value of the signal is 1,
therefore the PSNR is simply :math:`- 10 \cdot \log_{10}(MSE)`.
Args: Args:
prediction: a :class:`tf.Tensor` representing the prediction signal. prediction: a :class:`tf.Tensor` representing the prediction signal.
ground_truth: another :class:`tf.Tensor` with the same shape. ground_truth: another :class:`tf.Tensor` with the same shape.
maxp: maximum possible pixel value of the image (255 in in 8bit images)
Returns: Returns:
A scalar tensor representing the PSNR. A scalar tensor representing the PSNR.
""" """
def log10(x): maxp = float(maxp)
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
return tf.multiply(log10(tf.reduce_mean(tf.square(prediction - ground_truth))), def log10(x):
-10., name=name) with tf.name_scope("log10"):
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
mse = tf.reduce_mean(tf.square(prediction - ground_truth))
if maxp is None:
psnr = tf.multiply(log10(mse), -10., name=name)
else:
psnr = tf.multiply(log10(mse), -10.)
psnr = tf.add(tf.multiply(20., log10(maxp)), psnr, name=name)
return psnr
@contextmanager @contextmanager
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment