|
| 1 | +/** |
| 2 | + * Copyright 2018, Google, LLC. |
| 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 | + * http://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 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +const path = require('path'); |
| 19 | +const {assert} = require('chai'); |
| 20 | +const execa = require('execa'); |
| 21 | + |
| 22 | +const exec = async cmd => (await execa.shell(cmd)).stdout; |
| 23 | +const cmdDataset = `node vision/automlVisionDataset.js`; |
| 24 | +const cmdModel = `node vision/automlVisionModel.js`; |
| 25 | +const cmdPredict = `node vision/automlVisionPredict.js`; |
| 26 | + |
| 27 | +const testDataSetName = `testDataSet`; |
| 28 | +const dummyDataSet = `dummyDataSet`; |
| 29 | +const testModelName = `dummyModel`; |
| 30 | +const testImgPath = `./resources/`; |
| 31 | +const sampleImage2 = path.join(testImgPath, `testImage2.jpg`); |
| 32 | + |
| 33 | +describe(`auto ml vision`, () => { |
| 34 | + it.skip(`should create, list, and delete a dataset`, async () => { |
| 35 | + // Check to see that this dataset does not yet exist |
| 36 | + let output = await exec(`${cmdDataset} list-datasets`); |
| 37 | + assert.strictEqual(output.includes(testDataSetName), false); |
| 38 | + |
| 39 | + // Create dataset |
| 40 | + output = await exec(`${cmdDataset} create-dataset -n "${testDataSetName}"`); |
| 41 | + const dataSetId = output |
| 42 | + .split(`\n`)[1] |
| 43 | + .split(`:`)[1] |
| 44 | + .trim(); |
| 45 | + assert.match(output, new RegExp(testDataSetName)); |
| 46 | + |
| 47 | + // Delete dataset |
| 48 | + output = await exec(`${cmdDataset} delete-dataset -i "${dataSetId}"`); |
| 49 | + assert.match(output, /Dataset deleted./); |
| 50 | + }); |
| 51 | + |
| 52 | + // See : https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/vision/automl/model_test.py |
| 53 | + // We make two models running this test, see hard-coded workaround below |
| 54 | + it.skip(`should create a dataset, import data, and start making a model`, async () => { |
| 55 | + // Check to see that this dataset does not yet exist |
| 56 | + let output = await exec(`${cmdDataset} list-datasets`); |
| 57 | + assert.strictEqual(output.includes(dummyDataSet), false); |
| 58 | + |
| 59 | + // Create dataset |
| 60 | + output = await exec(`${cmdDataset} create-dataset -n "${dummyDataSet}"`); |
| 61 | + const dataSetId = output |
| 62 | + .split(`\n`)[1] |
| 63 | + .split(`:`)[1] |
| 64 | + .trim(); |
| 65 | + assert.match(output, new RegExp(dummyDataSet)); |
| 66 | + |
| 67 | + // Import Data |
| 68 | + output = await exec( |
| 69 | + `${cmdDataset} import-data -i "${dataSetId}" -p "gs://nodejs-docs-samples-vcm/flowerTraindata20lines.csv"` |
| 70 | + ); |
| 71 | + assert.match(output, /Data imported./); |
| 72 | + |
| 73 | + // Check to make sure model doesn't already exist |
| 74 | + output = await exec(`${cmdModel} list-models`); |
| 75 | + assert.notMatch(output, new RegExp(testModelName)); |
| 76 | + |
| 77 | + // begin training dataset, getting operation ID for next operation |
| 78 | + output = await exec(` |
| 79 | + ${cmdModel} create-model -i "${dataSetId}" -m "${testModelName}" -t "2"`); |
| 80 | + const operationName = output |
| 81 | + .split(`\n`)[0] |
| 82 | + .split(`:`)[1] |
| 83 | + .split(`/`) |
| 84 | + .pop() |
| 85 | + .trim(); |
| 86 | + assert.match(output, /Training started.../); |
| 87 | + |
| 88 | + // poll operation status, here confirming that operation is not complete yet |
| 89 | + output = await exec( |
| 90 | + `${cmdModel} get-operation-status -i "${dataSetId}" -o "${operationName}"` |
| 91 | + ); |
| 92 | + assert.match(output, /done: false/); |
| 93 | + }); |
| 94 | + |
| 95 | + it.skip(`should display evaluation from prexisting model`, async () => { |
| 96 | + const flowersModelId = `ICN723541179344731436`; |
| 97 | + const flowersDisplayName = `flowersTest`; |
| 98 | + |
| 99 | + // Confirm dataset exists |
| 100 | + let output = await exec(`${cmdDataset} list-datasets`); |
| 101 | + assert.match(output, new RegExp(flowersDisplayName)); |
| 102 | + |
| 103 | + // List model evaluations, confirm model exists |
| 104 | + output = await exec( |
| 105 | + `${cmdModel} list-model-evaluations -a "${flowersModelId}"` |
| 106 | + ); |
| 107 | + |
| 108 | + // Display evaluation |
| 109 | + output = await exec( |
| 110 | + `${cmdModel} display-evaluation -a "${flowersModelId}"` |
| 111 | + ); |
| 112 | + assert.match(output, /Model Precision/); |
| 113 | + }); |
| 114 | + |
| 115 | + it.skip(`should run Prediction from prexisting model`, async () => { |
| 116 | + const donotdeleteModelId = `ICN723541179344731436`; |
| 117 | + const flowersDisplayName = `flowers`; |
| 118 | + |
| 119 | + // Confirm dataset exists |
| 120 | + let output = await exec(`${cmdDataset} list-datasets`); |
| 121 | + assert.match(output, new RegExp(flowersDisplayName)); |
| 122 | + |
| 123 | + // List model evaluations, confirm model exists |
| 124 | + output = await exec( |
| 125 | + `${cmdModel} list-model-evaluations -a "${donotdeleteModelId}"` |
| 126 | + ); |
| 127 | + // Run prediction on 'testImage.jpg' in resources folder |
| 128 | + output = await exec( |
| 129 | + `${cmdPredict} predict -i "${donotdeleteModelId}" -f "${sampleImage2}" -s "0.5"` |
| 130 | + ); |
| 131 | + assert.match(output, /dandelion/); |
| 132 | + }); |
| 133 | + |
| 134 | + // List datasets |
| 135 | + it(`should list datasets`, async () => { |
| 136 | + const output = await exec(`${cmdDataset} list-datasets`); |
| 137 | + assert.match(output, /List of datasets:/); |
| 138 | + }); |
| 139 | +}); |
0 commit comments