Issue of "type resource != float" when trying to get frozen graph_def from a model of saved_model format
Hi @zehao-intel 

The Value Error occurs  if input_map, or return_elements contains names that do not appear in graph_def, or graph_def is not well-formed (e.g. it refers to an unknown tensor).

Also, the Saved Model serialisation format is only supported and the TFLite conversion from frozen graph is deprecated.

Could you please try `from_saved_model` if you are trying convert a saved model from TFLite.

Thanks.



 Hi pjpratik，

Thanks for your explanation and suggestions. They are very helpful!
It seems that 'from_saved_model' only convert a saved_model to TFLite model.
I would like to get a correct graph_def from the saved_model. Is that possible to achieve this by using TFLite API? Hi @zehao-intel 

As per [documentation](https://www.tensorflow.org/lite/models/convert/convert_models), the TFLite API supports converting a TensorFlow model to a TensorFlow Lite mode using `saved_model` format which is [recommended](https://www.tensorflow.org/lite/models/convert/convert_models#convert_a_savedmodel_recommended_) by TFLite. 

Converting from `frozen_graph`  is no longer supported in TF 2.x but can be accessed using `tf.compat.v1.lite.TFLiteConverter.from_frozen_graph` . 

Thanks.

 Hi @pjpratik ,

I have tried ```from_saved_model``` with the following code:
```
import tensorflow as tf
import tensorflow_text

saved_model_dir = './universal-sentence-encoder-multilingual_3'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)
```
But an error occurs:
![image](https://user-images.githubusercontent.com/86036206/236790372-cebb768d-f822-4357-b336-416a8c832702.png)
It seems that some binary contents is printed and the execution is stopped at ```converter.convert()```.
Could you please look into it?

Thanks
 Hi @zehao-intel 

The TensorFlow Lite builtin operator library only supports a limited number of TensorFlow operators, not every model is convertible. To allow conversion, we can enable the usage of [certain TensorFlow ops](https://www.tensorflow.org/lite/guide/op_select_allowlist) in their TensorFlow Lite model by using following syntax

```
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
  tf.lite.OpsSet.SELECT_TF_OPS # enable TensorFlow ops.
]
```
Please find the working gist [here](https://colab.research.google.com/gist/pjpratik/173bdfd2355e539ed24c70352e110186/60416.ipynb) and let us know if it helps.

Thanks. Hi @pjpratik ,

Many thanks to your comments.
I have tried your code and inspected the generated model.tflite with netron. I believe the model is correctly converted.
By the way, I made some investigation. It seems that there is no official way to get graph_def from a TFLite model.
In fact, I still need to find out how to get graph_def from the original saved_model. I have tried many ways, including the tf lite code I mentioned before, but none of them works.
Could you give me some suggestions about this?

Thank! Hi @zehao-intel 

Getting a graph def from tflite is not supported. For getting a frozen graph from checkpoints you can try https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

An example of command-line usage is:
```
bazel build tensorflow/python/tools:freeze_graph && \
bazel-bin/tensorflow/python/tools/freeze_graph \
--input_graph=some_graph_def.pb \
--input_checkpoint=model.ckpt-8361242 \
--output_graph=/tmp/frozen_graph.pb --output_node_names=softmax

```
Thanks.

 Hi @pjpratik ,

It's happy to hear that there is a stable method to get  a ```frozen graph``` from ```checkpoints```.
But for this model, it is in ```saved_model``` format.
I saw some arguments related with ```saved_model``` in the script you provided.
Is there a similar usage of getting  a ```frozen graph``` from ```saved_model``` with this script?

Thanks Hi @zehao-intel 

Importing from tensorflow.python or any other modules not supported, and can break unannounced as anything under tf.python.* is private, intended for development only, rather than for public use.

As mentioned earlier, frozen graphs are deprecated and using Saved Model format is recommended.

Is there any challenge to use Saved Model in your use case?

Thanks. Hi @pjpratik,

Thanks for your explanation. So TensorFlow don't recommend to use ```frozen graphs``` in version 2.X.
The task I am handled is to apply 8bit quantization to the TF model mentioned above. And I need to firstly get the frozen graph from this model so that I can modify the model by change the Op type and fuse Op patterns. I am not able to apply this process to a ```saved_model```.

Thanks. Hi @zehao-intel 

Sorry for delayed response.

We can apply 8 bit quantization while converting the SavedModel to TFLite model by adding these flags:

```
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
```
Please refer to this [documentation](https://www.tensorflow.org/lite/performance/post_training_integer_quant#convert_using_integer-only_quantization_) on converting using integer only quantization and let us know if it helps.

Thanks. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Hi @pjpratik ，

Sorry for the late reply.
I think this Issue could be closed.
Really appreciate your patient comments! Thanks for the confirmation.

Closing this issue. Please feel free to reopen if you'd like to work on this further.
### 1. System information

- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): CentOS Linux release 7.6.1810 (Core)
- TensorFlow installation (pip package or built from source): pip install tensorflow
- TensorFlow library (version, if pip package or github SHA, if built from source): 2.12.0

### 2. Code

I am trying to use the code of this function to get frozen_graph_def from a TF model in saved_model format: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/python/convert_saved_model.py#L134

No error occurs during the process of conversion. But a bug is reported after I pass the frozen_graph_def to tf.import_graph_def()
```
import tensorflow_text
import tensorflow as tf
from convert_saved_model import freeze_saved_model
from tensorflow.python.saved_model import signature_constants

saved_model_dir = "./universal-sentence-encoder-multilingual_3"
tag_set = ['serve']
signature_key = signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
frozen_graph_def, in_tensors, out_tensors, graph = freeze_saved_model(saved_model_dir, input_arrays=None, input_shapes=None,
                       output_arrays=None, tag_set=tag_set, signature_key=signature_key)

tf.import_graph_def(frozen_graph_def, name='') 
```
The model is downloaded from this tfhub link: https://tfhub.dev/google/universal-sentence-encoder-multilingual/3
### 3. Failure after conversion
If the conversion is successful, but the generated model is wrong, then state what is wrong:

- Can't be loaded by using tf.import_graph_def() API.


### 5. (optional) Any other info / logs
The code is run on Intel CLX Xeon CPU.

The log of this bug:
```
2023-04-26 22:31:36.666526: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2023-04-26 22:31:36.668374: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-04-26 22:31:36.708706: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-04-26 22:31:36.709089: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-04-26 22:31:38.113729: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
WARNING:tensorflow:From /home/zehaohua/tensorflow/tensorflow/lite/python/convert_saved_model.py:42: load (from tensorflow.python.saved_model.loader_impl) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.saved_model.load` instead.
2023-04-26 22:31:40.329195: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled
2023-04-26 22:31:42.536067: I tensorflow/core/grappler/devices.cc:66] Number of eligible GPUs (core count >= 8, compute capability >= 0.0): 0
2023-04-26 22:31:42.536209: I tensorflow/core/grappler/clusters/single_machine.cc:358] Starting new session
WARNING:tensorflow:From /home/zehaohua/miniconda3/envs/muse/lib/python3.8/site-packages/tensorflow/lite/python/util.py:306: convert_variables_to_constants (from tensorflow.python.framework.convert_to_constants) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
WARNING:tensorflow:From /home/zehaohua/miniconda3/envs/muse/lib/python3.8/site-packages/tensorflow/python/framework/convert_to_constants.py:952: extract_sub_graph (from tensorflow.python.framework.graph_util_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This API was designed for TensorFlow v1. See https://www.tensorflow.org/guide/migrate for instructions on how to migrate your code to TensorFlow v2.
Traceback (most recent call last):
  File "/home/zehaohua/miniconda3/envs/muse/lib/python3.8/site-packages/tensorflow/python/framework/importer.py", line 510, in _import_graph_def_internal
    results = c_api.TF_GraphImportGraphDefWithResults(
tensorflow.python.framework.errors_impl.InvalidArgumentError: input resource[0] expected type resource != float, the type of embeddings_sharded_0[0]
        In {{node EncoderDNN/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Gather}}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test_muse.py", line 13, in <module>
    tf.import_graph_def(frozen_graph_def, name='')
  File "/home/zehaohua/miniconda3/envs/muse/lib/python3.8/site-packages/tensorflow/python/util/deprecation.py", line 576, in new_func
    return func(*args, **kwargs)
  File "/home/zehaohua/miniconda3/envs/muse/lib/python3.8/site-packages/tensorflow/python/framework/importer.py", line 406, in import_graph_def
    return _import_graph_def_internal(
  File "/home/zehaohua/miniconda3/envs/muse/lib/python3.8/site-packages/tensorflow/python/framework/importer.py", line 515, in _import_graph_def_internal
    raise ValueError(str(e))
ValueError: input resource[0] expected type resource != float, the type of embeddings_sharded_0[0]
        In {{node EncoderDNN/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/Gather}}

```
