rgcda / haarpsi

The Haar wavelet-based perceptual similarity index (HaarPSI) is a similarity measure for images that aims to correctly assess the perceptual similarity between two images with respect to a human viewer.
MIT License
35 stars 5 forks source link

tensorflow dependency #1

Closed nareto closed 5 years ago

nareto commented 5 years ago

Hello, why is tensorflow used in the python file? As far as I can see it is used only for numerical operations that are already available in numpy. I find it bad practice to rely on it.

lecode-official commented 5 years ago

Hi @nareto,

TensorFlow is not used for some numerical operations, but there are two separate implementations of the HaarPSI algorithm in the Python implementation: one using NumPy and one using TensorFlow. Furthermore, TensorFlow is only a soft dependency, i.e. it is only loaded when it is installed on the system. If it is not available, then only the NumPy implementation will ever be used. Compare lines 19-24:

try:
    os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
    import tensorflow as tf
    is_tensorflow_available = True
except ImportError:
    is_tensorflow_available = False

The haar_psi method checks if the reference image and the distorted image are NumPy ndarrays or TensorFlow Tensors. Depending on the input the correct implementation (haar_psi_numpy or haar_psi_tensorflow) is used. If you think that is too much overhead, you may call the NumPy implementation directly: haar_psi_numpy. In that case no TensorFlow code is executed whatsoever.

The reason for the two separate implementations is, that I wanted to report HaarPSI values for images generated in my neural network. Rather than copying the tensors from the GPU, converting them to NumPy and making the calculation, I figured I could directly perform the operations on the GPU, which is much faster in that particular case.

I hope this helps.

Cheers David

nareto commented 5 years ago

well, that makes perfect sense. I think I incurred in this error yesterday because there was a bug in my code, which passed to haarpsi a string instead of a numpy array. I'm not sure now if that was exactly the case, but anyways I raised the exception saying I needed tensorflow installed, and acted a little too quickly.