-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add new sample for deploying a model with a node count #1601
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.samples.automl; | ||
|
||
// [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 DeployModelNodeCount { | ||
|
||
static void deployModelNodeCount(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 | ||
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"); | ||
} catch (IOException | InterruptedException | ExecutionException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
// [END automl_vision_object_detection_deploy_model_node_count] |
73 changes: 73 additions & 0 deletions
73
...n/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 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.protobuf.Empty; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
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 "Deploy Model Node Count" sample. */ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class DeployModelNodeCountIT { | ||
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); | ||
private static final String MODEL_ID = "IOD1854128448151224320"; | ||
private ByteArrayOutputStream bout; | ||
private PrintStream out; | ||
|
||
@Before | ||
public void setUp() { | ||
bout = new ByteArrayOutputStream(); | ||
out = new PrintStream(bout); | ||
System.setOut(out); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException, InterruptedException, ExecutionException { | ||
System.setOut(null); | ||
|
||
try (AutoMlClient client = AutoMlClient.create()) { | ||
OperationFuture<Empty, OperationMetadata> future = client.undeployModelAsync( | ||
ModelName.of(PROJECT_ID, "us-central1", MODEL_ID).toString()); | ||
|
||
future.get(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testModelApi() { | ||
DeployModelNodeCount.deployModelNodeCount(PROJECT_ID, MODEL_ID); | ||
|
||
String got = bout.toString(); | ||
assertThat(got).contains("Model deployment on 2 nodes finished"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.