face-api/src/ops/normalize.ts

13 lines
490 B
TypeScript
Raw Normal View History

2020-10-13 22:57:06 +02:00
import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js';
2020-08-18 13:54:53 +02:00
export function normalize(x: tf.Tensor4D, meanRgb: number[]): tf.Tensor4D {
return tf.tidy(() => {
const [r, g, b] = meanRgb
2020-10-26 01:01:36 +01:00
const avg_r = tf.fill([...x.shape.slice(0, 3), 1], r, 'float32')
const avg_g = tf.fill([...x.shape.slice(0, 3), 1], g, 'float32')
const avg_b = tf.fill([...x.shape.slice(0, 3), 1], b, 'float32')
2020-08-18 13:54:53 +02:00
const avg_rgb = tf.concat([avg_r, avg_g, avg_b], 3)
return tf.sub(x, avg_rgb)
})
}