Commit b65c74c1 by vincent

dispose image and input tensors on early return

parent e7d1d043
...@@ -54,6 +54,14 @@ export class Mtcnn extends NeuralNetwork<NetParams> { ...@@ -54,6 +54,14 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
) )
) )
const onReturn = (results: any) => {
// dispose tensors on return
imgTensor.dispose()
input.dispose()
stats.total = Date.now() - tsTotal
return results
}
const [height, width] = imgTensor.shape.slice(1) const [height, width] = imgTensor.shape.slice(1)
const scales = pyramidDown(minFaceSize, scaleFactor, [height, width]) const scales = pyramidDown(minFaceSize, scaleFactor, [height, width])
...@@ -71,8 +79,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> { ...@@ -71,8 +79,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
stats.total_stage1 = Date.now() - ts stats.total_stage1 = Date.now() - ts
if (!out1.boxes.length) { if (!out1.boxes.length) {
stats.total = Date.now() - tsTotal return onReturn({ results: [], stats })
return { results: [], stats }
} }
stats.stage2_numInputBoxes = out1.boxes.length stats.stage2_numInputBoxes = out1.boxes.length
...@@ -83,8 +90,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> { ...@@ -83,8 +90,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
stats.total_stage2 = Date.now() - ts stats.total_stage2 = Date.now() - ts
if (!out2.boxes.length) { if (!out2.boxes.length) {
stats.total = Date.now() - tsTotal return onReturn({ results: [], stats })
return { results: [], stats }
} }
stats.stage3_numInputBoxes = out2.boxes.length stats.stage3_numInputBoxes = out2.boxes.length
...@@ -93,9 +99,6 @@ export class Mtcnn extends NeuralNetwork<NetParams> { ...@@ -93,9 +99,6 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
const out3 = await stage3(inputCanvas, out2.boxes, scoreThresholds[2], params.onet, stats) const out3 = await stage3(inputCanvas, out2.boxes, scoreThresholds[2], params.onet, stats)
stats.total_stage3 = Date.now() - ts stats.total_stage3 = Date.now() - ts
imgTensor.dispose()
input.dispose()
const results = out3.boxes.map((box, idx) => ({ const results = out3.boxes.map((box, idx) => ({
faceDetection: new FaceDetection( faceDetection: new FaceDetection(
out3.scores[idx], out3.scores[idx],
...@@ -116,8 +119,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> { ...@@ -116,8 +119,7 @@ export class Mtcnn extends NeuralNetwork<NetParams> {
) )
})) }))
stats.total = Date.now() - tsTotal return onReturn({ results, stats })
return { results, stats }
} }
public async forward( public async forward(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment