使用AI平台进行预测时,“表未初始化”

时间:2020-07-15 13:17:46

标签: tensorflow keras google-cloud-ml

我正在尝试保存更快的R-CNN集线器模型,并将其与AI平台gcloud ai-platform local predict一起使用。我收到的错误是:

Failed to run the provided model: Exception during running the graph: [_Derived_]  Table not initialized.\n\t [[{{node hub_input/index_to_string_1_Lookup}}]]\n\t [[StatefulPartitionedCall_1/StatefulPartitionedCall/model/keras_layer/StatefulPartitionedCall]] (Error code: 2)\n'

用于保存模型的代码:

model_url = "https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1"

input = tf.keras.Input(shape=(), dtype=tf.string)
decoded = tf.keras.layers.Lambda(
    lambda y: tf.map_fn(
        lambda x: tf.image.resize(
            tf.image.convert_image_dtype(
                tf.image.decode_jpeg(x, channels=3), tf.float32), (416, 416)
        ),
        tf.io.decode_base64(y), dtype=tf.float32)
)(input)

results = hub.KerasLayer(model_url, signature_outputs_as_dict=True)(decoded)

model = tf.keras.Model(inputs=input, outputs=results)
model.save("./saved_model", save_format="tf")

当我加载tf.keras.models.load_model("./saved_model")并使用它进行预测时,该模型可以工作,但不能使用AI平台的本地预测。

用于AI平台本地预测的命令:

gcloud ai-platform local predict --model-dir ./saved_model --json-instances data.json --framework TENSORFLOW

版本:

python 3.7.0
tensorflow==2.2.0
tensorflow-hub==0.7.0

Saved_model_cli的输出:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is: 

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['image_bytes'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: serving_default_image_bytes:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['keras_layer'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 4)
        name: StatefulPartitionedCall_1:0
    outputs['keras_layer_1'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_1:1
    outputs['keras_layer_2'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: StatefulPartitionedCall_1:2
    outputs['keras_layer_3'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_1:3
    outputs['keras_layer_4'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: StatefulPartitionedCall_1:4
  Method name is: tensorflow/serving/predict

关于如何解决该错误的任何想法?

1 个答案:

答案 0 :(得分:0)

问题是您的输入被解释为标量。做:

input = tf.keras.Input(shape=(1,), dtype=tf.string)