我在Google Cloud平台上部署了一个经过训练的模型(CNN),我可以使用Python客户端库或gcloud命令从该模型获得预测结果。
我现在正在尝试使用Dot Net client 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
}
}
}感谢您的帮助,谢谢。
发布于 2017-07-20 17:16:53
这是一个已知的问题。
github issue#1068中显示了两个解决方法
https://stackoverflow.com/questions/43638529
复制相似问题