预先感谢您帮助解决此问题。我在 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:调用 InvokeEndpoint 操作时发生错误 (ModelError):收到来自模型的服务器错误 (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
})