感谢您提前帮助解决此问题。 我在Sagemaker上训练了一个模型。这是一个TensorFlow估算器,它将图像作为输入,使用InceptionV3计算高级功能(即瓶颈),然后使用密集层来预测新类。
它有点起作用:我可以训练它,服务它,并预测新的图像一个人。
现在,我想在一次独特的HTTP调用/ predict()调用中一次预测整批图像。怎么样?
我是这样做的:
from IPython.display import Image
import numpy as np
from keras.preprocessing import image
estimator = TensorFlow(entry_point=..., ...)
estimator.fit(train_data_location)
predictor = estimator.deploy(initial_instance_count=1,
instance_type='ml.m4.xlarge')
image_list = [
'e9bfa679-31bb-464e-9d9f-3bdb0ef9c121.jpeg', # 131
'b27880e1-6de8-43cf-a684-bb02aef1e44b.jpeg', # 170
]
directory = '/path/to/dir/'
images = np.empty((len(image_list), 299, 299, 3), dtype=np.float32)
# for filename in image_list:
for i,filename in enumerate(image_list):
path = os.path.join(directory, filename)
Image(path)
img = image.load_img(path, target_size=(299, 299))
x = image.img_to_array(img)
images[i] = x
print(images.shape)
# to_send = images[-1] # works for a unique image
to_send = images # doesn't work for a batch of images
# some other attempts that did not work
# to_send = images.tolist()
# to_send = [images[0].tolist(), images[1].tolist()]
print(np.shape(to_send))
predict_response = predictor.predict(to_send)
print('The model predicted the following classes: \n{}'.format(
predict_response['outputs']['classes']['int64Val']))
这会激发以下结果:
(2,299,299,3)
(2,299,299,3)#注意我发送的形状。那么为什么它会在下面的日志中抱怨形状[1,2,299,299,3]?
ModelError:发生错误 (ModelError)调用InvokeEndpoint操作时:Received 带有消息“”的模型的服务器错误(500)。看到 https://eu-west-1.console.aws.amazon.com/cloudwatch/home?region=eu-west-1#logEventViewer:group=/aws/sagemaker/Endpoints/sagemaker-tensorflow-py2-cpu-2018-02-05-16-48-38-496 在帐户047562184710获取更多信息
以下是来自AWS的日志:
# [2018-02-06 09:29:20,937] ERROR in serving: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input must be 4-dimensional[1,2,299,299,3]
# #011 [[Node: ResizeBilinear = ResizeBilinear[T=DT_FLOAT, _output_shapes=[[1,299,299,3]], align_corners=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ExpandDims, ResizeBilinear/size)]]")
# Traceback (most recent call last):
# File "/opt/amazon/lib/python2.7/site-packages/container_support/serving.py", line 161, in _invoke
# self.transformer.transform(content, input_content_type, requested_output_content_type)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 255, in transform
# return self.transform_fn(data, content_type, accepts), accepts
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 180, in f
# prediction = self.predict_fn(input)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 195, in predict_fn
# return self.proxy_client.request(data)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/proxy_client.py", line 51, in request
# return request_fn(data)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/proxy_client.py", line 79, in predict
# result = stub.Predict(request, self.request_timeout)
# File "/usr/local/lib/python2.7/dist-packages/grpc/beta/_client_adaptations.py", line 310, in __call__
# self._request_serializer, self._response_deserializer)
# File "/usr/local/lib/python2.7/dist-packages/grpc/beta/_client_adaptations.py", line 196, in _blocking_unary_unary
# raise _abortion_error(rpc_error_call)
# AbortionError: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input must be 4-dimensional[1,2,299,299,3]
# #011 [[Node: ResizeBilinear = ResizeBilinear[T=DT_FLOAT, _output_shapes=[[1,299,299,3]], align_corners=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ExpandDims, ResizeBilinear/size)]]")
# 2018-02-06 09:29:20,937 ERROR - model server - AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input must be 4-dimensional[1,2,299,299,3]
# #011 [[Node: ResizeBilinear = ResizeBilinear[T=DT_FLOAT, _output_shapes=[[1,299,299,3]], align_corners=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ExpandDims, ResizeBilinear/size)]]")
# Traceback (most recent call last):
# File "/opt/amazon/lib/python2.7/site-packages/container_support/serving.py", line 161, in _invoke
# self.transformer.transform(content, input_content_type, requested_output_content_type)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 255, in transform
# return self.transform_fn(data, content_type, accepts), accepts
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 180, in f
# prediction = self.predict_fn(input)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/serve.py", line 195, in predict_fn
# return self.proxy_client.request(data)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/proxy_client.py", line 51, in request
# return request_fn(data)
# File "/opt/amazon/lib/python2.7/site-packages/tf_container/proxy_client.py", line 79, in predict
# result = stub.Predict(request, self.request_timeout)
# File "/usr/local/lib/python2.7/dist-packages/grpc/beta/_client_adaptations.py", line 310, in __call__
# self._request_serializer, self._response_deserializer)
# File "/usr/local/lib/python2.7/dist-packages/grpc/beta/_client_adaptations.py", line 196, in _blocking_unary_unary
# raise _abortion_error(rpc_error_call)
# AbortionError: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input must be 4-dimensional[1,2,299,299,3]
# #011 [[Node: ResizeBilinear = ResizeBilinear[T=DT_FLOAT, _output_shapes=[[1,299,299,3]], align_corners=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ExpandDims, ResizeBilinear/size)]]")
# [2018-02-06 09:29:20,956] ERROR in serving: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input must be 4-dimensional[1,2,299,299,3]
# #011 [[Node: ResizeBilinear = ResizeBilinear[T=DT_FLOAT, _output_shapes=[[1,299,299,3]], align_corners=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ExpandDims, ResizeBilinear/size)]]")
# 2018-02-06 09:29:20,956 ERROR - model server - AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input must be 4-dimensional[1,2,299,299,3]
# #011 [[Node: ResizeBilinear = ResizeBilinear[T=DT_FLOAT, _output_shapes=[[1,299,299,3]], align_corners=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ExpandDims, ResizeBilinear/size)]]")
# 10.32.0.1 - - [06/Feb/2018:09:29:20 +0000] "POST /invocations HTTP/1.1" 500 0 "-" "AHC/2.0"
顺便说一句,如果我用PIL而不是Keras加载图像,我会遇到同样的问题:
image = Image.open(path)
image_array = np.array(image)
这是服务器端的代码:
def serving_input_fn(params):
""" See https://www.tensorflow.org/programmers_guide/
saved_model#using_savedmodel_with_estimators
and
See https://github.com/aws/sagemaker-python-sdk#creating-a-serving_input_fn
and https://docs.aws.amazon.com/sagemaker/latest/dg/
tf-training-inference-code-template.html
"""
# Download InceptionV3 if need be, in order to
# compute high level features (called bottleneck here),
# which are then fed into the model
model_dir = './pretrained_model/'
maybe_download_and_extract(params['data_url'],
dest_directory=model_dir)
model_path = os.path.join(model_dir, params['model_file_name'])
with tf.gfile.FastGFile(model_path, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
bottleneck_tensor, resized_input_tensor, input_tensor = (
tf.import_graph_def(
graph_def,
name='',
input_map=None,
return_elements=[
params['bottleneck_tensor_name'],
params['resized_input_tensor_name'],
'DecodeJpeg:0',
]))
return tf.estimator.export.ServingInputReceiver(bottleneck_tensor, {
INPUT_TENSOR_NAME: input_tensor
})
答案 0 :(得分:0)
SageMaker现在支持批量预测,这可能是完成此任务的更简单方法。有关更多信息,请访问:https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html
简而言之:
要执行批处理转换,请创建一个转换作业,其中包括以下信息: