face-api/src/ssdMobilenetv1/outputLayer.ts

60 lines
2.0 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 = [
2021-03-20 02:39:45 +01:00
tf.add(vec[0], tf.div(sizes[0], 2)),
tf.add(vec[1], tf.div(sizes[1], 2)),
2020-12-23 17:26:55 +01:00
];
2021-03-19 23:46:36 +01:00
return { sizes, centers };
2020-08-26 00:24:48 +02:00
}
function decodeBoxesLayer(x0: tf.Tensor2D, x1: tf.Tensor2D) {
2021-03-19 23:46:36 +01:00
const { sizes, 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]));
2021-03-20 02:39:45 +01:00
const div0_out = tf.div(tf.mul(tf.exp(tf.div(vec[2], 5)), sizes[0]), 2);
const add0_out = tf.add(tf.mul(tf.div(vec[0], 10), sizes[0]), centers[0]);
const div1_out = tf.div(tf.mul(tf.exp(tf.div(vec[3], 5)), sizes[1]), 2);
const add1_out = tf.add(tf.mul(tf.div(vec[1], 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
}
2021-03-19 23:46:36 +01:00
export function outputLayer(boxPredictions: tf.Tensor4D, classPredictions: tf.Tensor4D, 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,
);
2021-03-19 23:46:36 +01:00
boxes = tf.reshape(boxes, [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
2021-03-19 23:46:36 +01:00
scores = tf.reshape(scores, [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
2021-03-19 23:46:36 +01:00
return { boxes: boxesByBatch, scores: scoresByBatch };
2020-12-23 17:26:55 +01:00
});
}