Я хочу преобразовать сохраненную модель Keras в Saved_model, которую можно использовать с сервировкой 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 на диск, затем попробуйте преобразовать в save_model
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}}]]