1
+ import * as faceapi from '../../../src' ;
2
+ import { SizeType } from '../../../src/tinyYolov2/types' ;
3
+ import { describeWithNets , expectAllTensorsReleased , expectRectClose } from '../../utils' ;
4
+ import { expectedTinyYolov2Boxes } from './expectedResults' ;
5
+
6
+
7
+ describe ( 'tinyYolov2' , ( ) => {
8
+
9
+ let imgEl : HTMLImageElement
10
+
11
+ beforeAll ( async ( ) => {
12
+ const img = await ( await fetch ( 'base/test/images/faces.jpg' ) ) . blob ( )
13
+ imgEl = await faceapi . bufferToImage ( img )
14
+ } )
15
+
16
+ describeWithNets ( 'quantized weights' , { withTinyYolov2 : { quantized : true } } , ( { tinyYolov2 } ) => {
17
+
18
+ it ( 'inputSize lg, finds all faces' , async ( ) => {
19
+ const detections = await tinyYolov2 . locateFaces ( imgEl , { inputSize : SizeType . LG } )
20
+
21
+ const expectedScores = [ 0.86 , 0.86 , 0.85 , 0.83 , 0.81 , 0.81 ]
22
+ const maxBoxDelta = 3
23
+ const boxOrder = [ 0 , 1 , 2 , 3 , 4 , 5 ]
24
+
25
+ expect ( detections . length ) . toEqual ( 6 )
26
+ detections . forEach ( ( det , i ) => {
27
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
28
+ expectRectClose ( det . getBox ( ) , expectedTinyYolov2Boxes [ boxOrder [ i ] ] , maxBoxDelta )
29
+ } )
30
+ } )
31
+
32
+ it ( 'inputSize md, finds all faces' , async ( ) => {
33
+ const detections = await tinyYolov2 . locateFaces ( imgEl , { inputSize : SizeType . MD } )
34
+
35
+ const expectedScores = [ 0.89 , 0.87 , 0.83 , 0.82 , 0.81 , 0.72 ]
36
+ const maxBoxDelta = 16
37
+ const boxOrder = [ 5 , 4 , 0 , 2 , 1 , 3 ]
38
+
39
+ expect ( detections . length ) . toEqual ( 6 )
40
+ detections . forEach ( ( det , i ) => {
41
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
42
+ expectRectClose ( det . getBox ( ) , expectedTinyYolov2Boxes [ boxOrder [ i ] ] , maxBoxDelta )
43
+ } )
44
+ } )
45
+
46
+ it ( 'inputSize custom, finds all faces' , async ( ) => {
47
+ const detections = await tinyYolov2 . locateFaces ( imgEl , { inputSize : 416 } )
48
+
49
+ const expectedScores = [ 0.89 , 0.87 , 0.83 , 0.82 , 0.81 , 0.72 ]
50
+ const maxBoxDelta = 16
51
+ const boxOrder = [ 5 , 4 , 0 , 2 , 1 , 3 ]
52
+
53
+ expect ( detections . length ) . toEqual ( 6 )
54
+ detections . forEach ( ( det , i ) => {
55
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
56
+ expectRectClose ( det . getBox ( ) , expectedTinyYolov2Boxes [ boxOrder [ i ] ] , maxBoxDelta )
57
+ } )
58
+ } )
59
+
60
+ } )
61
+
62
+ describeWithNets ( 'uncompressed weights' , { withTinyYolov2 : { quantized : false } } , ( { tinyYolov2 } ) => {
63
+
64
+ it ( 'inputSize lg, finds all faces' , async ( ) => {
65
+ const detections = await tinyYolov2 . locateFaces ( imgEl , { inputSize : SizeType . LG } )
66
+
67
+ const expectedScores = [ 0.86 , 0.86 , 0.85 , 0.83 , 0.81 , 0.81 ]
68
+ const maxBoxDelta = 1
69
+ const boxOrder = [ 0 , 1 , 2 , 3 , 4 , 5 ]
70
+
71
+ expect ( detections . length ) . toEqual ( 6 )
72
+ detections . forEach ( ( det , i ) => {
73
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
74
+ expectRectClose ( det . getBox ( ) , expectedTinyYolov2Boxes [ boxOrder [ i ] ] , maxBoxDelta )
75
+ } )
76
+ } )
77
+
78
+ it ( 'inputSize md, finds all faces' , async ( ) => {
79
+ const detections = await tinyYolov2 . locateFaces ( imgEl , { inputSize : SizeType . MD } )
80
+
81
+ const expectedScores = [ 0.89 , 0.87 , 0.83 , 0.83 , 0.81 , 0.73 ]
82
+ const maxBoxDelta = 14
83
+ const boxOrder = [ 5 , 4 , 2 , 0 , 1 , 3 ]
84
+
85
+ expect ( detections . length ) . toEqual ( 6 )
86
+ detections . forEach ( ( det , i ) => {
87
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
88
+ expectRectClose ( det . getBox ( ) , expectedTinyYolov2Boxes [ boxOrder [ i ] ] , maxBoxDelta )
89
+ } )
90
+ } )
91
+
92
+ it ( 'inputSize custom, finds all faces' , async ( ) => {
93
+ const detections = await tinyYolov2 . locateFaces ( imgEl , { inputSize : 416 } )
94
+
95
+ const expectedScores = [ 0.89 , 0.87 , 0.83 , 0.83 , 0.81 , 0.73 ]
96
+ const maxBoxDelta = 14
97
+ const boxOrder = [ 5 , 4 , 2 , 0 , 1 , 3 ]
98
+
99
+ expect ( detections . length ) . toEqual ( 6 )
100
+ detections . forEach ( ( det , i ) => {
101
+ expect ( det . getScore ( ) ) . toBeCloseTo ( expectedScores [ i ] , 2 )
102
+ expectRectClose ( det . getBox ( ) , expectedTinyYolov2Boxes [ boxOrder [ i ] ] , maxBoxDelta )
103
+ } )
104
+ } )
105
+
106
+ } )
107
+
108
+ describe ( 'no memory leaks' , ( ) => {
109
+
110
+ describe ( 'NeuralNetwork, uncompressed model' , ( ) => {
111
+
112
+ it ( 'disposes all param tensors' , async ( ) => {
113
+ await expectAllTensorsReleased ( async ( ) => {
114
+ const res = await fetch ( 'base/weights_uncompressed/tiny_yolov2_model.weights' )
115
+ const weights = new Float32Array ( await res . arrayBuffer ( ) )
116
+ const net = faceapi . createTinyYolov2 ( weights )
117
+ net . dispose ( )
118
+ } )
119
+ } )
120
+
121
+ } )
122
+
123
+ describe ( 'NeuralNetwork, quantized model' , ( ) => {
124
+
125
+ it ( 'disposes all param tensors' , async ( ) => {
126
+ await expectAllTensorsReleased ( async ( ) => {
127
+ const net = new faceapi . TinyYolov2 ( )
128
+ await net . load ( 'base/weights' )
129
+ net . dispose ( )
130
+ } )
131
+ } )
132
+
133
+ } )
134
+
135
+ } )
136
+
137
+ } )
0 commit comments