【问题标题】:'no SavedModel bundles found!' on tensorflow_hub model deployment to AWS SageMaker'没有找到 SavedModel 包!'将 tensorflow_hub 模型部署到 AWS SageMaker
【发布时间】:2019-12-01 23:19:18
【问题描述】:

我尝试将通用句子编码器模型部署到 aws Sagemaker 端点并收到错误 raise ValueError('no SavedModel bundles found!')

我在下面显示了我的代码,我感觉我的一条路径不正确

import tensorflow as tf
import tensorflow_hub as hub
import numpy as np
from sagemaker import get_execution_role
from sagemaker.tensorflow.serving import Model

def tfhub_to_savedmodel(model_name,uri):
    tfhub_uri = uri
    model_path = 'encoder_model/' + model_name

    with tf.Session(graph=tf.Graph()) as sess:
        module = hub.Module(tfhub_uri) 
        input_params = module.get_input_info_dict()
        dtype = input_params['text'].dtype
        shape = input_params['text'].get_shape()

        # define the model inputs
        inputs = {'text': tf.placeholder(dtype, shape, 'text')}

        # define the model outputs
        # we want the class ids and probabilities for the top 3 classes
        logits = module(inputs['text'])
        outputs = {
            'vector': logits,
        }

        # export the model
        sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
        tf.saved_model.simple_save(
            sess,
            model_path,
            inputs=inputs,
            outputs=outputs)  

    return model_path


sagemaker_role = get_execution_role()

!tar -C "$PWD" -czf encoder.tar.gz encoder_model/
model_data = Session().upload_data(path='encoder.tar.gz',key_prefix='model')

env = {'SAGEMAKER_TFS_DEFAULT_MODEL_NAME': 'universal-sentence-encoder-large'}

model = Model(model_data=model_data, role=sagemaker_role, framework_version=1.12, env=env)
predictor = model.deploy(initial_instance_count=1, instance_type='ml.t2.medium')

【问题讨论】:

    标签: amazon-web-services tensorflow amazon-sagemaker tensorflow-hub


    【解决方案1】:

    我想你是从这个例子开始的? https://github.com/awslabs/amazon-sagemaker-examples/tree/master/sagemaker-python-sdk/tensorflow_serving_container

    您似乎没有正确保存 TF Serving 包:缺少型号版本号,因为这行:

    model_path = 'encoder_model/' + model_name
    

    用这个替换它应该可以解决您的问题:

    model_path = '{}/{}/00000001'.format('encoder_model/', model_name)
    

    您的模型工件应该如下所示(我使用了上面笔记本中的模型):

    mobilenet/
    mobilenet/mobilenet_v2_140_224/
    mobilenet/mobilenet_v2_140_224/00000001/
    mobilenet/mobilenet_v2_140_224/00000001/saved_model.pb
    mobilenet/mobilenet_v2_140_224/00000001/variables/
    mobilenet/mobilenet_v2_140_224/00000001/variables/variables.data-00000-of-00001
    mobilenet/mobilenet_v2_140_224/00000001/variables/variables.index
    

    然后,上传到 S3 并部署。

    【讨论】:

    • 谢谢你
    • 乐于助人。这最初也难倒我:)
    • 当我尝试从我的端点进行预测时,我收到了错误 OP_REQUIRES failed at lookup_table_op.cc:674 : Failed precondition: Table not initialized.。你会碰巧知道这是什么,因为我相信我上面的代码我在我的运行中初始化表
    • 你能解释一下它必须是什么格式吗?我很困惑 savedModel 需要在什么结构中......它基本上需要版本号 00000001 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多