alexanderkroner / saliency

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

Tensorflow Version 2.4.1 #15

Closed akki83 closed 2 years ago

akki83 commented 3 years ago

This code is not executing on TF V 2.4.1

alexanderkroner commented 3 years ago

Hey, that's right, I only trained and tested the models with TF1.13.1. However, it's possible to convert the pre-trained models to TF2 if that's something you're interested in. Just let me know!

akki83 commented 3 years ago

Yes, indeed I am very much interested.

On Fri, Jun 18, 2021 at 2:02 PM Alexander Kroner @.***> wrote:

Hey, that's right, I only trained and tested the models with TF1.13.1. However, it's possible to convert the pre-trained models to TF2 if that's something you're interested in. Just let me know!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/alexanderkroner/saliency/issues/15#issuecomment-863862564, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE7FFX5D4FSHJPGQUDPPPE3TTMABFANCNFSM463N6D2Q .

--

Thanks & Regards... Dr. Akshansh Gupta Scientist, *Intelligent System Group, CSIR-CEERI-Pilani Rajasthan 333031 Alternative E-mail **@. @.>*

https://www.ceeri.res.in/profiles/akshansh-gupta/ https://www.ceeri.res.in/profiles/akshansh-gupta/Contact no: +91 9013927309, 9899435903 https://scholar.google.co.in/citations?user=T6HIeewAAAAJ&hl=en I am Born With Potential, I am Born With Idea, I am Born With Greatness, I am Born With Courage, I Will Defeat The Problem and Succeed, I am Not For Crawling Because I Will Fly. (Dr. Abdul Kalam)

alexanderkroner commented 3 years ago

Here's a script to convert a model trained with TF1 to TF2. First, you need to download the model you're interested in from here, e.g. model_salicon_cpu.pb. Then, you can execute this script using TF2 to convert it:

import tensorflow as tf
from tensorflow.python.saved_model import signature_constants, tag_constants

export_dir = "./converted_model"
graph_pb = "./model_salicon_cpu.pb"

with tf.io.gfile.GFile(graph_pb, "rb") as f:
    graph_def = tf.compat.v1.GraphDef()
    graph_def.ParseFromString(f.read())

sig = {}

builder = tf.compat.v1.saved_model.Builder(export_dir)

with tf.compat.v1.Session(graph=tf.Graph()) as sess:
    tf.import_graph_def(graph_def, name="")
    g = tf.compat.v1.get_default_graph()

    input = g.get_tensor_by_name("input:0")
    output = g.get_tensor_by_name("output:0")

    sig_key = signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
    sig[sig_key] = tf.compat.v1.saved_model.predict_signature_def({"input": input},
                                                                  {"output": output})
    builder.add_meta_graph_and_variables(sess,
                                         [tag_constants.SERVING],
                                         signature_def_map=sig)
builder.save()

Once this is done, you can test whether the conversion was successful:

import tensorflow as tf

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

input_tensor = tf.ones((1, 240, 320, 3))
saliency = imported(input_tensor)["output"].numpy()

This should return a saliency map of size (1, 240, 320, 1). I hope this helps!