【问题标题】:How do I use Google Cloud Machine Learning Engine Client Library for Java for prediction如何使用 Google Cloud Machine Learning Engine Client Library for Java 进行预测
【发布时间】:2017-12-15 23:56:29
【问题描述】:

我在 Goggle Cloud 平台上上传了一个有效的 ML 模型(通过 python 和 gcloud ml-engine predict 测试)。

我目前正在尝试使用这个库从 Android 获得预测:Client Library for Java 和这个javadoc。 我在 AsyncTask 中使用服务帐户进行访问和 Android 代码,如下所示:

 JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            HttpTransport httpTransport = new com.google.api.client.http.javanet.NetHttpTransport();
            GoogleCredential credential = GoogleCredential.fromStream(is, httpTransport, jsonFactory);
            CloudMachineLearningEngine ml = new CloudMachineLearningEngine.Builder(httpTransport,jsonFactory,credential)
                    .setApplicationName("myCloudApplication")
                    .build();
            Log.i(TAG,"Successfully set up !!");

is 是包含我的服务帐户密钥的 json 文件的 InputStream。 我已经尝试了很多从这里得到的东西来对我训练有素的 ML 模型进行预测。我找不到任何在线示例。 这甚至可能吗?

非常感谢所有帮助。

【问题讨论】:

  • 您介意指定您遇到的错误吗?这将有助于确定问题是凭据还是其他原因。
  • 我没有错误,严格来说,我只是不明白如何在代码中使用 ml 对象。

标签: android google-cloud-ml


【解决方案1】:

这是绝对支持的。来自this sample

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.UriTemplate;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.discovery.Discovery;
import com.google.api.services.discovery.model.JsonSchema;
import com.google.api.services.discovery.model.RestDescription;
import com.google.api.services.discovery.model.RestMethod;
import java.io.File;

/*
 * Sample code for doing Cloud Machine Learning Engine online prediction in Java.
 */
public class OnlinePredictionSample {

  public static void main(String[] args) throws Exception {

    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    Discovery discovery = new Discovery.Builder(httpTransport, jsonFactory, null).build();

    RestDescription api = discovery.apis().getRest("ml", "v1").execute();
    RestMethod method = api.getResources().get("projects").getMethods().get("predict");

    JsonSchema param = new JsonSchema();
    String projectId = "YOUR_PROJECT_ID";
    // You should have already deployed a model and a version.
    // For reference, see https://cloud.google.com/ml-engine/docs/how-tos/deploying-models.
    String modelId = "YOUR_MODEL_ID";
    String versionId = "YOUR_VERSION_ID";
    param.set(
        "name", String.format("projects/%s/models/%s/versions/%s", projectId, modelId, versionId));

    GenericUrl url =
        new GenericUrl(UriTemplate.expand(api.getBaseUrl() + method.getPath(), param, true));
    System.out.println(url);

    String contentType = "application/json";
    File requestBodyFile = new File("input.txt");
    HttpContent content = new FileContent(contentType, requestBodyFile);
    System.out.println(content.getLength());

    GoogleCredential credential = GoogleCredential.getApplicationDefault();
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);
    HttpRequest request = requestFactory.buildRequest(method.getHttpMethod(), url, content);

    String response = request.execute().parseAsString();
    System.out.println(response);
  }
}

【讨论】:

  • 谢谢,但该解决方案不使用 com.google.api.services.ml.v1.CloudMachineLearningEngine API。
  • 要求严格吗?上面的代码在Android中不起作用吗?以上是我们目前唯一公开的 Java 示例。其他客户端往往会出现问题,因为我们在 API 中使用了一些高级功能。
  • 不,这不是一个严格的要求,但我对使用现有的 API 很感兴趣,因为我认为它比您建议的一般方法更方便。您提到的解决方案不使用 Service Account 或者这意味着据我了解您不能使用 GoogleNetHttpTransport.newTrustedTransport();也不是 GoogleNetHttpTransport.newTrustedTransport();方法。
  • 我的意思是“也不是 GoogleCredential.getApplicationDefault(); 方法”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 2016-09-26
  • 2018-04-19
相关资源
最近更新 更多