了解Google AI平台自定义预测例程的输入 [英] Understanding inputs for google ai platform custom prediction routines
问题描述
我正在按照此文档进行自定义预测例程,我试图了解自定义预测例程的输入是什么样子.发送输入的代码如下:
I am following this documentation on custom prediction routines and I am trying to understand how the inputs for custom prediction routine looks like. The code to send the input looks like this:
instances = [
[6.7, 3.1, 4.7, 1.5],
[4.6, 3.1, 1.5, 0.2],
]
service = discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
和Predictor.py
目前非常简单.我只是想了解输入的样子...
and the Predictor.py
at the moment is very simple. I am just trying to understand how the input looks like...
class Predictor(object):
"""An example Predictor for an AI Platform custom prediction routine."""
def __init__(self, model):
self._model = model
def predict(self, instances, **kwargs):
inputs = np.asarray(instances)
if kwargs.get('max'):
return np.argmax(inputs, axis=1)
return np.sum(inputs)
@classmethod
def from_path(cls, model_dir):
return cls(None)
但是当我尝试获取响应时,出现以下错误:
But when I try to get the response i get the following error:
{
"error": "Prediction failed: unknown error."
}
此外,调试代码非常困难,因为无法进入代码或打印日志...我不知道发生了什么...输入是什么样子?我应该如何访问它们? 这只是一个简单的测试,但是最终我要发送图像,因此调试起来会更加困难.我将如何收到它们?如何在预处理器中对其进行预处理?假设我在训练时所做的处理看起来像这样
Furthermore it is extremely difficult to debug the code, because there is no way to step into the code or print logs... I have no idea what's going on... How the input looks like? how should i access them? This is just a simple test, but eventually I want to send images, it will be even more difficult to debug then. How will I receive them? How will I preprocess them in the preprocessor? Let's assume that the proporcessing i have done at training time looks like this
data = cv2.imread(str(img_path))
data = cv2.resize(data, (224, 224))
data = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
x = data.astype(np.float32) / 255.
return np.expand_dims(x, axis=0)
instances
对象的外观如何,因此我可以相应地构造预处理器?
预先谢谢你.
How the instances
object looks like so i can construct the preprocessor accordingly?
thank you in advance.
推荐答案
在没有模型的情况下使用调试代码(在本文发布时)似乎不起作用.我使用以下代码使所有功能均可用于我的图像预测用例:
It looks like that using debug code (at the time of this post) without a model do not work. I used the following code to have everything worked for my image prediction use case:
image_filename = 'your image path'
PROJECT_ID = ''
MODEL_NAME = ''
VERSION_NAME = ''
img = base64.b64encode(open(image_filename, "rb").read()).decode()
image_bite_dict = {"key": "0", "image_bytes": {"b64": img}}
instances = [
image_bite_dict
]
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}/versions/{}'.format(PROJECT_ID, MODEL_NAME, VERSION_NAME)
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
这篇关于了解Google AI平台自定义预测例程的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!