Skip to content

Commit

Permalink
chore: moving vision automl samples (#373)
Browse files Browse the repository at this point in the history
* chore: moving vision automl samples

* refactored printout stream

* fixed the lint

* lint
  • Loading branch information
munkhuushmgl authored Oct 30, 2020
1 parent d2132fa commit 3340eb5
Show file tree
Hide file tree
Showing 10 changed files with 765 additions and 0 deletions.
Binary file added automl/snippets/resources/dandelion.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.vision;

// [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.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class ClassificationDeployModel {

// Deploy a model
static void classificationDeployModel(String projectId, String modelId)
throws IOException, ExecutionException, InterruptedException {
// String projectId = "YOUR_PROJECT_ID";
// String modelId = "YOUR_MODEL_ID";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {

// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);

// Build deploy model request.
DeployModelRequest deployModelRequest =
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();

// Deploy a model with the deploy model request.
OperationFuture<Empty, OperationMetadata> future =
client.deployModelAsync(deployModelRequest);

future.get();

// Display the deployment details of model.
System.out.println("Model deployment finished");
}
}
}
// [END automl_vision_classification_deploy_model]
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.vision;

// [START automl_vision_classification_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.ImageClassificationModelDeploymentMetadata;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class ClassificationDeployModelNodeCount {

// Deploy a model with a specified node count
static void classificationDeployModelNodeCount(String projectId, String modelId)
throws IOException, ExecutionException, InterruptedException {
// String projectId = "YOUR_PROJECT_ID";
// String modelId = "YOUR_MODEL_ID";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);

// Set how many nodes the model is deployed on
ImageClassificationModelDeploymentMetadata deploymentMetadata =
ImageClassificationModelDeploymentMetadata.newBuilder().setNodeCount(2).build();

DeployModelRequest request =
DeployModelRequest.newBuilder()
.setName(modelFullId.toString())
.setImageClassificationModelDeploymentMetadata(deploymentMetadata)
.build();
// Deploy the model
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
future.get();
System.out.println("Model deployment on 2 nodes finished");
}
}
}
// [END automl_vision_classification_deploy_model_node_count]
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.vision;

// [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.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class ClassificationUndeployModel {

// Deploy a model
static void classificationUndeployModel(String projectId, String modelId)
throws IOException, ExecutionException, InterruptedException {
// String projectId = "YOUR_PROJECT_ID";
// String modelId = "YOUR_MODEL_ID";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {

// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);

// Build deploy model request.
UndeployModelRequest undeployModelRequest =
UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build();

// Deploy a model with the deploy model request.
OperationFuture<Empty, OperationMetadata> future =
client.undeployModelAsync(undeployModelRequest);

future.get();

// Display the deployment details of model.
System.out.println("Model undeploy finished");
}
}
}
// [END automl_vision_classification_undeploy_model]
143 changes: 143 additions & 0 deletions automl/snippets/src/main/java/com/google/cloud/vision/ModelApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.vision;

// 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 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
* -Dexec.mainClass ='com.google.cloud.vision.samples.automl.ModelApi' -Dexec.args='create_model
* [datasetId] test_model'
*/
public class ModelApi {

// [START automl_vision_create_model]
/**
* Demonstrates using the AutoML client to create a model.
*
* @param projectId the Id of the project.
* @param computeRegion the Region name.
* @param dataSetId the Id of the dataset to which model is created.
* @param modelName the Name of the model.
* @param trainBudget the Budget for training the model.
*/
static void createModel(
String projectId,
String computeRegion,
String dataSetId,
String modelName,
String trainBudget) {
// Instantiates a client
try (AutoMlClient client = AutoMlClient.create()) {

// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, computeRegion);

// Set model metadata.
ImageClassificationModelMetadata imageClassificationModelMetadata =
Long.valueOf(trainBudget) == 0
? ImageClassificationModelMetadata.newBuilder().build()
: ImageClassificationModelMetadata.newBuilder()
.setTrainBudget(Long.valueOf(trainBudget))
.build();

// Set model name and model metadata for the image dataset.
Model myModel =
Model.newBuilder()
.setDisplayName(modelName)
.setDatasetId(dataSetId)
.setImageClassificationModelMetadata(imageClassificationModelMetadata)
.build();

// Create a model with the model metadata in the region.
OperationFuture<Model, OperationMetadata> response =
client.createModelAsync(projectLocation, myModel);

System.out.println(
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
@@ -0,0 +1,60 @@
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.vision;

// [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.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class ObjectDetectionDeployModelNodeCount {

static void objectDetectionDeployModelNodeCount(String projectId, String modelId)
throws IOException, ExecutionException, InterruptedException {
// String projectId = "YOUR_PROJECT_ID";
// String modelId = "YOUR_MODEL_ID";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);

// Set how many nodes the model is deployed on
ImageObjectDetectionModelDeploymentMetadata deploymentMetadata =
ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build();

DeployModelRequest request =
DeployModelRequest.newBuilder()
.setName(modelFullId.toString())
.setImageObjectDetectionModelDeploymentMetadata(deploymentMetadata)
.build();
// Deploy the model
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
future.get();
System.out.println("Model deployment on 2 nodes finished");
}
}
}
// [END automl_vision_object_detection_deploy_model_node_count]
Loading

0 comments on commit 3340eb5

Please sign in to comment.