Skip to content

Commit 9cd6d13

Browse files
unit tests for quanzized face detection and face landmark net
1 parent 61b702a commit 9cd6d13

File tree

7 files changed

+148
-56
lines changed

7 files changed

+148
-56
lines changed

dist/faceDetectionNet/FaceDetectionNet.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TNetInput } from '../types';
44
import { FaceDetection } from './FaceDetection';
55
export declare class FaceDetectionNet {
66
private _params;
7-
load(weightsOrUrl: Float32Array | string | undefined): Promise<void>;
7+
load(weightsOrUrl?: Float32Array | string): Promise<void>;
88
extractWeights(weights: Float32Array): void;
99
private forwardTensor(imgTensor);
1010
forward(input: tf.Tensor | NetInput | TNetInput): {

dist/faceDetectionNet/FaceDetectionNet.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/faceLandmarkNet/FaceLandmarkNet.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/faceDetectionNet/FaceDetectionNet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class FaceDetectionNet {
1919

2020
private _params: NetParams
2121

22-
public async load(weightsOrUrl: Float32Array | string | undefined): Promise<void> {
22+
public async load(weightsOrUrl?: Float32Array | string): Promise<void> {
2323
if (weightsOrUrl instanceof Float32Array) {
2424
this.extractWeights(weightsOrUrl)
2525
return

test/e2e/faceDetectionNet.test.ts

Lines changed: 89 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,113 @@
11
import * as faceapi from '../../src';
22
import { FaceDetection } from '../../src/faceDetectionNet/FaceDetection';
33
import { IRect } from '../../src/Rect';
4+
import { expectMaxDelta } from '../utils';
45

5-
function expectFaceDetectionEquals(result: FaceDetection, score: number, expectedBox: IRect) {
6-
const { x, y, width, height } = result.getBox()
7-
expect(result.getScore()).toBeCloseTo(score, 2)
8-
expect(Math.floor(x)).toEqual(expectedBox.x)
9-
expect(Math.floor(y)).toEqual(expectedBox.y)
10-
expect(Math.floor(width)).toEqual(expectedBox.width)
11-
expect(Math.floor(height)).toEqual(expectedBox.height)
6+
function expectRectClose(
7+
result: IRect,
8+
expectedBox: IRect,
9+
maxDelta: number
10+
) {
11+
const { x, y, width, height } = result
12+
expectMaxDelta(x, expectedBox.x, maxDelta)
13+
expectMaxDelta(y, expectedBox.y, maxDelta)
14+
expectMaxDelta(width, expectedBox.width, maxDelta)
15+
expectMaxDelta(height, expectedBox.height, maxDelta)
1216
}
1317

18+
const expectedBoxes = [
19+
{ x: 48, y: 253, width: 104, height: 129 },
20+
{ x: 260, y: 227, width: 76, height: 117 },
21+
{ x: 466, y: 165, width: 88, height: 130 },
22+
{ x: 234, y: 36, width: 84, height: 119 },
23+
{ x: 577, y: 65, width: 84, height: 105 },
24+
{ x: 84, y: 14, width: 79, height: 132 }
25+
]
26+
1427
describe('faceDetectionNet', () => {
1528

16-
let faceDetectionNet: faceapi.FaceDetectionNet, imgEl: HTMLImageElement
29+
let imgEl: HTMLImageElement
1730

1831
beforeAll(async () => {
19-
const res = await fetch('base/weights/uncompressed/face_detection_model.weights')
20-
const weights = new Float32Array(await res.arrayBuffer())
21-
faceDetectionNet = faceapi.faceDetectionNet(weights)
22-
2332
const img = await (await fetch('base/test/images/faces.jpg')).blob()
2433
imgEl = await faceapi.bufferToImage(img)
2534
})
2635

27-
it('scores > 0.8', async () => {
28-
const { width, height } = imgEl
36+
describe('uncompressed weights', () => {
37+
38+
let faceDetectionNet: faceapi.FaceDetectionNet
39+
40+
const expectedScores = [0.98, 0.89, 0.82, 0.75, 0.58, 0.55]
41+
const maxBoxDelta = 1
42+
43+
beforeAll(async () => {
44+
const res = await fetch('base/weights/uncompressed/face_detection_model.weights')
45+
const weights = new Float32Array(await res.arrayBuffer())
46+
faceDetectionNet = faceapi.faceDetectionNet(weights)
47+
})
2948

30-
const result = await faceDetectionNet.locateFaces(imgEl) as FaceDetection[]
31-
expect(result.length).toEqual(3)
49+
it('scores > 0.8', async () => {
50+
const detections = await faceDetectionNet.locateFaces(imgEl) as FaceDetection[]
3251

33-
result.forEach(res => {
34-
expect(res.getImageWidth()).toEqual(width)
35-
expect(res.getImageHeight()).toEqual(height)
52+
expect(detections.length).toEqual(3)
53+
detections.forEach((det, i) => {
54+
expect(det.getImageWidth()).toEqual(imgEl.width)
55+
expect(det.getImageHeight()).toEqual(imgEl.height)
56+
expect(det.getScore()).toBeCloseTo(expectedScores[i], 2)
57+
expectRectClose(det.getBox(), expectedBoxes[i], maxBoxDelta)
58+
})
3659
})
37-
const [d0, d1, d2] = result
38-
expectFaceDetectionEquals(d0, 0.98, { x: 48, y: 253, width: 104, height: 129 })
39-
expectFaceDetectionEquals(d1, 0.89, { x: 260, y: 227, width: 76, height: 117 })
40-
expectFaceDetectionEquals(d2, 0.82, { x: 466, y: 165, width: 88, height: 130 })
60+
61+
it('scores > 0.5', async () => {
62+
const detections = await faceDetectionNet.locateFaces(imgEl, 0.5) as FaceDetection[]
63+
64+
expect(detections.length).toEqual(6)
65+
detections.forEach((det, i) => {
66+
expect(det.getImageWidth()).toEqual(imgEl.width)
67+
expect(det.getImageHeight()).toEqual(imgEl.height)
68+
expect(det.getScore()).toBeCloseTo(expectedScores[i], 2)
69+
expectRectClose(det.getBox(), expectedBoxes[i], maxBoxDelta)
70+
})
71+
})
72+
4173
})
4274

43-
it('scores > 0.5', async () => {
44-
const { width, height } = imgEl
75+
describe('quantized weights', () => {
4576

46-
const result = await faceDetectionNet.locateFaces(imgEl, 0.5) as FaceDetection[]
47-
expect(result.length).toEqual(6)
77+
let faceDetectionNet: faceapi.FaceDetectionNet
4878

49-
result.forEach(res => {
50-
expect(res.getImageWidth()).toEqual(width)
51-
expect(res.getImageHeight()).toEqual(height)
79+
const expectedScores = [0.97, 0.88, 0.83, 0.82, 0.59, 0.52]
80+
const maxBoxDelta = 5
81+
82+
beforeAll(async () => {
83+
faceDetectionNet = new faceapi.FaceDetectionNet()
84+
await faceDetectionNet.load('base/weights')
5285
})
53-
const [d0, d1, d2, d3, d4, d5] = result
54-
expectFaceDetectionEquals(d0, 0.98, { x: 48, y: 253, width: 104, height: 129 })
55-
expectFaceDetectionEquals(d1, 0.89, { x: 260, y: 227, width: 76, height: 117 })
56-
expectFaceDetectionEquals(d2, 0.82, { x: 466, y: 165, width: 88, height: 130 })
57-
expectFaceDetectionEquals(d3, 0.75, { x: 234, y: 36, width: 84, height: 119 })
58-
expectFaceDetectionEquals(d4, 0.58, { x: 577, y: 65, width: 84, height: 105 })
59-
expectFaceDetectionEquals(d5, 0.55, { x: 84, y: 14, width: 79, height: 132 })
86+
87+
it('scores > 0.8', async () => {
88+
const detections = await faceDetectionNet.locateFaces(imgEl) as FaceDetection[]
89+
90+
expect(detections.length).toEqual(4)
91+
detections.forEach((det, i) => {
92+
expect(det.getImageWidth()).toEqual(imgEl.width)
93+
expect(det.getImageHeight()).toEqual(imgEl.height)
94+
expect(det.getScore()).toBeCloseTo(expectedScores[i], 2)
95+
expectRectClose(det.getBox(), expectedBoxes[i], maxBoxDelta)
96+
})
97+
})
98+
99+
it('scores > 0.5', async () => {
100+
const detections = await faceDetectionNet.locateFaces(imgEl, 0.5) as FaceDetection[]
101+
102+
expect(detections.length).toEqual(6)
103+
detections.forEach((det, i) => {
104+
expect(det.getImageWidth()).toEqual(imgEl.width)
105+
expect(det.getImageHeight()).toEqual(imgEl.height)
106+
expect(det.getScore()).toBeCloseTo(expectedScores[i], 2)
107+
expectRectClose(det.getBox(), expectedBoxes[i], maxBoxDelta)
108+
})
109+
})
110+
60111
})
112+
61113
})

0 commit comments

Comments
 (0)