@@ -54,6 +54,14 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
54
54
)
55
55
)
56
56
57
+ const onReturn = ( results : any ) => {
58
+ // dispose tensors on return
59
+ imgTensor . dispose ( )
60
+ input . dispose ( )
61
+ stats . total = Date . now ( ) - tsTotal
62
+ return results
63
+ }
64
+
57
65
const [ height , width ] = imgTensor . shape . slice ( 1 )
58
66
59
67
const scales = pyramidDown ( minFaceSize , scaleFactor , [ height , width ] )
@@ -71,8 +79,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
71
79
stats . total_stage1 = Date . now ( ) - ts
72
80
73
81
if ( ! out1 . boxes . length ) {
74
- stats . total = Date . now ( ) - tsTotal
75
- return { results : [ ] , stats }
82
+ return onReturn ( { results : [ ] , stats } )
76
83
}
77
84
78
85
stats . stage2_numInputBoxes = out1 . boxes . length
@@ -83,8 +90,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
83
90
stats . total_stage2 = Date . now ( ) - ts
84
91
85
92
if ( ! out2 . boxes . length ) {
86
- stats . total = Date . now ( ) - tsTotal
87
- return { results : [ ] , stats }
93
+ return onReturn ( { results : [ ] , stats } )
88
94
}
89
95
90
96
stats . stage3_numInputBoxes = out2 . boxes . length
@@ -93,9 +99,6 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
93
99
const out3 = await stage3 ( inputCanvas , out2 . boxes , scoreThresholds [ 2 ] , params . onet , stats )
94
100
stats . total_stage3 = Date . now ( ) - ts
95
101
96
- imgTensor . dispose ( )
97
- input . dispose ( )
98
-
99
102
const results = out3 . boxes . map ( ( box , idx ) => ( {
100
103
faceDetection : new FaceDetection (
101
104
out3 . scores [ idx ] ,
@@ -116,8 +119,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
116
119
)
117
120
} ) )
118
121
119
- stats . total = Date . now ( ) - tsTotal
120
- return { results, stats }
122
+ return onReturn ( { results, stats } )
121
123
}
122
124
123
125
public async forward (
0 commit comments