face-api/src/ssdMobilenetv1/outputLayer.ts

79 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-12-23 18:58:47 +01:00
import * as tf from '../../dist/tfjs.esm';
2020-08-26 00:24:48 +02:00
import { OutputLayerParams } from './types';
function getCenterCoordinatesAndSizesLayer(x: tf.Tensor2D) {
2020-12-23 17:26:55 +01:00
const vec = tf.unstack(tf.transpose(x, [1, 0]));
2020-08-26 00:24:48 +02:00
const sizes = [
tf.sub(vec[2], vec[0]),
2020-12-23 17:26:55 +01:00
tf.sub(vec[3], vec[1]),
];
2020-08-26 00:24:48 +02:00
const centers = [
tf.add(vec[0], tf.div(sizes[0], tf.scalar(2))),
2020-12-23 17:26:55 +01:00
tf.add(vec[1], tf.div(sizes[1], tf.scalar(2))),
];
2020-08-26 00:24:48 +02:00
return {
sizes,
2020-12-23 17:26:55 +01:00
centers,
};
2020-08-26 00:24:48 +02:00
}
function decodeBoxesLayer(x0: tf.Tensor2D, x1: tf.Tensor2D) {
const {
sizes,
2020-12-23 17:26:55 +01:00
centers,
} = getCenterCoordinatesAndSizesLayer(x0);
2020-08-26 00:24:48 +02:00
2020-12-23 17:26:55 +01:00
const vec = tf.unstack(tf.transpose(x1, [1, 0]));
const div0_out = tf.div(tf.mul(tf.exp(tf.div(vec[2], tf.scalar(5))), sizes[0]), tf.scalar(2));
const add0_out = tf.add(tf.mul(tf.div(vec[0], tf.scalar(10)), sizes[0]), centers[0]);
const div1_out = tf.div(tf.mul(tf.exp(tf.div(vec[3], tf.scalar(5))), sizes[1]), tf.scalar(2));
const add1_out = tf.add(tf.mul(tf.div(vec[1], tf.scalar(10)), sizes[1]), centers[1]);
2020-08-26 00:24:48 +02:00
return tf.transpose(
tf.stack([
tf.sub(add0_out, div0_out),
tf.sub(add1_out, div1_out),
tf.add(add0_out, div0_out),
2020-12-23 17:26:55 +01:00
tf.add(add1_out, div1_out),
2020-08-26 00:24:48 +02:00
]),
2020-12-23 17:26:55 +01:00
[1, 0],
);
2020-08-26 00:24:48 +02:00
}
export function outputLayer(
boxPredictions: tf.Tensor4D,
classPredictions: tf.Tensor4D,
2020-12-23 17:26:55 +01:00
params: OutputLayerParams,
2020-08-26 00:24:48 +02:00
) {
return tf.tidy(() => {
2020-12-23 17:26:55 +01:00
const batchSize = boxPredictions.shape[0];
2020-08-26 00:24:48 +02:00
let boxes = decodeBoxesLayer(
tf.reshape(tf.tile(params.extra_dim, [batchSize, 1, 1]), [-1, 4]) as tf.Tensor2D,
2020-12-23 17:26:55 +01:00
tf.reshape(boxPredictions, [-1, 4]) as tf.Tensor2D,
);
2020-08-26 00:24:48 +02:00
boxes = tf.reshape(
boxes,
2020-12-23 17:26:55 +01:00
[batchSize, (boxes.shape[0] / batchSize), 4],
);
2020-08-26 00:24:48 +02:00
2020-12-23 17:26:55 +01:00
const scoresAndClasses = tf.sigmoid(tf.slice(classPredictions, [0, 0, 1], [-1, -1, -1]));
let scores = tf.slice(scoresAndClasses, [0, 0, 0], [-1, -1, 1]) as tf.Tensor;
2020-08-26 00:24:48 +02:00
scores = tf.reshape(
scores,
2020-12-23 17:26:55 +01:00
[batchSize, scores.shape[1] as number],
);
2020-08-26 00:24:48 +02:00
2020-12-23 17:26:55 +01:00
const boxesByBatch = tf.unstack(boxes) as tf.Tensor2D[];
const scoresByBatch = tf.unstack(scores) as tf.Tensor1D[];
2020-08-26 00:24:48 +02:00
return {
boxes: boxesByBatch,
2020-12-23 17:26:55 +01:00
scores: scoresByBatch,
};
});
}