`Peak Signal to Noise Ratio `_. .. math:: PSNR = 20 \cdot \log_{10}(MAX_p) - 10 \cdot \log_{10}(MSE) Args: prediction: a :class:`tf.Tensor` representing the prediction signal. ground_truth: another :class:`t
(prediction, ground_truth, maxp=None, name='psnr')
| 37 | |
| 38 | # don't hurt to leave it here |
| 39 | def psnr(prediction, ground_truth, maxp=None, name='psnr'): |
| 40 | """`Peak Signal to Noise Ratio <https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio>`_. |
| 41 | |
| 42 | .. math:: |
| 43 | |
| 44 | PSNR = 20 \cdot \log_{10}(MAX_p) - 10 \cdot \log_{10}(MSE) |
| 45 | |
| 46 | Args: |
| 47 | prediction: a :class:`tf.Tensor` representing the prediction signal. |
| 48 | ground_truth: another :class:`tf.Tensor` with the same shape. |
| 49 | maxp: maximum possible pixel value of the image (255 in in 8bit images) |
| 50 | |
| 51 | Returns: |
| 52 | A scalar tensor representing the PSNR |
| 53 | """ |
| 54 | |
| 55 | maxp = float(maxp) |
| 56 | |
| 57 | def log10(x): |
| 58 | with tf.name_scope("log10"): |
| 59 | numerator = tf.log(x) |
| 60 | denominator = tf.log(tf.constant(10, dtype=numerator.dtype)) |
| 61 | return numerator / denominator |
| 62 | |
| 63 | mse = tf.reduce_mean(tf.square(prediction - ground_truth)) |
| 64 | if maxp is None: |
| 65 | psnr = tf.multiply(log10(mse), -10., name=name) |
| 66 | else: |
| 67 | psnr = tf.multiply(log10(mse), -10.) |
| 68 | psnr = tf.add(tf.multiply(20., log10(maxp)), psnr, name=name) |
| 69 | |
| 70 | return psnr |