Skip to content

Commit 7152e12

Browse files
added face recognition net tests to identify memory leaks
1 parent d3ddbb5 commit 7152e12

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

test/tests/e2e/faceRecognitionNet.test.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as tf from '@tensorflow/tfjs-core';
22

33
import * as faceapi from '../../../src';
4+
import { NetInput } from '../../../src/NetInput';
5+
import { expectAllTensorsReleased } from '../../utils';
6+
import { toNetInput } from '../../../src';
47

58
describe('faceRecognitionNet', () => {
69

@@ -153,4 +156,141 @@ describe('faceRecognitionNet', () => {
153156

154157
})
155158

159+
describe('no memory leaks', () => {
160+
161+
let faceRecognitionNet: faceapi.FaceRecognitionNet
162+
163+
beforeAll(async () => {
164+
const res = await fetch('base/weights/uncompressed/face_recognition_model.weights')
165+
const weights = new Float32Array(await res.arrayBuffer())
166+
faceRecognitionNet = faceapi.faceRecognitionNet(weights)
167+
})
168+
169+
describe('forwardInput', () => {
170+
171+
it('single image element', async () => {
172+
await expectAllTensorsReleased(async () => {
173+
const netInput = (new NetInput([imgEl1])).managed()
174+
const outTensor = await faceRecognitionNet.forwardInput(netInput)
175+
outTensor.dispose()
176+
})
177+
})
178+
179+
it('multiple image elements', async () => {
180+
await expectAllTensorsReleased(async () => {
181+
const netInput = (new NetInput([imgEl1, imgEl1, imgEl1])).managed()
182+
const outTensor = await faceRecognitionNet.forwardInput(netInput)
183+
outTensor.dispose()
184+
})
185+
})
186+
187+
it('single tf.Tensor3D', async () => {
188+
const tensor = tf.fromPixels(imgEl1)
189+
190+
await expectAllTensorsReleased(async () => {
191+
const netInput = (new NetInput([tensor])).managed()
192+
const outTensor = await faceRecognitionNet.forwardInput(netInput)
193+
outTensor.dispose()
194+
})
195+
196+
tensor.dispose()
197+
})
198+
199+
it('multiple tf.Tensor3Ds', async () => {
200+
const tensors = [imgEl1, imgEl1, imgEl1].map(el => tf.fromPixels(el))
201+
202+
await expectAllTensorsReleased(async () => {
203+
const netInput = (new NetInput(tensors)).managed()
204+
const outTensor = await faceRecognitionNet.forwardInput(netInput)
205+
outTensor.dispose()
206+
})
207+
208+
tensors.forEach(t => t.dispose())
209+
})
210+
211+
it('single batch size 1 tf.Tensor4Ds', async () => {
212+
const tensor = tf.tidy(() => tf.fromPixels(imgEl1).expandDims()) as tf.Tensor4D
213+
214+
await expectAllTensorsReleased(async () => {
215+
const outTensor = await faceRecognitionNet.forwardInput(await toNetInput(tensor, true))
216+
outTensor.dispose()
217+
})
218+
219+
tensor.dispose()
220+
})
221+
222+
it('multiple batch size 1 tf.Tensor4Ds', async () => {
223+
const tensors = [imgEl1, imgEl1, imgEl1]
224+
.map(el => tf.tidy(() => tf.fromPixels(el).expandDims())) as tf.Tensor4D[]
225+
226+
await expectAllTensorsReleased(async () => {
227+
const outTensor = await faceRecognitionNet.forwardInput(await toNetInput(tensors, true))
228+
outTensor.dispose()
229+
})
230+
231+
tensors.forEach(t => t.dispose())
232+
})
233+
234+
})
235+
236+
describe('computeFaceDescriptor', () => {
237+
238+
it('single image element', async () => {
239+
await expectAllTensorsReleased(async () => {
240+
await faceRecognitionNet.computeFaceDescriptor(imgEl1)
241+
})
242+
})
243+
244+
it('multiple image elements', async () => {
245+
await expectAllTensorsReleased(async () => {
246+
await faceRecognitionNet.computeFaceDescriptor([imgEl1, imgEl1, imgEl1])
247+
})
248+
})
249+
250+
it('single tf.Tensor3D', async () => {
251+
const tensor = tf.fromPixels(imgEl1)
252+
253+
await expectAllTensorsReleased(async () => {
254+
await faceRecognitionNet.computeFaceDescriptor(tensor)
255+
})
256+
257+
tensor.dispose()
258+
})
259+
260+
it('multiple tf.Tensor3Ds', async () => {
261+
const tensors = [imgEl1, imgEl1, imgEl1].map(el => tf.fromPixels(el))
262+
263+
264+
await expectAllTensorsReleased(async () => {
265+
await faceRecognitionNet.computeFaceDescriptor(tensors)
266+
})
267+
268+
tensors.forEach(t => t.dispose())
269+
})
270+
271+
it('single batch size 1 tf.Tensor4Ds', async () => {
272+
const tensor = tf.tidy(() => tf.fromPixels(imgEl1).expandDims()) as tf.Tensor4D
273+
274+
await expectAllTensorsReleased(async () => {
275+
await faceRecognitionNet.computeFaceDescriptor(tensor)
276+
})
277+
278+
tensor.dispose()
279+
})
280+
281+
it('multiple batch size 1 tf.Tensor4Ds', async () => {
282+
const tensors = [imgEl1, imgEl1, imgEl1]
283+
.map(el => tf.tidy(() => tf.fromPixels(el).expandDims())) as tf.Tensor4D[]
284+
285+
await expectAllTensorsReleased(async () => {
286+
await faceRecognitionNet.computeFaceDescriptor(tensors)
287+
})
288+
289+
tensors.forEach(t => t.dispose())
290+
})
291+
292+
})
293+
})
294+
295+
156296
})

0 commit comments

Comments
 (0)