2
我在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請求標頭和正文。然後,在這裏發佈轉儲。 –