pytorch-enhance
pytorch-enhance copied to clipboard
Attempts to calc psnr independently result in differences
I am using greyscale pngs which have been contrast stretched to have pixel values in range 0-255. I have a value for test_psnr, but when I independently calculate this value I get a different result. However I note that in your implementation of PSNR the max_val is set to 1, whereas elsewhere implementations use 255. Also in the enhance
method you multiply by 255 then clip to 0,255, which is at odds with using max_val is set to 1? My functions are below: can you advise what is the issue resulting in the differences?
def contrast_stretch(img: np.array) -> np.array:
"""Contrast stretch an image
Return pixels in range 0 to 255.
Parameters
----------
img : np.array
Image to be contrast stretched
Returns
-------
np.array
Contrast stretched image
"""
img_min = img.min()
img_max = img.max()
return (img - img_min) / (img_max - img_min)*255
def psnr(img1: np.array, img2: np.array, max_value: float = 255.0) -> float:
"""
Compute the PSNR between two images.
For 8bit greyscale max value is 255 and min is zero.
"""
mse = np.mean((img1 - img2) ** 2)
return round(20 * math.log10(max_value / math.sqrt(mse)), 1)
Thanks for catching the bug in the enhance
method. I haven't updated this in a while but I was planning on scrapping the metrics and using torchmetrics instead (i.e. SSIM, MSSIM, MSE, PSNR) since they work in distributed training settings.
There are also many exotic metrics in https://piq.readthedocs.io/en/latest/overview.html
Nice find. I haven't seen that library before.
I think I'm going to just refactor and remove the metrics and recommend users user piq or torchmetrics.