&引用;图形中没有名为[input]的操作;在java程序中使用hub.KerasLayer中的模型时,请使用python和tensorflow 2.1.0

&引用;图形中没有名为[input]的操作;在java程序中使用hub.KerasLayer中的模型时,请使用python和tensorflow 2.1.0,java,python,tensorflow,Java,Python,Tensorflow,我使用tf2.1.0和python的hub.KerasLayer训练了一个mobilnet_v2 tensorflow模型,并使用tf.keras.models.save_模型以pb格式导出它。 我用java加载了它,但是我找不到一种方法来正确地提供图形 以下是模型构建和导出代码: for image_batch, label_batch in train_generator: break IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3) feature_ext

我使用tf2.1.0和python的hub.KerasLayer训练了一个mobilnet_v2 tensorflow模型,并使用tf.keras.models.save_模型以pb格式导出它。 我用java加载了它,但是我找不到一种方法来正确地提供图形

以下是模型构建和导出代码:

for image_batch, label_batch in train_generator:
    break

IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)

feature_extractor_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2"

feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
                                         input_shape=IMG_SHAPE,
                                         name='input')

feature_batch = feature_extractor_layer(image_batch)
feature_extractor_layer.trainable = False

model = tf.keras.Sequential([
  feature_extractor_layer,
  layers.Dense(train_generator.num_classes, name='output')
])

...... training .......

tf.keras.models.save_model(model,export_path)

以下是我尝试用java为其提供信息的方式:

Tensor inputImage = getTensorFromImage() // a method defined in other code and tested ok
final Session s = new Session(graphFromPBLoadedModel);
final Tensor result = s.runner().feed("input", inputImage )
                        .fetch("output").run().get(0))
以下是生成的异常:

java.lang.IllegalArgumentException: No Operation named [input] in the Graph

我假设这是生成或导出过程中出现的签名问题,但没有找到正确的方法…

我可以看到问题是从运行
saved_model\u cli show--dir.'--我导出的模型目录中的所有

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['input_input'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 224, 224, 3)
        name: serving_default_input_input:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 5)
        name: StatefulPartitionedCall:0
  Method name is: tensorflow/serving/predict

使用
“服务默认输入”
,我可以解决这个问题。

您将其保存为
tf.keras
模型,您是否尝试过保存为
SavedModel
tf.saved\u model.save(model,./dir”)
?谢谢@frederikode,但是
tf.keras.models.save\u model()
默认情况下已以
SavedModel
格式导出。