Google云端机器学习API DotNet客户端 - 预测请求失败
问题描述:
我在Google云端平台中部署了一个训练有素的模型(CNN),我可以使用Python客户端库或gcloud
命令获得预测结果。Google云端机器学习API DotNet客户端 - 预测请求失败
我现在想用点网客户端V1.25(https://github.com/google/google-api-dotnet-client/tree/v1.25.0)得到的预测,但请求与{"error": "Missing "instances" field in request body."}
失败即使我发送JSON的形式为:
{"instances": [{"image":<base64ImageData>, "key":"1"}]}
我可以使用库来获取使用List()
方法的可用模型列表。
代码如下:
using System;
using System.Text;
using Google.Apis.Auth.OAuth2;
using System.IO;
using Google.Apis.Services;
using Google.Apis.CloudMachineLearningEngine.v1beta1.Data;
using Newtonsoft.Json;
namespace GoogleCloudTesting
{
Class Program
{
static void Main(string[] args)
{
GoogleCredential credential = GoogleCredential.GetApplicationDefaultAsync().Result;
var service = new Google.Apis.CloudMachineLearningEngine.v1beta1.CloudMachineLearningEngineService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Testing"
}
);
string jsonImagesPath = @"c:\path\to\images.json"; // {"instances": [{"image":<base64imagedata>, "key":"1"}]}
string json = File.ReadAllText(jsonImagesPath);
var request = new GoogleCloudMlV1beta1PredictRequest
{
HttpBody = new GoogleApiHttpBody { Data = json }
};
var predictRequest = service.Projects.Predict(request, "projects/my_project/models/my_model/versions/V1");
var result = predictRequest.Execute();
Console.WriteLine(result.Data); // null
}
}
}
赞赏任何帮助,谢谢。
https://cloud.google.com/ml-engine/reference/rest/v1/projects/predict建议base64编码数据必须由JSON对象替换具有名为'b64'的单个属性。所以也许你需要使用:{“instances”:[{“image”:{“b64”:“”},“key”:“1”}]} –
Chris
与Python版本相比, https://cloud.google.com/ml-engine/docs/how-tos/online-predict#requesting_predictions),但我不知道.net库足够好以确定地修复它。不同之处在于HttpBody没有在Python中使用。相反,更像service.Projects.Predict(body = json,name =“projects/my_project/models/my_model/versions/V1”)。再说一遍,我不能保证那会工作,但我很确定问题在于HttpBody。 – rhaertel80
请按照此处的说明操作:https://googlecloudplatform.github.io/google-cloud-dotnet/docs/faq.html#how-can-i-trace-requests-and-responses-in-rest-based-apis转储HTTP请求标头和正文。然后,在这里发布转储。 –