tf.saved_model.simple_从Keras模型转换时保存失败

2024-05-28 19:53:51 发布

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

我想将Keras保存的模型转换为可用于Tenserflow服务的保存的_模型

我使用预训练模型创建模型

feature_extractor_url = "https://tfhub.dev/google/tf2-preview/inception_v3/feature_vector/4"
feature_extractor_layer = hub.KerasLayer(feature_extractor_url, input_shape=self.img_dim)

feature_extractor_layer.trainable = False

model = tf.keras.Sequential([
  feature_extractor_layer,
  layers.Dense(32, activation='relu'),
  layers.Dense(14, activation='softmax')
])

然后将keras模型保存到磁盘,然后尝试转换为保存的_模型

import tensorflow as tf
import tensorflow_hub as hub

MODEL_FOLDER = "../data/model"

tf.keras.backend.set_learning_phase(0)  # Ignore dropout at inference

EXPORT_PATH = './models/my_estimate/1'
with tf.keras.backend.get_session() as sess:
    model = tf.keras.models.load_model(MODEL_FOLDER, custom_objects={'KerasLayer': hub.KerasLayer})
    tf.saved_model.simple_save(
        sess,
        EXPORT_PATH,
        inputs={'input_image': model.input},
        outputs={t.name: t for t in model.outputs})

但我得到的错误如下,我不知道如何修复它

FailedPreconditionError: 2 root error(s) found.
  (0) Failed precondition: Error while reading resource variable save_counter from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/save_counter/N10tensorflow3VarE does not exist.
     [[{{node save_counter/Read/ReadVariableOp}}]]
     [[Adam/dense_1/kernel/v/Read/ReadVariableOp/_3137]]
  (1) Failed precondition: Error while reading resource variable save_counter from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/save_counter/N10tensorflow3VarE does not exist.
     [[{{node save_counter/Read/ReadVariableOp}}]]

Tags: 模型layerlocalhostinputmodelsavetfas

热门问题