Skip to content

Commit c4b5fb3

Browse files
Separate unit tests and integration tests
1 parent a4bf9c9 commit c4b5fb3

File tree

8 files changed

+103
-49
lines changed

8 files changed

+103
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"spec_files": [
3+
"dist/integration_test/*_test.js"
4+
],
5+
"env": {
6+
"stopSpecOnExpectationFailure": false,
7+
"random": true
8+
}
9+
}

coral-tflite-delegate/jasmine.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"spec_files": [
3-
"dist/**/*_test.js"
3+
"dist/*_test.js"
44
],
55
"env": {
66
"stopSpecOnExpectationFailure": false,

coral-tflite-delegate/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"build": "tsc",
99
"test-dev": "jasmine --config=jasmine.json",
1010
"test": "yarn build && yarn test-dev",
11+
"test-integration-dev": "jasmine --config=jasmine-integration.json",
12+
"test-integration": "yarn build && yarn test-integration-dev",
1113
"lint": "tslint -p . -t verbose"
1214
},
1315
"devDependencies": {

coral-tflite-delegate/src/index.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,20 @@ import * as os from 'os';
2525
//
2626
// The following names are from the python implementation.
2727
// https://github.com/google-coral/pycoral/blob/9972f8ec6dbb8b2f46321e8c0d2513e0b6b152ce/pycoral/utils/edgetpu.py#L34-L38
28-
const libNames = new Map([
28+
const libNames = new Map<NodeJS.Platform, string>([
2929
['linux', 'libedgetpu.so.1'],
3030
['darwin', 'libedgetpu.1.dylib'],
31-
['windows', 'edgetpu.dll'],
31+
['win32', 'edgetpu.dll'],
3232
]);
3333

3434
export class CoralDelegate implements TFLiteDelegatePlugin {
35-
readonly name: 'CoralDelegate';
35+
readonly name = 'CoralDelegate';
3636
readonly tfliteVersion: '2.7';
3737
readonly node: TFLiteDelegatePlugin['node'];
3838

3939
constructor(readonly options: Array<[string, string]> = [],
40-
libPath?: string) {
40+
libPath?: string, platform = os.platform()) {
4141
if (!libPath) {
42-
const platform = os.platform();
4342
libPath = libNames.get(platform);
4443
if (!libPath) {
4544
throw new Error(`Unknown platform ${platform}`);

coral-tflite-delegate/src/index_test.ts

+21-37
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,33 @@
1515
* =============================================================================
1616
*/
1717

18-
import {CoralDelegate} from './index';
19-
import {loadTFLiteModel} from 'tfjs-tflite-node';
20-
import type {TFLiteModel} from 'tfjs-tflite-node/dist/tflite_model';
21-
import * as fs from 'fs';
22-
import * as tf from '@tensorflow/tfjs-core';
2318
import '@tensorflow/tfjs-backend-cpu';
24-
import * as jpeg from 'jpeg-js';
19+
import {CoralDelegate} from './index';
2520

2621
describe('coral delegate', () => {
27-
const modelPath = './test_model/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite';
28-
let model: TFLiteModel;
29-
let parrot: tf.Tensor;
30-
let labels: string[];
31-
32-
beforeEach(async () => {
33-
model = await loadTFLiteModel(modelPath, {
34-
delegates: [new CoralDelegate([['device', 'usb']])],
35-
});
36-
37-
// Load the input image of a parrot.
38-
const parrotJpeg = jpeg.decode(
39-
fs.readFileSync('./test_model/parrot-small.jpg'));
22+
it('has the name "CoralDelegate"', () => {
23+
expect(new CoralDelegate().name).toEqual('CoralDelegate');
24+
});
4025

41-
// Create an RGB array of the parrot's pixels (no Alpha channel).
42-
const {width, height, data} = parrotJpeg;
43-
const parrotRGB = new Uint8Array(width * height * 3);
44-
for (let i = 0; i < width * height; i++) {
45-
const i3 = i * 3;
46-
const i4 = i * 4;
47-
parrotRGB[i3] = data[i4];
48-
parrotRGB[i3 + 1] = data[i4 + 1];
49-
parrotRGB[i3 + 2] = data[i4 + 2];
50-
}
26+
it('stores options', () => {
27+
const options: Array<[string, string]> = [['foo', 'bar'], ['123', '456']];
28+
const coralDelegate = new CoralDelegate(options);
29+
expect(coralDelegate.options).toEqual(options);
30+
});
5131

52-
parrot = tf.tensor(parrotRGB, [1, 224, 224, 3]);
53-
labels = fs.readFileSync('./test_model/inat_bird_labels.txt', 'utf-8')
54-
.split('\n');
32+
it('allows manually setting lib path', () => {
33+
const libPath = 'some lib path';
34+
const coralDelegate = new CoralDelegate([], libPath);
35+
expect(coralDelegate.node.path).toEqual(libPath);
5536
});
5637

57-
it('runs a coral model (will fail without coral device)', () => {
58-
const prediction = model.predict(parrot);
59-
const argmax = tf.argMax(prediction as tf.Tensor, 1);
60-
const label = labels[argmax.dataSync()[0]];
61-
expect(label).toEqual('Ara macao (Scarlet Macaw)');
38+
it('sets the lib path automatically based on platorm', () => {
39+
const coralLinux = new CoralDelegate([], undefined, 'linux');
40+
const coralMac = new CoralDelegate([], undefined, 'darwin');
41+
const coralWindows = new CoralDelegate([], undefined, 'win32');
42+
43+
expect(coralLinux.node.path).toEqual('libedgetpu.so.1');
44+
expect(coralMac.node.path).toEqual('libedgetpu.1.dylib');
45+
expect(coralWindows.node.path).toEqual('edgetpu.dll');
6246
});
6347
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
import {CoralDelegate} from '../index';
19+
import {loadTFLiteModel} from 'tfjs-tflite-node';
20+
import type {TFLiteModel} from 'tfjs-tflite-node/dist/tflite_model';
21+
import * as fs from 'fs';
22+
import * as tf from '@tensorflow/tfjs-core';
23+
import '@tensorflow/tfjs-backend-cpu';
24+
import * as jpeg from 'jpeg-js';
25+
26+
describe('coral delegate', () => {
27+
const modelPath = './test_model/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite';
28+
let model: TFLiteModel;
29+
let parrot: tf.Tensor;
30+
let labels: string[];
31+
32+
beforeEach(async () => {
33+
model = await loadTFLiteModel(modelPath, {
34+
delegates: [new CoralDelegate([['device', 'usb']])],
35+
});
36+
37+
// Load the input image of a parrot.
38+
const parrotJpeg = jpeg.decode(
39+
fs.readFileSync('./test_model/parrot-small.jpg'));
40+
41+
// Create an RGB array of the parrot's pixels (no Alpha channel).
42+
const {width, height, data} = parrotJpeg;
43+
const parrotRGB = new Uint8Array(width * height * 3);
44+
for (let i = 0; i < width * height; i++) {
45+
const i3 = i * 3;
46+
const i4 = i * 4;
47+
parrotRGB[i3] = data[i4];
48+
parrotRGB[i3 + 1] = data[i4 + 1];
49+
parrotRGB[i3 + 2] = data[i4 + 2];
50+
}
51+
52+
parrot = tf.tensor(parrotRGB, [1, 224, 224, 3]);
53+
labels = fs.readFileSync('./test_model/inat_bird_labels.txt', 'utf-8')
54+
.split('\n');
55+
});
56+
57+
it('runs a coral model (will fail without coral device)', () => {
58+
const prediction = model.predict(parrot);
59+
const argmax = tf.argMax(prediction as tf.Tensor, 1);
60+
const label = labels[argmax.dataSync()[0]];
61+
expect(label).toEqual('Ara macao (Scarlet Macaw)');
62+
});
63+
});

coral-tflite-delegate/tslint.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../tslint.json"
3+
}

presubmit.ts

-6
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,7 @@ parentDirs.forEach(curParentDir => {
3939
dirs.push(...curDirs.map(curDir => join(curParentDir, curDir)));
4040
})
4141

42-
const skippedDirs = new Set(['coral-tflite-delegate']);
43-
4442
dirs.forEach(dir => {
45-
if (skippedDirs.has(dir)) {
46-
console.log(`~~~~~~~~~~~~ Skipping ${dir} ~~~~~~~~~~~~`);
47-
return;
48-
}
4943
shell.cd(dir);
5044

5145
if (!fs.existsSync('./package.json')) {

0 commit comments

Comments
 (0)