Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing samples for classification and rename object detection sa… #1604

Merged
merged 4 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.samples.automl;

// [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) {
// 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");
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_vision_classification_deploy_model]
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.samples.automl;

// [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) {
// 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");
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_vision_classification_deploy_model_node_count]
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.samples.automl;

// [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) {
// 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");
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_vision_classification_undeploy_model]
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class DeployModelNodeCount {
class ObjectDetectionDeployModelNodeCount {

static void deployModelNodeCount(String projectId, String modelId) {
static void objectDetectionDeployModelNodeCount(String projectId, String modelId) {
// String projectId = "YOUR_PROJECT_ID";
// String modelId = "YOUR_MODEL_ID";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.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;

public class ClassificationDeployModelIT {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String MODEL_ID = "ICN3125115511348658176";
private ByteArrayOutputStream bout;
private PrintStream out;

@Before
public void setUp() {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testClassificationDeployModelApi() {
ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID);

String got = bout.toString();
assertThat(got).contains("Model deployment finished");

ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID);

got = bout.toString();
assertThat(got).contains("Model undeploy finished");
}

@Test
public void testClassificationDeployModelNodeCountApi() {
ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID);

String got = bout.toString();
assertThat(got).contains("Model deployment on 2 nodes finished");

ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID);

got = bout.toString();
assertThat(got).contains("Model undeploy finished");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/** Tests for vision "Deploy Model Node Count" sample. */
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class DeployModelNodeCountIT {
public class ObjectDetectionDeployModelNodeCountIT {
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
private static final String MODEL_ID = "IOD1854128448151224320";
private ByteArrayOutputStream bout;
Expand All @@ -64,8 +64,8 @@ public void tearDown() throws IOException, InterruptedException, ExecutionExcept
}

@Test
public void testModelApi() {
DeployModelNodeCount.deployModelNodeCount(PROJECT_ID, MODEL_ID);
public void testObjectDetectionDeployModelNodeCountApi() {
ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID);

String got = bout.toString();
assertThat(got).contains("Model deployment on 2 nodes finished");
Expand Down