繁体   English   中英

将预训练的 Keras 加载到 Sagemaker - 本地分类有效,但 sagemaker 分类发生变化

[英]Loading Pretrained Keras to Sagemaker - local classification works but sagemaker classification changes

编辑:找到解决方案,请参阅帖子底部。

我有一个预训练的 keras model (model.h5),它是一个用于图像分类的 CNN。 我的目标是在 sagemaker 上部署 model 并使用 lambda function 与 sagemaker 端点接口并进行预测。 当我使用以下代码在本地机器上使用 model 进行预测时,我会得到预期的结果:

model = load_model(r'model.h5')
photo_fp = r'/path/to/photo.jpg'

img = Image.open(photo_fp).resize((128,128))
image_array = np.array(img) / 255.
img_batch = np.expand_dims(image_array, axis=0)

print(model.predict(img_batch))
# [[9.9984562e-01 1.5430539e-04 2.2775747e-14 9.5851349e-16]]

但是,当我将 model 部署为 sagemaker 上的端点时,我得到了不同的结果。 下面是我将 model 部署为端点的代码:

model = load_model(r'model.h5')

import tensorflow as tf
from tensorflow import keras
import sagemaker
import boto3, re
from sagemaker import get_execution_role
def convert_h5_to_aws(loaded_model):
    # Interpreted from 'Data Liam'
    from tensorflow.python.saved_model import builder
    from tensorflow.python.saved_model.signature_def_utils import predict_signature_def
    from tensorflow.python.saved_model import tag_constants
    
    model_version = '1'
    export_dir = 'export/Servo/' + model_version
    
    # Build the Protocol Buffer SavedModel at 'export_dir'
    builder = builder.SavedModelBuilder(export_dir)
    
    # Create prediction signature to be used by TensorFlow Serving Predict API
    signature = predict_signature_def(
        inputs={"inputs": loaded_model.input}, outputs={"score": loaded_model.output})

    with tf.compat.v1.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        # Save the meta graph and variables
        builder.add_meta_graph_and_variables(
            sess=sess, tags=[tag_constants.SERVING], signature_def_map={"serving_default": signature})
        builder.save()
    
    #create a tarball/tar file and zip it
    import tarfile
    with tarfile.open('model.tar.gz', mode='w:gz') as archive:
        archive.add('export', recursive=True)
        
convert_h5_to_aws(model)

sagemaker_session = sagemaker.Session()
inputs = sagemaker_session.upload_data(path='model.tar.gz', key_prefix='model')

!touch train.py # from notebook
# the (default) IAM role
role = get_execution_role()
framework_version = tf.__version__

# Create Sagemaker model
from sagemaker.tensorflow.model import TensorFlowModel
sagemaker_model = TensorFlowModel(model_data = 's3://' + sagemaker_session.default_bucket() + '/model/model.tar.gz',
                                  role = role,
                                  framework_version = framework_version,
                                  entry_point = 'train.py')

predictor = sagemaker_model.deploy(initial_instance_count=1,
                                   instance_type='ml.m4.xlarge')

这可以很好地部署并保存为端点。 然后,我调用端点:

runtime = boto3.client('runtime.sagemaker')
endpoint_name = 'endpoint-name-for-stackoverflow'

img = Image.open(photo_fp).resize((128,128))
image_array = np.array(img) / 255.
img_batch = np.expand_dims(image_array, axis=0)
predictor = TensorFlowPredictor(endpoint_name)
result = predictor.predict(data=img_batch)
print(result)
# {'predictions': [[0.199595317, 0.322404563, 0.209394112, 0.268606]]}

如您所见,分类器将所有输出预测为几乎相等的概率,这与本地机器上的预测不同。 这让我相信我的部署出现了问题。

I have tried loading the model weights and json model structure to sagemaker rather than the entire h5 model but that yielded the same results. 我还使用了调用端点而不是预测器 API,代码如下:

payload = json.dumps(img_batch.tolist())
response = runtime.invoke_endpoint(EndpointName=endpoint_name,
                                   ContentType='application/json',
                                   Body=payload)
result = json.loads(response['Body'].read().decode())
print(result)
# {'predictions': [[0.199595317, 0.322404563, 0.209394112, 0.268606]]}

但是,同样的结果。

任何想法为什么我使用 sagemaker 得到的结果与使用相同 model 的本地机器不同? 谢谢!

编辑:找到解决方案。 问题出在 TensorflowModel 框架版本参数上。 我将 framework_version 更改为“1.12”并在 Sagemaker Jupyter 实例中安装了 1.12 版本,并使用 TF 1.12 在本地重新训练了我的 model。 我不完全确定为什么会这样,但我发现的所有博客(例如这个)都使用了 1.12。 希望这可以帮助。

为了社区的利益,在答案部分提供解决方案

问题出在TensorflowModel框架版本参数上。 在将framework_version更改为1.12并在Sagemaker Jupyter实例中安装TF 1.12版本并使用TF 1.12在本地重新训练 model 后,得到了相同的结果。 (从彼得范卡特维克转述)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM