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

[devops] Fix testing efficiency #652

Merged
merged 6 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ We’re still rolling out all of our unit tests, but if you want to contribute t
```npm run test:single```

- To run a test on a single model
```npm run test -- --model:YourModelNameHere```
```npm run test -- --model=YourModelNameHere```

This last one is case sensitive!

Expand Down
2 changes: 1 addition & 1 deletion src/BodyPix/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('bodyPix', () => {
return img;
}

beforeEach(async () => {
beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
bp = await bodyPix();
});
Expand Down
16 changes: 8 additions & 8 deletions src/CharRNN/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ describe('charRnn', () => {
rnn = await charRNN(RNN_MODEL_URL, undefined);
});

it('instantiates an rnn with all the defaults', async () => {
expect(rnn.ready).toBeTruthy();
expect(rnn.defaults.seed).toBe(RNN_DEFAULTS.seed);
expect(rnn.defaults.length).toBe(RNN_DEFAULTS.length);
expect(rnn.defaults.temperature).toBe(RNN_DEFAULTS.temperature);
expect(rnn.defaults.stateful).toBe(RNN_DEFAULTS.stateful);
});

// it('loads the model with all the defaults', async () => {
// expect(rnn.cellsAmount).toBe(RNN_MODEL_DEFAULTS.cellsAmount);
// expect(rnn.vocabSize).toBe(RNN_MODEL_DEFAULTS.vocabSize);
// });

describe('generate', () => {
it('instantiates an rnn with all the defaults', async () => {
expect(rnn.ready).toBeTruthy();
expect(rnn.defaults.seed).toBe(RNN_DEFAULTS.seed);
expect(rnn.defaults.length).toBe(RNN_DEFAULTS.length);
expect(rnn.defaults.temperature).toBe(RNN_DEFAULTS.temperature);
expect(rnn.defaults.stateful).toBe(RNN_DEFAULTS.stateful);
});

it('Should generate content that follows default options if given an empty object', async() => {
const result = await rnn.generate({});
expect(result.sample.length).toBe(20);
Expand Down
13 changes: 7 additions & 6 deletions src/FaceApi/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,18 @@ describe('faceApi', () => {
// return canvas;
// }

beforeEach(async () => {
beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
faceapi = await faceApi();
});

it('Should create faceApi with all the defaults', async () => {
expect(faceapi.config.withLandmarks).toBe(FACEAPI_DEFAULTS.withLandmarks);
expect(faceapi.config.withDescriptors).toBe(FACEAPI_DEFAULTS.withDescriptors);
});

describe('landmarks', () => {

it('Should create faceApi with all the defaults', async () => {
expect(faceapi.config.withLandmarks).toBe(FACEAPI_DEFAULTS.withLandmarks);
expect(faceapi.config.withDescriptors).toBe(FACEAPI_DEFAULTS.withDescriptors);
});

it('Should get landmarks for Frida', async () => {
const img = await getImage();
await faceapi.detectSingle(img)
Expand Down
150 changes: 98 additions & 52 deletions src/ImageClassifier/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

const { imageClassifier } = ml5;
const {
imageClassifier
} = ml5;

const TM_URL = 'https://storage.googleapis.com/tm-models/WfgKPytY/model.json';

const DEFAULTS = {
learningRate: 0.0001,
Expand All @@ -16,67 +20,109 @@ const DEFAULTS = {
version: 2,
};

async function getImage() {
const img = new Image();
img.crossOrigin = true;
img.src = 'https://cdn.jsdelivr.net/gh/ml5js/ml5-library@development/assets/bird.jpg';
await new Promise((resolve) => {
img.onload = resolve;
});
return img;
}

async function getCanvas() {
const img = await getImage();
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0);
return canvas;
}

describe('imageClassifier', () => {
let classifier;

async function getImage() {
const img = new Image();
img.crossOrigin = true;
img.src = 'https://cdn.jsdelivr.net/gh/ml5js/ml5-library@development/assets/bird.jpg';
await new Promise((resolve) => { img.onload = resolve; });
return img;
}

async function getCanvas() {
const img = await getImage();
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0);
return canvas;
}

beforeEach(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
classifier = await imageClassifier('MobileNet', undefined, {});
});
/**
* Test imageClassifier with teachable machine
*/
// Teachable machine model
describe('with Teachable Machine model', () => {

beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
classifier = await imageClassifier(TM_URL, undefined, {});
});

describe('instantiate', () => {
it('Should create a classifier with all the defaults', async () => {
console.log(classifier)
expect(classifier.modelUrl).toBe(TM_URL);
});
});

it('Should create a classifier with all the defaults', async () => {
expect(classifier.version).toBe(DEFAULTS.version);
expect(classifier.alpha).toBe(DEFAULTS.alpha);
expect(classifier.topk).toBe(DEFAULTS.topk);
expect(classifier.ready).toBeTruthy();
});

describe('classify', () => {
it('Should classify an image of a Robin', async () => {
const img = await getImage();
await classifier.classify(img)
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});

it('Should support p5 elements with an image on .elt', async () => {
const img = await getImage();
await classifier.classify({ elt: img })
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});

it('Should support HTMLCanvasElement', async () => {
const canvas = await getCanvas();
await classifier.classify(canvas)
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});
/**
* Test imageClassifier with Mobilenet
*/
describe('imageClassifier with Mobilenet', () => {

it('Should support p5 elements with canvas on .canvas', async () => {
const canvas = await getCanvas();
await classifier.classify({ canvas })
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
classifier = await imageClassifier('MobileNet', undefined, {});
});

it('Should support p5 elements with canvas on .elt', async () => {
const canvas = await getCanvas();
await classifier.classify({ elt: canvas })
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
describe('instantiate', () => {

it('Should create a classifier with all the defaults', async () => {
expect(classifier.version).toBe(DEFAULTS.version);
expect(classifier.alpha).toBe(DEFAULTS.alpha);
expect(classifier.topk).toBe(DEFAULTS.topk);
expect(classifier.ready).toBeTruthy();
});
})

describe('classify', () => {

it('Should classify an image of a Robin', async () => {
const img = await getImage();
await classifier.classify(img)
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});

it('Should support p5 elements with an image on .elt', async () => {
const img = await getImage();
await classifier.classify({
elt: img
})
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});

it('Should support HTMLCanvasElement', async () => {
const canvas = await getCanvas();
await classifier.classify(canvas)
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});

it('Should support p5 elements with canvas on .canvas', async () => {
const canvas = await getCanvas();
await classifier.classify({
canvas
})
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});

it('Should support p5 elements with canvas on .elt', async () => {
const canvas = await getCanvas();
await classifier.classify({
elt: canvas
})
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
});
});

});
});

})
10 changes: 2 additions & 8 deletions src/KMeans/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const KMEANS_DEFAULTS = {
describe('kMeans', () => {
let kmeansModel;
const dataurl = 'https://raw.githubusercontent.com/ml5js/ml5-examples/development/d3/KMeans/KMeans_GaussianClusterDemo/data/gaussian2d_2clusters.csv'
beforeEach(async () => {

beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
kmeansModel = await kmeans(dataurl, KMEANS_DEFAULTS, (err, result) => {
return;
Expand All @@ -43,11 +44,4 @@ describe('kMeans', () => {
expect(unique).toBe(2);
});








});
34 changes: 17 additions & 17 deletions src/NeuralNetwork/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ const {
} = ml5;

const NN_DEFAULTS = {
task: 'regression',
activationHidden: 'sigmoid',
activationOutput: 'sigmoid',
debug: false,
learningRate: 0.25,
inputs: 2,
outputs: 1,
noVal: null,
hiddenUnits: 16,
modelMetrics: ['accuracy'],
modelLoss: 'meanSquaredError',
modelOptimizer: null,
batchSize: 64,
epochs: 32,
task: 'regression',
activationHidden: 'sigmoid',
activationOutput: 'sigmoid',
debug: false,
learningRate: 0.25,
inputs: 2,
outputs: 1,
noVal: null,
hiddenUnits: 16,
modelMetrics: ['accuracy'],
modelLoss: 'meanSquaredError',
modelOptimizer: null,
batchSize: 64,
epochs: 32,
}


describe('neuralNetwork', () => {
let nn;

beforeEach(async () => {
beforeAll(async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
nn = await neuralNetwork();
});
Expand All @@ -50,7 +50,7 @@ describe('neuralNetwork', () => {
// expect(nn.config.training.modelMetrics).toBe(NN_DEFAULTS.modelMetrics);
expect(nn.config.training.modelLoss).toBe(NN_DEFAULTS.modelLoss);
// expect(nn.config.training.modelOptimizer).toBe();

// data defaults
// expect(nn.config.dataOptions.dataUrl).toBe();
// expect(nn.config.dataOptions.inputs).toBe(NN_DEFAULTS.inputs);
Expand All @@ -60,4 +60,4 @@ describe('neuralNetwork', () => {

});

});
});