ValueError:未能分析模型:仅支持具有单个子图的模型,模型具有X个子图

2024-10-05 17:41:45 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试将保存的模型转换为tflite模型时,会出现此错误,我不知道如何通过此步骤。我保存的_模型是针对mobilenet_ssd_v2的,我是通过这个创建的 gist

以下是我正在使用的Colab代码:

!pip install tf-nightly
#import tensorflow as tf
import tensorflow.compat.v2 as tf
tf.enable_v2_behavior()
tf.__version__

def representative_dataset():
    for _ in range(100):
      data = np.random.rand(1, 244, 244, 3)
      yield [data.astype(np.uint8)]

saved_model_obj = tf.saved_model.load("/content/model/")
#print(saved_model_obj.signatures)
concrete_func = saved_model_obj.signatures['serving_default']
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func]) # path to the SavedModel directory
converter.experimental_new_converter = True
converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, # enable TensorFlow Lite ops.
  tf.lite.OpsSet.SELECT_TF_OPS, # enable TensorFlow ops.
  tf.lite.OpsSet.TFLITE_BUILTINS_INT8
]
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.inference_input_type = tf.uint8  # or tf.uint8
converter.inference_output_type = tf.uint8  # or tf.uint8

tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

完全错误:

RuntimeError                              Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/optimize/calibrator.py in __init__(self, model_content)
     57       self._calibrator = (
---> 58           _calibration_wrapper.CalibrationWrapper(model_content))
     59     except Exception as e:

RuntimeError: Only models with a single subgraph are supported, model had 11 subgraphs

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
5 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/optimize/calibrator.py in __init__(self, model_content)
     58           _calibration_wrapper.CalibrationWrapper(model_content))
     59     except Exception as e:
---> 60       raise ValueError("Failed to parse the model: %s." % e)
     61     if not self._calibrator:
     62       raise ValueError("Failed to parse the model.")

ValueError: Failed to parse the model: Only models with a single subgraph are supported, model had 11 subgraphs.

任何帮助都将不胜感激


Tags: thetoselfmodeltftensorflowaslite