Skip to content

Commit cb52d62

Browse files
resizeLayer
1 parent 42e41f1 commit cb52d62

File tree

6 files changed

+61
-52
lines changed

6 files changed

+61
-52
lines changed

src/faceDetectionNet/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as tf from '@tensorflow/tfjs-core';
2+
3+
import { resizeLayer } from './resizeLayer';
4+
5+
export function faceDetectionNet() {
6+
7+
async function forward(input: ImageData|ImageData[]) {
8+
9+
const imgTensors = (input instanceof ImageData ? [input] : input)
10+
.map(data => tf.fromPixels(data))
11+
.map(data => tf.expandDims(data, 0)) as tf.Tensor4D[]
12+
13+
const imgTensor = tf.cast(tf.concat(imgTensors, 0), 'float32')
14+
15+
const resized = resizeLayer(imgTensor)
16+
17+
18+
19+
20+
return resized
21+
}
22+
23+
return {
24+
forward
25+
}
26+
}

src/faceDetectionNet/preprocessor/index.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/faceDetectionNet/preprocessor/whileLayer.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/faceDetectionNet/resizeLayer.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as tf from '@tensorflow/tfjs-core';
2+
3+
// TODO: hardcoded params
4+
const resizedImageSize = [512, 512] as [number, number]
5+
const weight = tf.scalar(0.007843137718737125)
6+
const bias = tf.scalar(1)
7+
8+
export function resizeLayer(imgTensor: tf.Tensor4D) {
9+
const resizedImgs = tf.image.resizeBilinear(imgTensor, resizedImageSize, false)
10+
return tf.sub(tf.mul(resizedImgs, weight), bias)
11+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { euclideanDistance } from './euclideanDistance';
2+
import { faceDetectionNet } from './faceDetectionNet';
23
import { faceRecognitionNet } from './faceRecognitionNet';
34
import { normalize } from './normalize';
45

56
export {
67
euclideanDistance,
8+
faceDetectionNet,
79
faceRecognitionNet,
810
normalize
911
}

src/tfcpatches/TensorArray.ts

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

33
export class TensorArray {
4-
private _tensors: tf.Tensor[] | undefined
4+
private _tensors: tf.Tensor[]
55

66
constructor(
7-
private _size: number,
8-
private _dtype: tf.DataType = null,
9-
private _elementShape: number[] = null,
7+
size: number,
8+
private _dtype: tf.DataType | null = null,
9+
private _elementShape: number[] | null = null,
1010
private _dynamicSize: boolean = false,
1111
private _clearAfterRead: boolean = true,
1212
private _identicalElementShapes: boolean = false,
13-
private _tensorArrayName: string = null
13+
private _tensorArrayName: string = ''
1414
) {
15-
if (_size) {
16-
this._tensors = Array(_size).fill(0).map(_ => tf.scalar(0))
17-
}
15+
this._tensors = size
16+
? Array(size).fill(0).map(_ => tf.scalar(0))
17+
: []
1818
}
1919

2020
public scatter(indices: tf.Tensor1D, value: tf.Tensor, unusedFlow: tf.Scalar): tf.Scalar {
21+
this.expectValidSize('scatter', indices)
2122
if (indices.shape.length !== 1) {
2223
throw new Error(`scatter - expected rank of indices (${indices.shape.length}) to be 1`)
2324
}
24-
if (indices.shape[0] > this._size) {
25-
throw new Error(`scatter - expected indices.shape[0] (${indices.shape[0]}) to be >= this._size (${this.size})`)
26-
}
2725
if (indices.shape[0] !== value.shape[0]) {
2826
throw new Error(`scatter - expected indices.shape[0] (${indices.shape[0]}) to equal value.shape[0] (${value.shape[0]})`)
2927
}
3028

3129
const unstacked = tf.unstack(value, 0)
3230
Array.from(indices.dataSync()).forEach((idx, i) => {
33-
this._tensors[idx] = unstacked[i]
31+
(this._tensors as tf.Tensor[])[idx] = unstacked[i]
3432
})
3533

3634
const unusedFlowOut = tf.scalar(0)
3735
return unusedFlowOut
3836
}
3937

4038
public gather(indices: tf.Tensor1D, unusedFlow: tf.Scalar, dtype?: tf.DataType, elementShape?: number[]) : tf.Tensor {
39+
this.expectValidSize('gather', indices)
4140
const tensors = Array.from(indices.dataSync()).map(idx => this._tensors[idx])
4241
return tf.concat(tensors)
4342
}
4443

45-
public size(unusedFlow: tf.Scalar) {
46-
return this._size
44+
public size(unusedFlow?: tf.Scalar) {
45+
return this._tensors.length
46+
}
47+
48+
private expectValidSize(methodName: string, indices: tf.Tensor1D) {
49+
if (!this._tensors) {
50+
throw new Error('scatter - TensorArray is not initialized')
51+
}
52+
if (indices.shape[0] > this._tensors.length) {
53+
throw new Error(`${methodName} - expected indices.shape[0] (${indices.shape[0]}) to be >= this.size() (${this.size()})`)
54+
}
4755
}
4856
}

0 commit comments

Comments
 (0)