【发布时间】:2020-09-15 17:31:44
【问题描述】:
我一直在尝试使用The Universal Sentence Encoder Multilingual 与 tensorflow(1.15) keras 进行迁移学习。我在 Sequential 模型中使用句子编码器作为 KerasLayer。训练结束后,我使用tf.saved_model.save 将我的模型保存为saved_model.pb。这是我的模型:
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text
USE_V3 = "https://tfhub.dev/google/universal-sentence-encoder-multilingual-large/3"
my_model = tf.keras.models.Sequential(
[
tf.keras.layers.InputLayer(input_shape=(), dtype=tf.string),
hub.KerasLayer(USE_V3, trainable=False),
tf.keras.layers.Dense(3, activation="softmax"),
]
)
my_model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])
我使用谷歌人工智能平台进行训练和预测。训练没有问题,它训练模型并将模型保存在谷歌云存储上。当我部署我保存的模型进行预测时,会在 ai 平台的模型部分创建一个发布版本。我使用 TensorFlow 作为框架,python 3.7,运行时版本 1.15。当我使用TEST & USE 部分获取样本预测时,它给出了这个错误:(我也尝试了api,返回相同的错误)
{
"error": "Prediction failed: Error during model execution: AbortionError(code=StatusCode.NOT_FOUND, details=\"{{function_node __inference_signature_wrapper_139623}} {{function_node __inference_signature_wrapper_139623}} {{function_node __inference___call___137889}} {{function_node __inference___call___137889}} {{function_node __inference_restored_function_body_100331}} {{function_node __inference_restored_function_body_100331}} [_Derived_]{{function_node __inference___call___55591}} {{function_node __inference___call___55591}} Op type not registered 'SentencepieceOp' in binary running on localhost. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.\n\t [[{{node StatefulPartitionedCall}}]]\n\t [[StatefulPartitionedCall]]\n\t [[muse_sentiment_classification/embedding/StatefulPartitionedCall]]\n\t [[StatefulPartitionedCall]]\n\t [[StatefulPartitionedCall]]\")"
}
简而言之,在预测机中找不到SentencepieceOp(来自tensorflow_text)。然后,我尝试使用找到here 的解决方法。这篇文章使用了缺少SentencepieceEncodeSparse 的解决方法,但我认为原因是相似的。我按照建议创建了自己的Custom Prediction Routine,并将tensorflow_text 的依赖项放入其中。自定义预测例程的模型大小限制为 500MB,而我的模型约为 350MB。当我尝试为我的模型创建新版本时,这一次它不会创建发布版本并给出此内存错误:
Create Version failed. Bad model detected with error: Model requires more memory than allowed. Please try to decrease the model size and re-deploy. If you continue to experience errors, please contact support.
第一个问题:在没有自定义预测例程解决方法的情况下,是否有适当的方法在 ai-platform 中部署使用 Universal Sentence Encoder Multilingual 的模型?
第二个问题:如果我必须使用自定义预测例程,我该如何解决这个内存问题?我的意思是我只有 1 个额外的密集层。如何减少内存使用量?
编辑:我使用 gcloud (v296.0.1) 和云控制台进行部署,配置相同。这些是框架TensorFlow 和Custom Prediction Routine 的部署脚本:
TensorFlow 部署:
MODEL_DIR="gs://my-bucket--us-central1/training/sentiment_training/_model/"
VERSION_NAME="test_v1"
MODEL_NAME="Sentiment"
FRAMEWORK="TensorFlow"
gcloud ai-platform versions create $VERSION_NAME \
--model $MODEL_NAME \
--origin $MODEL_DIR \
--runtime-version=1.15 \
--framework $FRAMEWORK \
--python-version=3.7
自定义预测例程部署:
MODEL_DIR="gs://my-bucket--us-central1/training/sentiment_training/_model/"
VERSION_NAME="test_v1"
MODEL_NAME="Sentiment"
CUSTOM_CODE_PATH="gs://my-bucket--us-central1/packages/custom-op-tf-predictor-0.1.tar.gz"
PREDICTOR_CLASS="predictor.CustomOpTfPredictor"
gcloud beta ai-platform versions create $VERSION_NAME \
--model $MODEL_NAME \
--origin $MODEL_DIR \
--runtime-version=1.15 \
--python-version=3.7 \
--package-uris=$CUSTOM_CODE_PATH \
--prediction-class=$PREDICTOR_CLASS
【问题讨论】:
-
您能分享一下您是如何部署模型的吗?
-
@guillaumeblaquiere 我在最后添加了部署脚本。
标签: python tensorflow keras google-cloud-platform google-cloud-ml