Skip to content

Commit 3481fb3

Browse files
nnegreybcoe
authored and
Ace Nassri
committed
docs: add vision classification ga samples (#297)
* docs: add vision classification ga samples * lint fix * Fix field and add missing file * Increase timeout * Decrease time to run tests * lint * Update license headers * Update temp output file to not cause collisions * Use spawnsync instead of execsync for stderr output * Lint and license header fixes Co-authored-by: Benjamin E. Coe <[email protected]>
1 parent 382e528 commit 3481fb3

10 files changed

+466
-1
lines changed

automl/language_entity_extraction_predict.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
66
//
7-
// http://www.apache.org/licenses/LICENSE-2.0
7+
// https://www.apache.org/licenses/LICENSE-2.0
88
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,

automl/resources/test.png

1.97 MB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {after, describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
const uuid = require('uuid');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const CREATE_DATASET_REGION_TAG = 'vision_classification_create_dataset';
27+
const LOCATION = 'us-central1';
28+
29+
describe('Automl Vision Classification Create Dataset Test', () => {
30+
const client = new AutoMlClient();
31+
let datasetId;
32+
33+
it('should create a dataset', async () => {
34+
const projectId = await client.getProjectId();
35+
const displayName = `test_${uuid
36+
.v4()
37+
.replace(/-/g, '_')
38+
.substring(0, 26)}`;
39+
40+
// create
41+
const create_output = execSync(
42+
`node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}`
43+
);
44+
assert.match(create_output, /Dataset id:/);
45+
46+
datasetId = create_output.split('Dataset id: ')[1].split('\n')[0];
47+
});
48+
49+
after('delete created dataset', async () => {
50+
const projectId = await client.getProjectId();
51+
const request = {
52+
name: client.datasetPath(projectId, LOCATION, datasetId),
53+
};
54+
const [operation] = await client.deleteDataset(request);
55+
await operation.promise();
56+
});
57+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {after, describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const CREATE_MODEL_REGION_TAG = 'vision_classification_create_model';
26+
const LOCATION = 'us-central1';
27+
const DATASET_ID = 'ICN6257835245115015168';
28+
29+
describe('Automl Vision Classification Create Model Tests', () => {
30+
const client = new AutoMlClient();
31+
let operationId;
32+
33+
it('should create a model', async () => {
34+
const projectId = await client.getProjectId();
35+
const create_output = execSync(
36+
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} classification_test_create_model`
37+
);
38+
39+
assert.match(create_output, /Training started/);
40+
41+
operationId = create_output
42+
.split('Training operation name: ')[1]
43+
.split('\n')[0];
44+
});
45+
46+
after('cancel model training', async () => {
47+
await client.operationsClient.cancelOperation({name: operationId});
48+
});
49+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const DEPLOY_MODEL_REGION_TAG =
24+
'vision_classification_deploy_model_node_count.js';
25+
const LOCATION = 'us-central1';
26+
const MODEL_ID = 'ICN0000000000000000000';
27+
28+
describe('Automl Vision Classification Deploy Model Test', () => {
29+
const client = new AutoMlClient();
30+
31+
it('should deploy a model with a specified node count', async () => {
32+
// As model deployment can take a long time, instead try to deploy a
33+
// nonexistent model and confirm that the model was not found, but other
34+
// elements of the request were valid.
35+
const projectId = await client.getProjectId();
36+
const args = [DEPLOY_MODEL_REGION_TAG, projectId, LOCATION, MODEL_ID];
37+
const output = cp.spawnSync('node', args, {encoding: 'utf8'});
38+
39+
assert.match(output.stderr, /NOT_FOUND/);
40+
assert.match(output.stderr, /The model does not exist./);
41+
});
42+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const {assert} = require('chai');
18+
const {before, describe, it} = require('mocha');
19+
const {AutoMlClient} = require('@google-cloud/automl').v1;
20+
21+
const cp = require('child_process');
22+
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
25+
const MODEL_ID = 'ICN5317963909599068160';
26+
const PREDICT_REGION_TAG = 'vision_classification_predict';
27+
const LOCATION = 'us-central1';
28+
29+
describe('Automl Vision Classification Predict Test', () => {
30+
const client = new AutoMlClient();
31+
32+
before('should verify the model is deployed', async () => {
33+
const projectId = await client.getProjectId();
34+
const request = {
35+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
36+
};
37+
38+
const [response] = await client.getModel(request);
39+
if (response.deploymentState === 'UNDEPLOYED') {
40+
const request = {
41+
name: client.modelPath(projectId, LOCATION, MODEL_ID),
42+
};
43+
44+
const [operation] = await client.deployModel(request);
45+
46+
// Wait for operation to complete.
47+
await operation.promise();
48+
}
49+
});
50+
51+
it('should predict', async () => {
52+
const projectId = await client.getProjectId();
53+
const filePath = 'resources/test.png';
54+
55+
const predictOutput = execSync(
56+
`node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${filePath}`
57+
);
58+
assert.match(predictOutput, /Predicted class name/);
59+
});
60+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
displayName = 'YOUR_DISPLAY_NAME'
21+
) {
22+
// [START automl_vision_classification_create_dataset]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const projectId = 'YOUR_PROJECT_ID';
27+
// const location = 'us-central1';
28+
// const displayName = 'YOUR_DISPLAY_NAME';
29+
30+
// Imports the Google Cloud AutoML library
31+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
32+
33+
// Instantiates a client
34+
const client = new AutoMlClient();
35+
36+
async function createDataset() {
37+
// Construct request
38+
// Specify the classification type
39+
// Types:
40+
// MultiLabel: Multiple labels are allowed for one example.
41+
// MultiClass: At most one label is allowed per example.
42+
const request = {
43+
parent: client.locationPath(projectId, location),
44+
dataset: {
45+
displayName: displayName,
46+
imageClassificationDatasetMetadata: {
47+
classificationType: 'MULTILABEL',
48+
},
49+
},
50+
};
51+
52+
// Create dataset
53+
const [operation] = await client.createDataset(request);
54+
55+
// Wait for operation to complete.
56+
const [response] = await operation.promise();
57+
58+
console.log(`Dataset name: ${response.name}`);
59+
console.log(`
60+
Dataset id: ${
61+
response.name
62+
.split('/')
63+
[response.name.split('/').length - 1].split('\n')[0]
64+
}`);
65+
}
66+
67+
createDataset();
68+
// [END automl_vision_classification_create_dataset]
69+
}
70+
71+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(
18+
projectId = 'YOUR_PROJECT_ID',
19+
location = 'us-central1',
20+
datasetId = 'YOUR_DATASET_ID',
21+
displayName = 'YOUR_DISPLAY_NAME'
22+
) {
23+
// [START automl_vision_classification_create_model]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const projectId = 'YOUR_PROJECT_ID';
28+
// const location = 'us-central1';
29+
// const dataset_id = 'YOUR_DATASET_ID';
30+
// const displayName = 'YOUR_DISPLAY_NAME';
31+
32+
// Imports the Google Cloud AutoML library
33+
const {AutoMlClient} = require(`@google-cloud/automl`).v1;
34+
35+
// Instantiates a client
36+
const client = new AutoMlClient();
37+
38+
async function createModel() {
39+
// Construct request
40+
const request = {
41+
parent: client.locationPath(projectId, location),
42+
model: {
43+
displayName: displayName,
44+
datasetId: datasetId,
45+
imageClassificationModelMetadata: {
46+
trainBudgetMilliNodeHours: 24000,
47+
},
48+
},
49+
};
50+
51+
// Don't wait for the LRO
52+
const [operation] = await client.createModel(request);
53+
console.log(`Training started... ${operation}`);
54+
console.log(`Training operation name: ${operation.name}`);
55+
}
56+
57+
createModel();
58+
// [END automl_vision_classification_create_model]
59+
}
60+
61+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)