From 0dc66ce82611147edc27a337007c15cf220c6bc1 Mon Sep 17 00:00:00 2001 From: Anthony Wens Date: Tue, 5 May 2020 16:39:20 -0700 Subject: [PATCH 1/4] automl_vision: remove unused dataset samples --- .../vision/samples/automl/DatasetApi.java | 356 ------------------ 1 file changed, 356 deletions(-) delete mode 100644 vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DatasetApi.java diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DatasetApi.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DatasetApi.java deleted file mode 100644 index bcf5ec74614..00000000000 --- a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DatasetApi.java +++ /dev/null @@ -1,356 +0,0 @@ -/* - * 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.samples.automl; - -// Imports the Google Cloud client library -import com.google.cloud.automl.v1beta1.AutoMlClient; -import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType; -import com.google.cloud.automl.v1beta1.Dataset; -import com.google.cloud.automl.v1beta1.DatasetName; -import com.google.cloud.automl.v1beta1.GcsDestination; -import com.google.cloud.automl.v1beta1.GcsSource; -import com.google.cloud.automl.v1beta1.ImageClassificationDatasetMetadata; -import com.google.cloud.automl.v1beta1.InputConfig; -import com.google.cloud.automl.v1beta1.ListDatasetsRequest; -import com.google.cloud.automl.v1beta1.LocationName; -import com.google.cloud.automl.v1beta1.OutputConfig; -import com.google.protobuf.Empty; -import java.io.IOException; -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.DatasetAPI' -Dexec.args='create_dataset - * test_dataset' - */ -public class DatasetApi { - - // [START automl_vision_create_dataset] - /** - * Demonstrates using the AutoML client to create a dataset - * - * @param projectId the Google Cloud Project ID. - * @param computeRegion the Region name. (e.g., "us-central1") - * @param datasetName the name of the dataset to be created. - * @param multiLabel the type of classification problem. Set to FALSE by default. False - - * MULTICLASS , True - MULTILABEL - */ - static void createDataset( - String projectId, String computeRegion, String datasetName, boolean multiLabel) { - - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Classification type assigned based on multiLabel value. - ClassificationType classificationType = - multiLabel ? ClassificationType.MULTILABEL : ClassificationType.MULTICLASS; - - // Specify the image classification type for the dataset. - ImageClassificationDatasetMetadata imageClassificationDatasetMetadata = - ImageClassificationDatasetMetadata.newBuilder() - .setClassificationType(classificationType) - .build(); - - // Set dataset with dataset name and set the dataset metadata. - Dataset myDataset = - Dataset.newBuilder() - .setDisplayName(datasetName) - .setImageClassificationDatasetMetadata(imageClassificationDatasetMetadata) - .build(); - - // Create dataset with the dataset metadata in the region. - Dataset dataset = client.createDataset(projectLocation, myDataset); - - // Display the dataset information - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Image classification dataset specification:"); - System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata())); - System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_create_dataset] - - // [START automl_vision_list_datasets] - /** - * Demonstrates using the AutoML client to list all datasets. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param filter the Filter expression. - */ - static void listDatasets(String projectId, String computeRegion, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Build the List datasets request - ListDatasetsRequest request = - ListDatasetsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); - - // List all the datasets available in the region by applying the filter. - System.out.print("List of datasets:"); - for (Dataset dataset : client.listDatasets(request).iterateAll()) { - // Display the dataset information - System.out.println(String.format("\nDataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Image classification dataset specification:"); - System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata())); - System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_list_datasets] - - // [START automl_vision_get_dataset] - /** - * Demonstrates using the AutoML client to get a dataset by ID. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset. - */ - static void getDataset(String projectId, String computeRegion, String datasetId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Get all the information about a given dataset. - Dataset dataset = client.getDataset(datasetFullId); - - // Display the dataset information. - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Image classification dataset specification:"); - System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata())); - System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_dataset] - - // [START automl_vision_import_data] - /** - * Demonstrates using the AutoML client to import labeled images. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset to which the training data will be imported. - * @param path the Google Cloud Storage URIs. Target files must be in AutoML vision CSV format. - */ - static void importData(String projectId, String computeRegion, String datasetId, String path) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - GcsSource.Builder gcsSource = GcsSource.newBuilder(); - - // Get multiple training data files to be imported - String[] inputUris = path.split(","); - for (String inputUri : inputUris) { - gcsSource.addInputUris(inputUri); - } - - // Import data from the input URI - InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); - System.out.println("Processing import..."); - Empty response = client.importDataAsync(datasetFullId.toString(), inputConfig).get(); - System.out.println(String.format("Dataset imported. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_import_data] - - // [START automl_vision_export_data] - /** - * Demonstrates using the AutoML client to export a dataset to a Google Cloud Storage bucket. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset. - * @param gcsUri the Destination URI (Google Cloud Storage) - */ - static void exportData(String projectId, String computeRegion, String datasetId, String gcsUri) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Set the output URI - GcsDestination gcsDestination = - GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build(); - - // Export the dataset to the output URI. - OutputConfig outputConfig = - OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); - System.out.println("Processing export..."); - - Empty response = client.exportDataAsync(datasetFullId, outputConfig).get(); - System.out.println(String.format("Dataset exported. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_export_data] - - // [START automl_vision_delete_dataset] - /** - * Delete a dataset. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param datasetId the Id of the dataset. - */ - static void deleteDataset(String projectId, String computeRegion, String datasetId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Delete a dataset. - Empty response = client.deleteDatasetAsync(datasetFullId).get(); - - System.out.println(String.format("Dataset deleted. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_delete_dataset] - - public static void main(String[] args) { - argsHelper(args); - } - - static void argsHelper(String[] args) { - ArgumentParser parser = - ArgumentParsers.newFor("DatasetApi") - .build() - .defaultHelp(true) - .description("Dataset API operations."); - Subparsers subparsers = parser.addSubparsers().dest("command"); - - Subparser createDatasetParser = subparsers.addParser("create_dataset"); - createDatasetParser.addArgument("datasetName"); - createDatasetParser - .addArgument("multiLabel") - .nargs("?") - .type(Boolean.class) - .choices(Boolean.FALSE, Boolean.TRUE) - .setDefault(Boolean.FALSE); - - Subparser listDatasetsParser = subparsers.addParser("list_datasets"); - listDatasetsParser - .addArgument("filter") - .nargs("?") - .setDefault("imageClassificationDatasetMetadata:*"); - - Subparser getDatasetParser = subparsers.addParser("get_dataset"); - getDatasetParser.addArgument("datasetId"); - - Subparser importDataParser = subparsers.addParser("import_data"); - importDataParser.addArgument("datasetId"); - importDataParser.addArgument("path"); - - Subparser exportDataParser = subparsers.addParser("export_data"); - exportDataParser.addArgument("datasetId"); - exportDataParser.addArgument("gcsUri"); - - Subparser deleteDatasetParser = subparsers.addParser("delete_dataset"); - deleteDatasetParser.addArgument("datasetId"); - - 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_dataset")) { - createDataset( - projectId, computeRegion, ns.getString("datasetName"), ns.getBoolean("multiLabel")); - } - if (ns.get("command").equals("list_datasets")) { - listDatasets(projectId, computeRegion, ns.getString("filter")); - } - if (ns.get("command").equals("get_dataset")) { - getDataset(projectId, computeRegion, ns.getString("datasetId")); - } - if (ns.get("command").equals("import_data")) { - importData(projectId, computeRegion, ns.getString("datasetId"), ns.getString("path")); - } - if (ns.get("command").equals("export_data")) { - exportData(projectId, computeRegion, ns.getString("datasetId"), ns.getString("gcsUri")); - } - if (ns.get("command").equals("delete_dataset")) { - deleteDataset(projectId, computeRegion, ns.getString("datasetId")); - } - - } catch (ArgumentParserException e) { - parser.handleError(e); - } - } -} From d922fd58cd40bc3d232543b72b51ac1fa071dba4 Mon Sep 17 00:00:00 2001 From: Anthony Wens Date: Tue, 5 May 2020 16:39:45 -0700 Subject: [PATCH 2/4] automl_vision: remove tests for unused dataset samples --- .../vision/samples/automl/DatasetApiIT.java | 109 ------------------ 1 file changed, 109 deletions(-) delete mode 100644 vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DatasetApiIT.java diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DatasetApiIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DatasetApiIT.java deleted file mode 100644 index 04cc0a6f4d2..00000000000 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DatasetApiIT.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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.samples.automl; - -import static com.google.common.truth.Truth.assertThat; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for Automl vision "Dataset API" sample. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class DatasetApiIT { - - private static final String PROJECT_ID = "java-docs-samples-testing"; - private static final String BUCKET = PROJECT_ID + "-vcm"; - private static final String COMPUTE_REGION = "us-central1"; - private ByteArrayOutputStream bout; - private PrintStream out; - private String datasetId; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); - } - - @After - public void tearDown() { - System.setOut(null); - } - - @Test - public void testCreateImportDeleteDataset() { - // Create a random dataset name with a length of 32 characters (max allowed by AutoML) - // To prevent name collisions when running tests in multiple java versions at once. - // AutoML doesn't allow "-", but accepts "_" - String datasetName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); - // Act - DatasetApi.createDataset(PROJECT_ID, COMPUTE_REGION, datasetName, false); - - // Assert - String got = bout.toString(); - datasetId = - bout.toString() - .split("\n")[0] - .split("/")[(bout.toString().split("\n")[0]).split("/").length - 1]; - assertThat(got).contains("Dataset id:"); - - // Act - DatasetApi.importData( - PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/flower_traindata.csv"); - - // Assert - got = bout.toString(); - assertThat(got).contains("Dataset id:"); - - // Act - DatasetApi.deleteDataset(PROJECT_ID, COMPUTE_REGION, datasetId); - - // Assert - got = bout.toString(); - assertThat(got).contains("Dataset deleted."); - } - - @Test - public void testListGetDatasets() { - // Act - DatasetApi.listDatasets(PROJECT_ID, COMPUTE_REGION, "imageClassificationDatasetMetadata:*"); - - // Assert - String got = bout.toString(); - datasetId = - bout.toString() - .split("\n")[1] - .split("/")[(bout.toString().split("\n")[1]).split("/").length - 1]; - assertThat(got).contains("Dataset id:"); - - // Act - DatasetApi.getDataset(PROJECT_ID, COMPUTE_REGION, datasetId); - - // Assert - got = bout.toString(); - - assertThat(got).contains("Dataset id:"); - } -} From e236d6e68e0abeba55a560bef70197afec89ebc9 Mon Sep 17 00:00:00 2001 From: Anthony Wens Date: Tue, 5 May 2020 16:44:53 -0700 Subject: [PATCH 3/4] automl_vision: remove unused model samples --- .../cloud/vision/samples/automl/ModelApi.java | 332 ------------------ 1 file changed, 332 deletions(-) diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java index 9a89c215d40..4b07977df3e 100644 --- a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ModelApi.java @@ -101,287 +101,6 @@ static void createModel( } // [END automl_vision_create_model] - // [START automl_vision_get_operation_status] - /** - * Demonstrates using the AutoML client to get operation status. - * - * @param operationFullId the complete name of a operation. For example, the name of your - * operation is projects/[projectId]/locations/us-central1/operations/[operationId]. - */ - static void getOperationStatus(String operationFullId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the latest state of a long-running operation. - Operation response = client.getOperationsClient().getOperation(operationFullId); - - System.out.println(String.format("Operation status: %s", response)); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_operation_status] - - // [START automl_vision_list_models] - /** - * Demonstrates using the AutoML client to list all models. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param filter - Filter expression. - */ - static void listModels(String projectId, String computeRegion, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Create list models request - ListModelsRequest listModelsRequest = - ListModelsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); - - System.out.println("List of models:"); - for (Model model : client.listModels(listModelsRequest).iterateAll()) { - // Display the model information. - System.out.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Image classification model metadata:"); - System.out.println( - "Tranning budget: " + model.getImageClassificationModelMetadata().getTrainBudget()); - System.out.println( - "Tranning cost: " + model.getImageClassificationModelMetadata().getTrainCost()); - System.out.println( - String.format( - "Stop reason: %s", model.getImageClassificationModelMetadata().getStopReason())); - System.out.println( - String.format( - "Base model id: %s", model.getImageClassificationModelMetadata().getBaseModelId())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_list_models] - - // [START automl_vision_get_model] - /** - * Demonstrates using the AutoML client to get model details. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - */ - static void getModel(String projectId, String computeRegion, String modelId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Get complete detail of the model. - Model model = client.getModel(modelFullId); - - // Display the model information. - System.out.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Image classification model metadata:"); - System.out.println( - "Tranning budget: " + model.getImageClassificationModelMetadata().getTrainBudget()); - System.out.println( - "Tranning cost:" + model.getImageClassificationModelMetadata().getTrainCost()); - System.out.println( - String.format( - "Stop reason: %s", model.getImageClassificationModelMetadata().getStopReason())); - System.out.println( - String.format( - "Base model id: %s", model.getImageClassificationModelMetadata().getBaseModelId())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_model] - - // [START automl_vision_list_model_evaluations] - /** - * Demonstrates using the AutoML client to list model evaluations. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - * @param filter the Filter expression. - */ - static void listModelEvaluations( - String projectId, String computeRegion, String modelId, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Create list model evaluations request - ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .setFilter(filter) - .build(); - - System.out.println("List of model evaluations:"); - // List all the model evaluations in the model by applying filter. - for (ModelEvaluation element : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { - System.out.println(element); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_list_model_evaluations] - - // [START automl_vision_get_model_evaluation] - /** - * Demonstrates using the AutoML client to get model evaluations. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - * @param modelEvaluationId the Id of your model evaluation. - */ - static void getModelEvaluation( - String projectId, String computeRegion, String modelId, String modelEvaluationId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model evaluation. - ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); - // Perform the AutoML Model request to get Model Evaluation information - ModelEvaluation response = client.getModelEvaluation(modelEvaluationFullId); - - System.out.println(response); - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_get_model_evaluation] - - // [START automl_vision_display_evaluation] - /** - * Demonstrates using the AutoML client to display model evaluation. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - * @param filter the filter expression. - */ - static void displayEvaluation( - String projectId, String computeRegion, String modelId, String filter) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // List all the model evaluations in the model by applying filter. - ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .setFilter(filter) - .build(); - - // Iterate through the results. - String modelEvaluationId = ""; - for (ModelEvaluation element : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { - if (element.getAnnotationSpecId() != null) { - modelEvaluationId = element.getName().split("/")[element.getName().split("/").length - 1]; - } - } - - // Resource name for the model evaluation. - ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); - - // Get a model evaluation. - ModelEvaluation modelEvaluation = client.getModelEvaluation(modelEvaluationFullId); - - ClassificationEvaluationMetrics classMetrics = - modelEvaluation.getClassificationEvaluationMetrics(); - List confidenceMetricsEntries = - classMetrics.getConfidenceMetricsEntryList(); - - // Showing model score based on threshold of 0.5 - for (ConfidenceMetricsEntry confidenceMetricsEntry : confidenceMetricsEntries) { - if (confidenceMetricsEntry.getConfidenceThreshold() == 0.5) { - System.out.println("Precision and recall are based on a score threshold of 0.5"); - System.out.println( - String.format("Model Precision: %.2f ", confidenceMetricsEntry.getPrecision() * 100) - + '%'); - System.out.println( - String.format("Model Recall: %.2f ", confidenceMetricsEntry.getRecall() * 100) + '%'); - System.out.println( - String.format("Model F1 score: %.2f ", confidenceMetricsEntry.getF1Score() * 100) - + '%'); - System.out.println( - String.format( - "Model Precision@1: %.2f ", confidenceMetricsEntry.getPrecisionAt1() * 100) - + '%'); - System.out.println( - String.format("Model Recall@1: %.2f ", confidenceMetricsEntry.getRecallAt1() * 100) - + '%'); - System.out.println( - String.format("Model F1 score@1: %.2f ", confidenceMetricsEntry.getF1ScoreAt1() * 100) - + '%'); - } - } - } catch (IOException e) { - e.printStackTrace(); - } - } - // [END automl_vision_display_evaluation] - - // [START automl_vision_delete_model] - /** - * Demonstrates using the AutoML client to delete a model. - * - * @param projectId the Id of the project. - * @param computeRegion the Region name. - * @param modelId the Id of the model. - */ - static void deleteModel(String projectId, String computeRegion, String modelId) { - // Instantiates a client - try (AutoMlClient client = AutoMlClient.create()) { - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Delete a model. - Empty response = client.deleteModelAsync(modelFullId).get(); - - System.out.println("Model deletion started..."); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } - // [END automl_vision_delete_model] - public static void main(String[] args) { argsHelper(args); } @@ -399,33 +118,6 @@ static void argsHelper(String[] args) { createModelParser.addArgument("modelName"); createModelParser.addArgument("trainBudget"); - Subparser listModelParser = subparsers.addParser("list_models"); - listModelParser - .addArgument("filter") - .nargs("?") - .setDefault("imageClassificationModelMetadata:*"); - - Subparser getModelParser = subparsers.addParser("get_model"); - getModelParser.addArgument("modelId"); - - Subparser listModelEvaluationsParser = subparsers.addParser("list_model_evaluations"); - listModelEvaluationsParser.addArgument("modelId"); - listModelEvaluationsParser.addArgument("filter").nargs("?").setDefault(""); - - Subparser getModelEvaluationParser = subparsers.addParser("get_model_evaluation"); - getModelEvaluationParser.addArgument("modelId"); - getModelEvaluationParser.addArgument("modelEvaluationId"); - - Subparser displayEvaluationParser = subparsers.addParser("display_evaluation"); - displayEvaluationParser.addArgument("modelId"); - displayEvaluationParser.addArgument("filter").nargs("?").setDefault(""); - - Subparser deleteModelParser = subparsers.addParser("delete_model"); - deleteModelParser.addArgument("modelId"); - - Subparser getOperationStatusParser = subparsers.addParser("get_operation_status"); - getOperationStatusParser.addArgument("operationFullId"); - String projectId = System.getenv("GOOGLE_CLOUD_PROJECT"); String computeRegion = System.getenv("REGION_NAME"); @@ -444,30 +136,6 @@ static void argsHelper(String[] args) { ns.getString("modelName"), ns.getString("trainBudget")); } - if (ns.get("command").equals("list_models")) { - listModels(projectId, computeRegion, ns.getString("filter")); - } - if (ns.get("command").equals("get_model")) { - getModel(projectId, computeRegion, ns.getString("modelId")); - } - if (ns.get("command").equals("list_model_evaluations")) { - listModelEvaluations( - projectId, computeRegion, ns.getString("modelId"), ns.getString("filter")); - } - if (ns.get("command").equals("get_model_evaluation")) { - getModelEvaluation( - projectId, computeRegion, ns.getString("modelId"), ns.getString("modelEvaluationId")); - } - if (ns.get("command").equals("display_evaluation")) { - displayEvaluation( - projectId, computeRegion, ns.getString("modelId"), ns.getString("filter")); - } - if (ns.get("command").equals("delete_model")) { - deleteModel(projectId, computeRegion, ns.getString("modelId")); - } - if (ns.get("command").equals("get_operation_status")) { - getOperationStatus(ns.getString("operationFullId")); - } } catch (ArgumentParserException e) { parser.handleError(e); } From 1462bf8a79b0606025df891539b02fcde5e7800f Mon Sep 17 00:00:00 2001 From: Anthony Wens Date: Wed, 6 May 2020 13:04:30 -0700 Subject: [PATCH 4/4] automl: delete obviated tests --- .../vision/samples/automl/ModelApiIT.java | 85 ------------------- 1 file changed, 85 deletions(-) delete mode 100644 vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ModelApiIT.java diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ModelApiIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ModelApiIT.java deleted file mode 100644 index 164c5e9586e..00000000000 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ModelApiIT.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.samples.automl; - -import static com.google.common.truth.Truth.assertThat; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Tests for vision "Model API" sample. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class ModelApiIT { - private static final String PROJECT_ID = "java-docs-samples-testing"; - private static final String COMPUTE_REGION = "us-central1"; - private ByteArrayOutputStream bout; - private PrintStream out; - private String modelId; - private String modelEvaluationId; - - @Before - public void setUp() { - - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - System.setOut(out); - } - - @After - public void tearDown() { - System.setOut(null); - } - - @Test - public void testModelApi() { - // Act - ModelApi.listModels(PROJECT_ID, COMPUTE_REGION, ""); - - // Assert - String got = bout.toString(); - modelId = got.split("\n")[1].split("/")[got.split("\n")[1].split("/").length - 1]; - assertThat(got).contains("Model id:"); - - // Act - ModelApi.getModel(PROJECT_ID, COMPUTE_REGION, modelId); - - // Assert - got = bout.toString(); - assertThat(got).contains("Model name:"); - - // Act - ModelApi.listModelEvaluations(PROJECT_ID, COMPUTE_REGION, modelId, ""); - - // Assert - got = bout.toString(); - modelEvaluationId = got.split("List of model evaluations:")[1].split("\"")[1].split("/")[7]; - assertThat(got).contains("name:"); - - // Act - ModelApi.getModelEvaluation(PROJECT_ID, COMPUTE_REGION, modelId, modelEvaluationId); - - // Assert - got = bout.toString(); - assertThat(got).contains("name:"); - } -}