alexanderkroner / saliency

Contextual Encoder-Decoder Network for Visual Saliency Prediction [Neural Networks 2020]
MIT License
172 stars 46 forks source link

Tensorflow v2.7 #24

Closed iboraham closed 2 years ago

iboraham commented 2 years ago

Hi

I'm trying to test your pretrained model on SALICON with my images. I used #15 this method to convert tf1 model to tf2 however, output seems not correct to me.

Here is my code,

import tensorflow as tf

imported = tf.saved_model.load("./converted_model")
imported = imported.signatures["serving_default"]

img = tf.io.read_file("face.jpg")
tensor = tf.io.decode_image(img, channels=3, dtype=tf.dtypes.float32)
#tensor = tf.image.resize(tensor, [240, 320])
input_tensor = tf.expand_dims(tensor, axis=0)
# input_tensor = tf.ones((1, 240, 320, 3))
saliency = imported(input_tensor)["output"]

tf.keras.preprocessing.image.save_img('face_out.jpg',saliency[0])

Here is the input image face

Here is the output image face_out

Thanks!

alexanderkroner commented 2 years ago

Hi! You see this incorrect output because the input to the model requires values in the range of [0, 255]. If you simply remove the dtype argument from the function decode_image, then this solves the issue! Another point you should consider is that the model ideally receives images of the size it was trained on. For the SALICON version, this is (240, 320).

imported = tf.saved_model.load("./converted_model")
imported = imported.signatures["serving_default"]

img = tf.io.read_file("face.jpg")
tensor = tf.io.decode_image(img, channels=3)

inference_shape = (240, 320)
original_shape = tensor.shape[:2]

input_tensor = tf.expand_dims(tensor, axis=0)

input_tensor = tf.image.resize(input_tensor, inference_shape,
                               preserve_aspect_ratio=True)

saliency = imported(input_tensor)["output"]

saliency = tf.image.resize(saliency, original_shape)
iboraham commented 2 years ago

That worked, thanks a lot!