Skip to content

Commit 997a04b

Browse files
authored
[devops] Fix testing efficiency (#652)
* only lowercase if not a URL * same fix for sound classifier * only call modelInstantiation with beforeAll() rather than beforeEach()
1 parent 25d4cba commit 997a04b

File tree

7 files changed

+134
-93
lines changed

7 files changed

+134
-93
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ We’re still rolling out all of our unit tests, but if you want to contribute t
249249
```npm run test:single```
250250

251251
- To run a test on a single model
252-
```npm run test -- --model:YourModelNameHere```
252+
```npm run test -- --model=YourModelNameHere```
253253

254254
This last one is case sensitive!
255255

src/BodyPix/index_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('bodyPix', () => {
4747
return img;
4848
}
4949

50-
beforeEach(async () => {
50+
beforeAll(async () => {
5151
jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000;
5252
bp = await bodyPix();
5353
});

src/CharRNN/index_test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ describe('charRnn', () => {
3333
rnn = await charRNN(RNN_MODEL_URL, undefined);
3434
});
3535

36-
it('instantiates an rnn with all the defaults', async () => {
37-
expect(rnn.ready).toBeTruthy();
38-
expect(rnn.defaults.seed).toBe(RNN_DEFAULTS.seed);
39-
expect(rnn.defaults.length).toBe(RNN_DEFAULTS.length);
40-
expect(rnn.defaults.temperature).toBe(RNN_DEFAULTS.temperature);
41-
expect(rnn.defaults.stateful).toBe(RNN_DEFAULTS.stateful);
42-
});
43-
4436
// it('loads the model with all the defaults', async () => {
4537
// expect(rnn.cellsAmount).toBe(RNN_MODEL_DEFAULTS.cellsAmount);
4638
// expect(rnn.vocabSize).toBe(RNN_MODEL_DEFAULTS.vocabSize);
4739
// });
4840

4941
describe('generate', () => {
42+
it('instantiates an rnn with all the defaults', async () => {
43+
expect(rnn.ready).toBeTruthy();
44+
expect(rnn.defaults.seed).toBe(RNN_DEFAULTS.seed);
45+
expect(rnn.defaults.length).toBe(RNN_DEFAULTS.length);
46+
expect(rnn.defaults.temperature).toBe(RNN_DEFAULTS.temperature);
47+
expect(rnn.defaults.stateful).toBe(RNN_DEFAULTS.stateful);
48+
});
49+
5050
it('Should generate content that follows default options if given an empty object', async() => {
5151
const result = await rnn.generate({});
5252
expect(result.sample.length).toBe(20);

src/FaceApi/index_test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,18 @@ describe('faceApi', () => {
4141
// return canvas;
4242
// }
4343

44-
beforeEach(async () => {
44+
beforeAll(async () => {
4545
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
4646
faceapi = await faceApi();
4747
});
48-
49-
it('Should create faceApi with all the defaults', async () => {
50-
expect(faceapi.config.withLandmarks).toBe(FACEAPI_DEFAULTS.withLandmarks);
51-
expect(faceapi.config.withDescriptors).toBe(FACEAPI_DEFAULTS.withDescriptors);
52-
});
5348

5449
describe('landmarks', () => {
50+
51+
it('Should create faceApi with all the defaults', async () => {
52+
expect(faceapi.config.withLandmarks).toBe(FACEAPI_DEFAULTS.withLandmarks);
53+
expect(faceapi.config.withDescriptors).toBe(FACEAPI_DEFAULTS.withDescriptors);
54+
});
55+
5556
it('Should get landmarks for Frida', async () => {
5657
const img = await getImage();
5758
await faceapi.detectSingle(img)

src/ImageClassifier/index_test.js

Lines changed: 98 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
55

6-
const { imageClassifier } = ml5;
6+
const {
7+
imageClassifier
8+
} = ml5;
9+
10+
const TM_URL = 'https://storage.googleapis.com/tm-models/WfgKPytY/model.json';
711

812
const DEFAULTS = {
913
learningRate: 0.0001,
@@ -16,67 +20,109 @@ const DEFAULTS = {
1620
version: 2,
1721
};
1822

23+
async function getImage() {
24+
const img = new Image();
25+
img.crossOrigin = true;
26+
img.src = 'https://cdn.jsdelivr.net/gh/ml5js/ml5-library@development/assets/bird.jpg';
27+
await new Promise((resolve) => {
28+
img.onload = resolve;
29+
});
30+
return img;
31+
}
32+
33+
async function getCanvas() {
34+
const img = await getImage();
35+
const canvas = document.createElement('canvas');
36+
canvas.width = img.width;
37+
canvas.height = img.height;
38+
canvas.getContext('2d').drawImage(img, 0, 0);
39+
return canvas;
40+
}
41+
1942
describe('imageClassifier', () => {
2043
let classifier;
2144

22-
async function getImage() {
23-
const img = new Image();
24-
img.crossOrigin = true;
25-
img.src = 'https://cdn.jsdelivr.net/gh/ml5js/ml5-library@development/assets/bird.jpg';
26-
await new Promise((resolve) => { img.onload = resolve; });
27-
return img;
28-
}
29-
30-
async function getCanvas() {
31-
const img = await getImage();
32-
const canvas = document.createElement('canvas');
33-
canvas.width = img.width;
34-
canvas.height = img.height;
35-
canvas.getContext('2d').drawImage(img, 0, 0);
36-
return canvas;
37-
}
38-
39-
beforeEach(async () => {
40-
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
41-
classifier = await imageClassifier('MobileNet', undefined, {});
42-
});
45+
/**
46+
* Test imageClassifier with teachable machine
47+
*/
48+
// Teachable machine model
49+
describe('with Teachable Machine model', () => {
50+
51+
beforeAll(async () => {
52+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
53+
classifier = await imageClassifier(TM_URL, undefined, {});
54+
});
55+
56+
describe('instantiate', () => {
57+
it('Should create a classifier with all the defaults', async () => {
58+
console.log(classifier)
59+
expect(classifier.modelUrl).toBe(TM_URL);
60+
});
61+
});
4362

44-
it('Should create a classifier with all the defaults', async () => {
45-
expect(classifier.version).toBe(DEFAULTS.version);
46-
expect(classifier.alpha).toBe(DEFAULTS.alpha);
47-
expect(classifier.topk).toBe(DEFAULTS.topk);
48-
expect(classifier.ready).toBeTruthy();
4963
});
5064

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

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

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

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

76-
it('Should support p5 elements with canvas on .elt', async () => {
77-
const canvas = await getCanvas();
78-
await classifier.classify({ elt: canvas })
79-
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
77+
describe('instantiate', () => {
78+
79+
it('Should create a classifier with all the defaults', async () => {
80+
expect(classifier.version).toBe(DEFAULTS.version);
81+
expect(classifier.alpha).toBe(DEFAULTS.alpha);
82+
expect(classifier.topk).toBe(DEFAULTS.topk);
83+
expect(classifier.ready).toBeTruthy();
84+
});
85+
})
86+
87+
describe('classify', () => {
88+
89+
it('Should classify an image of a Robin', async () => {
90+
const img = await getImage();
91+
await classifier.classify(img)
92+
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
93+
});
94+
95+
it('Should support p5 elements with an image on .elt', async () => {
96+
const img = await getImage();
97+
await classifier.classify({
98+
elt: img
99+
})
100+
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
101+
});
102+
103+
it('Should support HTMLCanvasElement', async () => {
104+
const canvas = await getCanvas();
105+
await classifier.classify(canvas)
106+
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
107+
});
108+
109+
it('Should support p5 elements with canvas on .canvas', async () => {
110+
const canvas = await getCanvas();
111+
await classifier.classify({
112+
canvas
113+
})
114+
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
115+
});
116+
117+
it('Should support p5 elements with canvas on .elt', async () => {
118+
const canvas = await getCanvas();
119+
await classifier.classify({
120+
elt: canvas
121+
})
122+
.then(results => expect(results[0].label).toBe('robin, American robin, Turdus migratorius'));
123+
});
80124
});
125+
81126
});
82-
});
127+
128+
})

src/KMeans/index_test.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const KMEANS_DEFAULTS = {
1616
describe('kMeans', () => {
1717
let kmeansModel;
1818
const dataurl = 'https://raw.githubusercontent.com/ml5js/ml5-examples/development/d3/KMeans/KMeans_GaussianClusterDemo/data/gaussian2d_2clusters.csv'
19-
beforeEach(async () => {
19+
20+
beforeAll(async () => {
2021
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
2122
kmeansModel = await kmeans(dataurl, KMEANS_DEFAULTS, (err, result) => {
2223
return;
@@ -43,11 +44,4 @@ describe('kMeans', () => {
4344
expect(unique).toBe(2);
4445
});
4546

46-
47-
48-
49-
50-
51-
52-
5347
});

src/NeuralNetwork/index_test.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,27 @@ const {
99
} = ml5;
1010

1111
const NN_DEFAULTS = {
12-
task: 'regression',
13-
activationHidden: 'sigmoid',
14-
activationOutput: 'sigmoid',
15-
debug: false,
16-
learningRate: 0.25,
17-
inputs: 2,
18-
outputs: 1,
19-
noVal: null,
20-
hiddenUnits: 16,
21-
modelMetrics: ['accuracy'],
22-
modelLoss: 'meanSquaredError',
23-
modelOptimizer: null,
24-
batchSize: 64,
25-
epochs: 32,
12+
task: 'regression',
13+
activationHidden: 'sigmoid',
14+
activationOutput: 'sigmoid',
15+
debug: false,
16+
learningRate: 0.25,
17+
inputs: 2,
18+
outputs: 1,
19+
noVal: null,
20+
hiddenUnits: 16,
21+
modelMetrics: ['accuracy'],
22+
modelLoss: 'meanSquaredError',
23+
modelOptimizer: null,
24+
batchSize: 64,
25+
epochs: 32,
2626
}
2727

2828

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

32-
beforeEach(async () => {
32+
beforeAll(async () => {
3333
jasmine.DEFAULT_TIMEOUT_INTERVAL = 15000;
3434
nn = await neuralNetwork();
3535
});
@@ -50,7 +50,7 @@ describe('neuralNetwork', () => {
5050
// expect(nn.config.training.modelMetrics).toBe(NN_DEFAULTS.modelMetrics);
5151
expect(nn.config.training.modelLoss).toBe(NN_DEFAULTS.modelLoss);
5252
// expect(nn.config.training.modelOptimizer).toBe();
53-
53+
5454
// data defaults
5555
// expect(nn.config.dataOptions.dataUrl).toBe();
5656
// expect(nn.config.dataOptions.inputs).toBe(NN_DEFAULTS.inputs);
@@ -60,4 +60,4 @@ describe('neuralNetwork', () => {
6060

6161
});
6262

63-
});
63+
});

0 commit comments

Comments
 (0)