[英]Unable to save TensorFlow Keras LSTM model to SavedModel format
System information:系统信息:
OS Platform and Distribution (eg, Linux Ubuntu 16.04): Windows 10操作系统平台和分发(例如,Linux Ubuntu 16.04):Windows 10
TensorFlow installed from (source or binary): pip installed TensorFlow 安装自(源或二进制):安装 pip
TensorFlow version (use command below): v2.0.0-rc2-26-g64c3d382ca 2.0.0 TensorFlow 版本(使用下面的命令):v2.0.0-rc2-26-g64c3d382ca 2.0.0
Python version: 3.7.1 Python 版本:3.7.1
Error:错误:
Unable to save TensorFlow Keras LSTM model to SavedModel format for exporting to Google Cloud bucket.无法将 TensorFlow Keras LSTM model 保存为 SavedModel 格式以导出到 Google Cloud 存储桶。
Error Message:错误信息:
ValueError: Attempted to save a function b'__inference_lstm_2_layer_call_fn_36083' which references a symbolic Tensor Tensor("dropout/mul_1:0", shape=(None, 1280), dtype=float32) that is not a simple constant. ValueError: 试图保存一个 function b'__inference_lstm_2_layer_call_fn_36083' 它引用了一个符号张量 Tensor("dropout/mul_1:0", shape=(None, 1280), dtype=float32) 这不是一个简单的常量。 This is not supported.
这是不支持的。
Code:代码:
import tensorflow as tf
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tqdm
import datetime
from sklearn.preprocessing import LabelBinarizer
model = tf.keras.Sequential([
tf.keras.layers.Masking(mask_value=0.),
tf.keras.layers.LSTM(512, dropout=0.5, recurrent_dropout=0.5),
tf.keras.layers.Dense(256, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(len(LABELS), activation='softmax')
])
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy', 'top_k_categorical_accuracy'])
test_file = 'C:/.../testlist01.txt'
train_file = 'C:/.../trainlist01.txt'
with open(test_file) as f:
test_list = [row.strip() for row in list(f)]
with open(train_file) as f:
train_list = [row.strip() for row in list(f)]
train_list = [row.split(' ')[0] for row in train_list]
def make_generator(file_list):
def generator():
np.random.shuffle(file_list)
for path in file_list:
full_path = os.path.join(BASE_PATH, path).replace('.avi', '.npy')
label = os.path.basename(os.path.dirname(path))
features = np.load(full_path)
padded_sequence = np.zeros((SEQUENCE_LENGTH, 1280))
padded_sequence[0:len(features)] = np.array(features)
transformed_label = encoder.transform([label])
yield padded_sequence, transformed_label[0]
return generator
train_dataset = tf.data.Dataset.from_generator(make_generator(train_list),
output_types=(tf.float32, tf.int16),
output_shapes=((SEQUENCE_LENGTH, 1280), (len(LABELS))))
train_dataset = train_dataset.batch(16).prefetch(tf.data.experimental.AUTOTUNE)
valid_dataset = tf.data.Dataset.from_generator(make_generator(test_list),
output_types=(tf.float32, tf.int16),
output_shapes=((SEQUENCE_LENGTH, 1280), (len(LABELS))))
valid_dataset = valid_dataset.batch(16).prefetch(tf.data.experimental.AUTOTUNE)
model.fit(train_dataset, epochs=17, validation_data=valid_dataset)
BASE_DIRECTORY = 'C:\\...\\saved_model\\LSTM\\1\\';
tf.saved_model.save(model, BASE_DIRECTORY)
In addition to the answer of The Guy with The Hat:除了戴帽子的家伙的答案:
The .h5 part is sufficient to tell keras to store it as keras model save. .h5部分足以告诉 keras 将其存储为 keras model 保存。
model.save('path_to_saved_model/model.h5')
should do the trick.应该做的伎俩。
Try saving it with the Keras API, not the SavedModel API.尝试使用 Keras API 保存它,而不是 SavedModel API。 See Save and serialize models with Keras: Export to SavedModel .
请参阅使用 Keras 保存和序列化模型:导出到 SavedModel 。
model.save('path_to_saved_model', save_format='tf')
That should save the model to a SavedModel format.这应该将 model 保存为 SavedModel 格式。
There is a bug I think and you need to set the dropout to 0 for the functions tf.saved_model.save
and model.save(.., save_format='tf')
to work我认为有一个错误,您需要将 dropout 设置为 0 函数
tf.saved_model.save
和model.save(.., save_format='tf')
才能工作
This seems to be a bug with both TensorFlow 2.0 and 2.1, after upgrading my TensorFlow to v2.2, it's working fine.这似乎是 TensorFlow 2.0 和 2.1 的错误,在将我的 TensorFlow 升级到 v2.2 后,它工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.