Skip to content

Commit

Permalink
chore: version bump and clean up automl vision samples (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhuushmgl authored and anguillanneuf committed Dec 5, 2022
1 parent 51876ae commit c02a1d0
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

// [START automl_vision_classification_deploy_model]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.DeployModelRequest;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.DeployModelRequest;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.OperationMetadata;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

// [START automl_vision_classification_undeploy_model]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.cloud.automl.v1beta1.UndeployModelRequest;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.OperationMetadata;
import com.google.cloud.automl.v1.UndeployModelRequest;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,13 @@

// Imports the Google Cloud client library
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics;
import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics.ConfidenceMetricsEntry;
import com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata;
import com.google.cloud.automl.v1beta1.ListModelEvaluationsRequest;
import com.google.cloud.automl.v1beta1.ListModelsRequest;
import com.google.cloud.automl.v1beta1.LocationName;
import com.google.cloud.automl.v1beta1.Model;
import com.google.cloud.automl.v1beta1.ModelEvaluation;
import com.google.cloud.automl.v1beta1.ModelEvaluationName;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.longrunning.Operation;
import com.google.protobuf.Empty;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.ImageClassificationModelMetadata;
import com.google.cloud.automl.v1.LocationName;
import com.google.cloud.automl.v1.Model;
import com.google.cloud.automl.v1.OperationMetadata;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import net.sourceforge.argparse4j.inf.Subparsers;

/**
* Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java
Expand All @@ -64,7 +48,7 @@ static void createModel(
String computeRegion,
String dataSetId,
String modelName,
String trainBudget) {
String trainBudget) throws IOException, ExecutionException, InterruptedException {
// Instantiates a client
try (AutoMlClient client = AutoMlClient.create()) {

Expand All @@ -76,7 +60,7 @@ static void createModel(
Long.valueOf(trainBudget) == 0
? ImageClassificationModelMetadata.newBuilder().build()
: ImageClassificationModelMetadata.newBuilder()
.setTrainBudget(Long.valueOf(trainBudget))
.setTrainBudgetMilliNodeHours(Long.valueOf(trainBudget))
.build();

// Set model name and model metadata for the image dataset.
Expand All @@ -95,49 +79,7 @@ static void createModel(
String.format(
"Training operation name: %s", response.getInitialFuture().get().getName()));
System.out.println("Training started...");
} catch (IOException | ExecutionException | InterruptedException e) {
e.printStackTrace();
}
}
// [END automl_vision_create_model]

public static void main(String[] args) {
argsHelper(args);
}

static void argsHelper(String[] args) {
ArgumentParser parser =
ArgumentParsers.newFor("ModelApi")
.build()
.defaultHelp(true)
.description("Model API operations.");
Subparsers subparsers = parser.addSubparsers().dest("command");

Subparser createModelParser = subparsers.addParser("create_model");
createModelParser.addArgument("datasetId");
createModelParser.addArgument("modelName");
createModelParser.addArgument("trainBudget");

String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
String computeRegion = System.getenv("REGION_NAME");

if (projectId == null || computeRegion == null) {
System.out.println("Set `GOOGLE_CLOUD_PROJECT` and `REGION_NAME` as specified in the README");
System.exit(-1);
}

try {
Namespace ns = parser.parseArgs(args);
if (ns.get("command").equals("create_model")) {
createModel(
projectId,
computeRegion,
ns.getString("datasetId"),
ns.getString("modelName"),
ns.getString("trainBudget"));
}
} catch (ArgumentParserException e) {
parser.handleError(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

// [START automl_vision_object_detection_deploy_model_node_count]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.DeployModelRequest;
import com.google.cloud.automl.v1beta1.ImageObjectDetectionModelDeploymentMetadata;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.DeployModelRequest;
import com.google.cloud.automl.v1.ImageObjectDetectionModelDeploymentMetadata;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.OperationMetadata;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@
package com.google.cloud.vision;

// Imports the Google Cloud client library
import com.google.cloud.automl.v1beta1.AnnotationPayload;
import com.google.cloud.automl.v1beta1.ExamplePayload;
import com.google.cloud.automl.v1beta1.Image;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.PredictResponse;
import com.google.cloud.automl.v1beta1.PredictionServiceClient;
import com.google.cloud.automl.v1.AnnotationPayload;
import com.google.cloud.automl.v1.ExamplePayload;
import com.google.cloud.automl.v1.Image;
import com.google.cloud.automl.v1.ModelName;
import com.google.cloud.automl.v1.PredictResponse;
import com.google.cloud.automl.v1.PredictionServiceClient;
import com.google.protobuf.ByteString;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;

/**
* Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java
Expand All @@ -65,7 +61,7 @@ static void predict(
String computeRegion,
String modelId,
String filePath,
String scoreThreshold) {
String scoreThreshold) throws IOException {

// Instantiate client for prediction service.
try (PredictionServiceClient predictionClient = PredictionServiceClient.create()) {
Expand All @@ -92,45 +88,7 @@ static void predict(
System.out.println(
"Predicted class score :" + annotationPayload.getClassification().getScore());
}
} catch (IOException e) {
e.printStackTrace();
}
}
// [END automl_vision_predict]

public static void main(String[] args) {
argsHelper(args);
}

static void argsHelper(String[] args) {
ArgumentParser parser =
ArgumentParsers.newFor("PredictionApi")
.build()
.defaultHelp(true)
.description("Prediction API Operation");

parser.addArgument("modelId").required(true);
parser.addArgument("filePath").required(true);
parser.addArgument("scoreThreshold").nargs("?").type(String.class).setDefault("");

String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
String computeRegion = System.getenv("REGION_NAME");

if (projectId == null || computeRegion == null) {
System.out.println("Set `GOOGLE_CLOUD_PROJECT` and `REGION_NAME` as specified in the README");
System.exit(-1);
}

try {
Namespace ns = parser.parseArgs(args);
predict(
projectId,
computeRegion,
ns.getString("modelId"),
ns.getString("filePath"),
ns.getString("scoreThreshold"));
} catch (ArgumentParserException e) {
parser.handleError(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,4 @@ public void testClassificationUndeployModelApi() {
assertThat(e.getMessage()).contains("The model does not exist");
}
}

@Test
public void testClassificationDeployModelNodeCountApi() {
// As model deployment can take a long time, instead try to deploy a
// nonexistent model and confirm that the model was not found, but other
// elements of the request were valid.
try {
ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID);
String got = bout.toString();
assertThat(got).contains("The model does not exist");
} catch (IOException | ExecutionException | InterruptedException e) {
assertThat(e.getMessage()).contains("The model does not exist");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.DeployModelRequest;
import com.google.cloud.automl.v1beta1.Model;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.DeployModelRequest;
import com.google.cloud.automl.v1.Model;
import com.google.cloud.automl.v1.ModelName;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -77,7 +77,7 @@ public void tearDown() {
}

@Test
public void testPredict() {
public void testPredict() throws IOException {
PredictionApi.predict(PROJECT_ID, COMPUTE_REGION, modelId, filePath, scoreThreshold);
String got = bout.toString();
assertThat(got).contains("dandelion");
Expand Down

0 comments on commit c02a1d0

Please sign in to comment.