如何使用Google Cloud Machine Learning Engine Java客户端库进行预测 [英] How do I use Google Cloud Machine Learning Engine Client Library for Java for prediction

查看:224
本文介绍了如何使用Google Cloud Machine Learning Engine Java客户端库进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Goggle Cloud平台上有一个正在上传的ML模型(通过python和gcloud ml-engine预报测试).

I have a working uploaded ML-model on Goggle Cloud platform (Tested via python and gcloud ml-engine predict).

我目前正在尝试使用以下库从Android获得预测: > javadoc . 我在如下所示的AsyncTask中使用服务帐户进行访问和Android代码:

I am currently trying to get predictions from Android using this library: Client Library for Java with this javadoc. I use a service account for access and Android code in a AsyncTask that looks like this:

 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 !!");

是包含我的服务帐户密钥的json文件的InputStream. 我已经尝试了很多事情,可以从我训练有素的ML模型做出预测.我找不到任何在线示例. 这有可能吗?

is is the InputStream to the json file containing my Service Account Key. I have tried many things getting from here to make predictions against my trained ML-model. I can't find any online examples. Is this even possible?

我们非常感谢所有帮助.

All help is deeply appreciated.

推荐答案

绝对支持.来自此示例:

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);
  }
}

这篇关于如何使用Google Cloud Machine Learning Engine Java客户端库进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆