在Google AI平台上部署包含Universal Sentence Encoder Multilingual的模型

时间:2020-05-28 11:53:51

标签: python tensorflow keras google-cloud-platform google-cloud-ml

我一直在尝试使用The Universal Sentence Encoder Multilingual与tensorflow(1.15)keras进行迁移学习。我在顺序模型中将句子编码器用作KerasLayer。训练后,我使用saved_model.pb将模型另存为tf.saved_model.save。这是我的模型:

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"])

我使用Google AI平台进行培训和预测。培训没有问题,它可以训练模型并将其保存在Google云存储中。当我部署保存的模型进行预测时,会在ai平台的“模型”部分中创建发行版本。我使用TensorFlow作为Framework,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平台中使用Universal Sentence Encoder Multilingual的模型,而无需使用“自定义预测例程”解决方法?

第二个问题:如果必须使用“自定义预测例程”,如何解决此内存问题?我的意思是我只有1个额外的密集层。如何减少内存使用量?

编辑:我使用gcloud(v296.0.1)和云控制台进行具有相同配置的部署。这些是框架TensorFlowCustom 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

1 个答案:

答案 0 :(得分:0)

根据Google支持,使用未记录的机器类型(mls1-c4-m4)作为预测机器解决了我的问题。我只是在Custom Prediction Routine部署结束时添加了--machine-type=mls1-c4-m4。这台机器的内存限制为4gb,但仍处于alpha状态。