Skip to content

Commit 06972db

Browse files
implemented faster normalize to reduce prediction time
1 parent 39069ea commit 06972db

File tree

8 files changed

+27
-25
lines changed

8 files changed

+27
-25
lines changed

dist/face-recognition.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/faceRecognitionNet/index.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/faceRecognitionNet/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/normalize.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export declare function normalize(arr: number[]): number[];
1+
import * as tf from '@tensorflow/tfjs-core';
2+
export declare function normalize(input: number[]): tf.Tensor4D;

dist/normalize.js

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/normalize.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/faceRecognitionNet/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ export function faceRecognitionNet(weights: Float32Array) {
1212

1313
return tf.tidy(() => {
1414

15-
const norm = normalize(input)
16-
17-
const x = tf.tensor4d(norm, [1, 150, 150, 3])
15+
const x = normalize(input)
1816

1917
let out = convDown(x, params.conv32_down)
2018
out = tf.maxPool(out, 3, 2, 'valid')

src/normalize.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
export function normalize(arr: number[]) {
2-
const avg_r = 122.782;
3-
const avg_g = 117.001;
4-
const avg_b = 104.298;
5-
const avgs = [avg_r, avg_g, avg_b]
6-
return arr.map((val, i) => {
7-
const avg = avgs[i % 3]
8-
return (val - avg) / 256
1+
import * as tf from '@tensorflow/tfjs-core';
2+
3+
export function normalize(input: number[]): tf.Tensor4D {
4+
return tf.tidy(() => {
5+
const avg_r = tf.fill([1, 150, 150, 1], 122.782);
6+
const avg_g = tf.fill([1, 150, 150, 1], 117.001);
7+
const avg_b = tf.fill([1, 150, 150, 1], 104.298);
8+
const avg_rgb = tf.concat([avg_r, avg_g, avg_b], 3)
9+
10+
const x = tf.tensor4d(input, [1, 150, 150, 3])
11+
return tf.div(tf.sub(x, avg_rgb), tf.fill(x.shape, 256))
912
})
1013
}

0 commit comments

Comments
 (0)