Skip to content

Commit

Permalink
docs: add vision classification ga samples (#297)
Browse files Browse the repository at this point in the history
* 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 <bencoe@google.com>
  • Loading branch information
2 people authored and Ace Nassri committed Nov 17, 2022
1 parent c4d3d86 commit e91181c
Show file tree
Hide file tree
Showing 10 changed files with 466 additions and 1 deletion.
2 changes: 1 addition & 1 deletion automl/language_entity_extraction_predict.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// 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
// https://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,
Expand Down
Binary file added automl/resources/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions automl/test/vision_classification_create_dataset.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 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
//
// https://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.

'use strict';

const {assert} = require('chai');
const {after, describe, it} = require('mocha');
const {AutoMlClient} = require('@google-cloud/automl').v1;

const cp = require('child_process');
const uuid = require('uuid');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const CREATE_DATASET_REGION_TAG = 'vision_classification_create_dataset';
const LOCATION = 'us-central1';

describe('Automl Vision Classification Create Dataset Test', () => {
const client = new AutoMlClient();
let datasetId;

it('should create a dataset', async () => {
const projectId = await client.getProjectId();
const displayName = `test_${uuid
.v4()
.replace(/-/g, '_')
.substring(0, 26)}`;

// create
const create_output = execSync(
`node ${CREATE_DATASET_REGION_TAG}.js ${projectId} ${LOCATION} ${displayName}`
);
assert.match(create_output, /Dataset id:/);

datasetId = create_output.split('Dataset id: ')[1].split('\n')[0];
});

after('delete created dataset', async () => {
const projectId = await client.getProjectId();
const request = {
name: client.datasetPath(projectId, LOCATION, datasetId),
};
const [operation] = await client.deleteDataset(request);
await operation.promise();
});
});
49 changes: 49 additions & 0 deletions automl/test/vision_classification_create_model.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 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
//
// https://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.

'use strict';

const {assert} = require('chai');
const {after, describe, it} = require('mocha');
const {AutoMlClient} = require('@google-cloud/automl').v1;

const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const CREATE_MODEL_REGION_TAG = 'vision_classification_create_model';
const LOCATION = 'us-central1';
const DATASET_ID = 'ICN6257835245115015168';

describe('Automl Vision Classification Create Model Tests', () => {
const client = new AutoMlClient();
let operationId;

it('should create a model', async () => {
const projectId = await client.getProjectId();
const create_output = execSync(
`node ${CREATE_MODEL_REGION_TAG}.js ${projectId} ${LOCATION} ${DATASET_ID} classification_test_create_model`
);

assert.match(create_output, /Training started/);

operationId = create_output
.split('Training operation name: ')[1]
.split('\n')[0];
});

after('cancel model training', async () => {
await client.operationsClient.cancelOperation({name: operationId});
});
});
42 changes: 42 additions & 0 deletions automl/test/vision_classification_deploy_model_node_count.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2020 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
//
// https://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.

'use strict';

const {assert} = require('chai');
const {describe, it} = require('mocha');
const {AutoMlClient} = require('@google-cloud/automl').v1;

const cp = require('child_process');

const DEPLOY_MODEL_REGION_TAG =
'vision_classification_deploy_model_node_count.js';
const LOCATION = 'us-central1';
const MODEL_ID = 'ICN0000000000000000000';

describe('Automl Vision Classification Deploy Model Test', () => {
const client = new AutoMlClient();

it('should deploy a model with a specified node count', async () => {
// As model deployment can take a long time, instead try to deploy a
// nonexistent model and confirm that the model was not found, but other
// elements of the request were valid.
const projectId = await client.getProjectId();
const args = [DEPLOY_MODEL_REGION_TAG, projectId, LOCATION, MODEL_ID];
const output = cp.spawnSync('node', args, {encoding: 'utf8'});

assert.match(output.stderr, /NOT_FOUND/);
assert.match(output.stderr, /The model does not exist./);
});
});
60 changes: 60 additions & 0 deletions automl/test/vision_classification_predict.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2020 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
//
// https://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.

'use strict';

const {assert} = require('chai');
const {before, describe, it} = require('mocha');
const {AutoMlClient} = require('@google-cloud/automl').v1;

const cp = require('child_process');

const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const MODEL_ID = 'ICN5317963909599068160';
const PREDICT_REGION_TAG = 'vision_classification_predict';
const LOCATION = 'us-central1';

describe('Automl Vision Classification Predict Test', () => {
const client = new AutoMlClient();

before('should verify the model is deployed', async () => {
const projectId = await client.getProjectId();
const request = {
name: client.modelPath(projectId, LOCATION, MODEL_ID),
};

const [response] = await client.getModel(request);
if (response.deploymentState === 'UNDEPLOYED') {
const request = {
name: client.modelPath(projectId, LOCATION, MODEL_ID),
};

const [operation] = await client.deployModel(request);

// Wait for operation to complete.
await operation.promise();
}
});

it('should predict', async () => {
const projectId = await client.getProjectId();
const filePath = 'resources/test.png';

const predictOutput = execSync(
`node ${PREDICT_REGION_TAG}.js ${projectId} ${LOCATION} ${MODEL_ID} ${filePath}`
);
assert.match(predictOutput, /Predicted class name/);
});
});
71 changes: 71 additions & 0 deletions automl/vision_classification_create_dataset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2020 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
//
// https://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.

'use strict';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
displayName = 'YOUR_DISPLAY_NAME'
) {
// [START automl_vision_classification_create_dataset]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const displayName = 'YOUR_DISPLAY_NAME';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function createDataset() {
// Construct request
// Specify the classification type
// Types:
// MultiLabel: Multiple labels are allowed for one example.
// MultiClass: At most one label is allowed per example.
const request = {
parent: client.locationPath(projectId, location),
dataset: {
displayName: displayName,
imageClassificationDatasetMetadata: {
classificationType: 'MULTILABEL',
},
},
};

// Create dataset
const [operation] = await client.createDataset(request);

// Wait for operation to complete.
const [response] = await operation.promise();

console.log(`Dataset name: ${response.name}`);
console.log(`
Dataset id: ${
response.name
.split('/')
[response.name.split('/').length - 1].split('\n')[0]
}`);
}

createDataset();
// [END automl_vision_classification_create_dataset]
}

main(...process.argv.slice(2));
61 changes: 61 additions & 0 deletions automl/vision_classification_create_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020 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
//
// https://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.

'use strict';

function main(
projectId = 'YOUR_PROJECT_ID',
location = 'us-central1',
datasetId = 'YOUR_DATASET_ID',
displayName = 'YOUR_DISPLAY_NAME'
) {
// [START automl_vision_classification_create_model]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const location = 'us-central1';
// const dataset_id = 'YOUR_DATASET_ID';
// const displayName = 'YOUR_DISPLAY_NAME';

// Imports the Google Cloud AutoML library
const {AutoMlClient} = require(`@google-cloud/automl`).v1;

// Instantiates a client
const client = new AutoMlClient();

async function createModel() {
// Construct request
const request = {
parent: client.locationPath(projectId, location),
model: {
displayName: displayName,
datasetId: datasetId,
imageClassificationModelMetadata: {
trainBudgetMilliNodeHours: 24000,
},
},
};

// Don't wait for the LRO
const [operation] = await client.createModel(request);
console.log(`Training started... ${operation}`);
console.log(`Training operation name: ${operation.name}`);
}

createModel();
// [END automl_vision_classification_create_model]
}

main(...process.argv.slice(2));
Loading

0 comments on commit e91181c

Please sign in to comment.