Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const numAnchors = anchors.shape[0];
// Reshape to height, width, num_anchors, box_params.
const anchorsTensor = tf.reshape(anchors, [1, 1, numAnchors, 2]);
const gridShape = feats.shape.slice(1, 3); // height, width
const gridY = tf.tile(tf.reshape(tf.range(0, gridShape[0]), [-1, 1, 1, 1]), [1, gridShape[1], 1, 1]);
const gridX = tf.tile(tf.reshape(tf.range(0, gridShape[1]), [1, -1, 1, 1]), [gridShape[0], 1, 1, 1]);
const grid = tf.concat([gridX, gridY], 3).cast(feats.dtype);
feats = feats.reshape([gridShape[0], gridShape[1], numAnchors, numClasses + 5]);
const [xy, wh, con, probs] = tf.split(feats, [2, 2, 1, numClasses], 3);
// Adjust preditions to each spatial grid point and anchor size.
const boxXy = tf.div(tf.add(tf.sigmoid(xy), grid), gridShape.reverse());
const boxWh = tf.div(tf.mul(tf.exp(wh), anchorsTensor), inputShape.reverse());
const boxConfidence = tf.sigmoid(con);
let boxClassProbs;
if (isV3) {
boxClassProbs = tf.sigmoid(probs);
} else {
boxClassProbs = tf.softmax(probs);
}
return [boxXy, boxWh, boxConfidence, boxClassProbs];
}
) {
const numAnchors = anchors.shape[0];
// Reshape to height, width, num_anchors, box_params.
const anchorsTensor = tf.reshape(anchors, [1, 1, numAnchors, 2]);
const gridShape = feats.shape.slice(1, 3); // height, width
const gridY = tf.tile(tf.reshape(tf.range(0, gridShape[0]), [-1, 1, 1, 1]), [1, gridShape[1], 1, 1]);
const gridX = tf.tile(tf.reshape(tf.range(0, gridShape[1]), [1, -1, 1, 1]), [gridShape[0], 1, 1, 1]);
const grid = tf.concat([gridX, gridY], 3).cast(feats.dtype);
feats = feats.reshape([gridShape[0], gridShape[1], numAnchors, numClasses + 5]);
const [xy, wh, con, probs] = tf.split(feats, [2, 2, 1, numClasses], 3);
// Adjust preditions to each spatial grid point and anchor size.
const boxXy = tf.div(tf.add(tf.sigmoid(xy), grid), gridShape.reverse());
const boxWh = tf.div(tf.mul(tf.exp(wh), anchorsTensor), inputShape.reverse());
const boxConfidence = tf.sigmoid(con);
let boxClassProbs;
if (isV3) {
boxClassProbs = tf.sigmoid(probs);
} else {
boxClassProbs = tf.softmax(probs);
}
return [boxXy, boxWh, boxConfidence, boxClassProbs];
}
const actionIndexTensor = await tf.tidy(() => {
if (Array.isArray(x)) x = tf.tensor(x);
let predictions = tf.squeeze(this.predict(x.expandDims(0)));
if (this.isGreedy) {
return tf.argMax(predictions).arraySync()[0];
}
// Scale to [0, 1]
predictions = tf.div(tf.add(1, predictions), 2);
// Due to noise, can still be larger/smaller
const min = tf.min(predictions);
const max = tf.max(predictions);
predictions = tf.div(tf.sub(predictions, min), tf.sub(max, min));
// Now sample with the given temperature
const logProbs = tf.div(tf.log(predictions), temperature);
const expProbs = tf.exp(logProbs);
const sumExpProbs = tf.sum(expProbs);
const probs = tf.div(expProbs, sumExpProbs);
return tf.multinomial(probs, 1, null, true);
});
const actionIndex = (await actionIndexTensor.array())[0];
const onehot = onehotBuffer.toTensor();
let output;
if (this.model.embedding) {
const embedded = tf.matMul(onehot, this.model.embedding);
output = tf.multiRNNCell(this.cells, embedded, this.state.c, this.state.h);
} else {
output = tf.multiRNNCell(this.cells, onehot, this.state.c, this.state.h);
}
this.state.c = output[0];
this.state.h = output[1];
const outputH = this.state.h[1];
const weightedResult = tf.matMul(outputH, this.model.fullyConnectedWeights);
const logits = tf.add(weightedResult, this.model.fullyConnectedBiases);
const divided = tf.div(logits, tf.tensor(temperature));
const probabilities = tf.exp(divided);
probabilitiesNormalized = await tf.div(
probabilities,
tf.sum(probabilities),
).data();
if (i < userInput.length - 1) {
input = encodedInput[i + 1];
} else {
input = sampleFromDistribution(probabilitiesNormalized);
results.push(input);
}
}
let generated = '';
results.forEach((char) => {
const result = tf.tidy(() => {
return tf.div(tf.add(inputDeproc, tf.scalar(1)), tf.scalar(2));
});
inputDeproc.dispose();
function sampleLogits(
logits: tf.Tensor1D,
temperature?: number,
seed?: number) {
temperature = temperature !== undefined ? temperature : 1.;
if (temperature < 0. || temperature > 1.) {
throw new Error('Invalid temperature specified');
}
let result: tf.Scalar;
if (temperature === 0) {
result = tf.argMax(logits, 0) as tf.Scalar;
} else {
if (temperature < 1) {
logits = tf.div(logits, tf.scalar(temperature, 'float32'));
}
const scores = tf.reshape(tf.softmax(logits, 0), [1, -1]) as tf.Tensor2D;
const sample = tf.multinomial(scores, 1, seed, true) as tf.Tensor1D;
result = tf.reshape(sample, []) as tf.Scalar;
}
return result;
}
export function yolo_boxes_to_corners(box_xy, box_wh) {
const two = tf.tensor1d([2.0]);
const box_mins = tf.sub(box_xy, tf.div(box_wh, two));
const box_maxes = tf.add(box_xy, tf.div(box_wh, two));
const dim_0 = box_mins.shape[0];
const dim_1 = box_mins.shape[1];
const dim_2 = box_mins.shape[2];
const size = [dim_0, dim_1, dim_2, 1];
return tf.concat([
box_mins.slice([0, 0, 0, 1], size),
box_mins.slice([0, 0, 0, 0], size),
box_maxes.slice([0, 0, 0, 1], size),
box_maxes.slice([0, 0, 0, 0], size),
], 3);
}
export function yolo_boxes_to_corners(box_xy, box_wh) {
const two = tf.tensor1d([2.0]);
const box_mins = tf.sub(box_xy, tf.div(box_wh, two));
const box_maxes = tf.add(box_xy, tf.div(box_wh, two));
const dim_0 = box_mins.shape[0];
const dim_1 = box_mins.shape[1];
const dim_2 = box_mins.shape[2];
const size = [dim_0, dim_1, dim_2, 1];
return tf.concat([
box_mins.slice([0, 0, 0, 1], size),
box_mins.slice([0, 0, 0, 0], size),
box_maxes.slice([0, 0, 0, 1], size),
box_maxes.slice([0, 0, 0, 0], size),
], 3);
}