Skip to content

Commit b65c74c

Browse files
dispose image and input tensors on early return
1 parent e7d1d04 commit b65c74c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/mtcnn/Mtcnn.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
5454
)
5555
)
5656

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+
5765
const [height, width] = imgTensor.shape.slice(1)
5866

5967
const scales = pyramidDown(minFaceSize, scaleFactor, [height, width])
@@ -71,8 +79,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
7179
stats.total_stage1 = Date.now() - ts
7280

7381
if (!out1.boxes.length) {
74-
stats.total = Date.now() - tsTotal
75-
return { results: [], stats }
82+
return onReturn({ results: [], stats })
7683
}
7784

7885
stats.stage2_numInputBoxes = out1.boxes.length
@@ -83,8 +90,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
8390
stats.total_stage2 = Date.now() - ts
8491

8592
if (!out2.boxes.length) {
86-
stats.total = Date.now() - tsTotal
87-
return { results: [], stats }
93+
return onReturn({ results: [], stats })
8894
}
8995

9096
stats.stage3_numInputBoxes = out2.boxes.length
@@ -93,9 +99,6 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
9399
const out3 = await stage3(inputCanvas, out2.boxes, scoreThresholds[2], params.onet, stats)
94100
stats.total_stage3 = Date.now() - ts
95101

96-
imgTensor.dispose()
97-
input.dispose()
98-
99102
const results = out3.boxes.map((box, idx) => ({
100103
faceDetection: new FaceDetection(
101104
out3.scores[idx],
@@ -116,8 +119,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
116119
)
117120
}))
118121

119-
stats.total = Date.now() - tsTotal
120-
return { results, stats }
122+
return onReturn({ results, stats })
121123
}
122124

123125
public async forward(

0 commit comments

Comments
 (0)