PINTO0309 / tflite2tensorflow

Generate saved_model, tfjs, tf-trt, EdgeTPU, CoreML, quantized tflite, ONNX, OpenVINO, Myriad Inference Engine blob and .pb from .tflite. Support for building environments with Docker. It is possible to directly access the host PC GUI and the camera to verify the operation. NVIDIA GPU (dGPU) support. Intel iHD GPU (iGPU) support. Supports inverse quantization of INT8 quantization model.
https://qiita.com/PINTO
MIT License
258 stars 38 forks source link

IndexError: list index (0) out of range #17

Closed alexionby closed 2 years ago

alexionby commented 2 years ago

1. Ubuntu 18.04

2. OS Architecture x86_64

4. Version of TensorFlow e.g. v2.6.0

9. segm_full_sparse_v1008.tflite

10. Coverted model

Converted by command (from Docker): tflite2tensorflow --model_path segm_full_sparse_v1008.tflite --flatc_path ../flatc --schema_path ../schema.fbs --output_pb

12. Issue Details

This code: temp = tf.keras.models.load_model("saved_model/", compile=False, ) Produce error:

WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
/tmp/ipykernel_2258/3543548130.py in <module>
----> 1 temp = tf.keras.models.load_model("saved_model/", compile=False)

~/anaconda3/envs/tfmot/lib/python3.8/site-packages/keras/saving/save.py in load_model(filepath, custom_objects, compile, options)
    203         filepath = path_to_string(filepath)
    204         if isinstance(filepath, str):
--> 205           return saved_model_load.load(filepath, compile, options)
    206 
    207   raise IOError(

~/anaconda3/envs/tfmot/lib/python3.8/site-packages/keras/saving/saved_model/load.py in load(path, compile, options)
    123                     'tf.saved_model.save(). To confirm, there should be a file '
    124                     'named "keras_metadata.pb" in the SavedModel directory.')
--> 125     _read_legacy_metadata(object_graph_def, metadata)
    126 
    127   if not metadata.nodes:

~/anaconda3/envs/tfmot/lib/python3.8/site-packages/keras/saving/saved_model/load.py in _read_legacy_metadata(object_graph_def, metadata)
    194   # Older SavedModels store the metadata directly in the proto instead of the
    195   # separate pb file.
--> 196   node_paths = _generate_object_paths(object_graph_def)
    197   for node_id, proto in enumerate(object_graph_def.nodes):
    198     if (proto.WhichOneof('kind') == 'user_object' and

~/anaconda3/envs/tfmot/lib/python3.8/site-packages/keras/saving/saved_model/load.py in _generate_object_paths(object_graph_def)
    221     current_node = nodes_to_visit.pop()
    222     current_path = paths[current_node]
--> 223     for reference in object_graph_def.nodes[current_node].children:
    224       if reference.node_id in paths:
    225         continue

IndexError: list index (0) out of range

How to convert this model to Keras (h5) ?

PINTO0309 commented 2 years ago

It's probably a bug in Keras. The option to convert to h5 was removed a long time ago, as there are many bugs related to loading h5.

$ python3
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> m = tf.saved_model.load('saved_model')
INFO:tensorflow:Saver not created because there are no variables in the graph to restore
2021-10-20 12:41:15.226458: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /opt/intel/openvino_2021/data_processing/dl_streamer/lib:/opt/intel/openvino_2021/data_processing/gstreamer/lib:/opt/intel/openvino_2021/opencv/lib:/opt/intel/openvino_2021/deployment_tools/ngraph/lib:/opt/intel/openvino_2021/deployment_tools/inference_engine/external/tbb/lib::/opt/intel/openvino_2021/deployment_tools/inference_engine/external/hddl/lib:/opt/intel/openvino_2021/deployment_tools/inference_engine/external/omp/lib:/opt/intel/openvino_2021/deployment_tools/inference_engine/external/gna/lib:/opt/intel/openvino_2021/deployment_tools/inference_engine/external/mkltiny_lnx/lib:/opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64:/usr/local/cuda/compat/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64
2021-10-20 12:41:15.226478: W tensorflow/stream_executor/cuda/cuda_driver.cc:269] failed call to cuInit: UNKNOWN ERROR (303)
2021-10-20 12:41:15.226496: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:163] no NVIDIA GPU device is present: /dev/nvidia0 does not exist
2021-10-20 12:41:15.226629: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-10-20 12:41:15.237417: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:185] None of the MLIR Optimization Passes are enabled (registered 2)
>>> 

v1008.zip

alexionby commented 2 years ago

Got it. Thank you. Conversion to tensorflow works fine. I hoped that it's possible to retrain this model from saved weights via keras.model.fit() method.