Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function yoloCorrectBoxes(
boxXy,
boxWh,
imageShape
) {
let boxYx = tf.concat(tf.split(boxXy, 2, 3).reverse(), 3);
let boxHw = tf.concat(tf.split(boxWh, 2, 3).reverse(), 3);
// Scale boxes back to original image shape.
const boxMins = tf.mul(tf.sub(boxYx, tf.div(boxHw, 2)), imageShape);
const boxMaxes = tf.mul(tf.add(boxYx, tf.div(boxHw, 2)), imageShape);
const boxes = tf.concat([
...tf.split(boxMins, 2, 3),
...tf.split(boxMaxes, 2, 3)
], 3);
return boxes;
}
const feats: tf.Tensor1D = tf.tidy(() => {
// Add button input to decoder feats and translate to [-1, 1].
const buttonTensor = tf.tensor1d([this.button], 'float32');
const buttonScaled =
tf.sub(tf.mul(2., tf.div(buttonTensor, NUM_BUTTONS - 1)), 1);
return buttonScaled.as1D();
});
const all_boxes = yolo_boxes_to_corners(box_xy, box_wh);
let [boxes, scores, classes] = yolo_filter_boxes(
all_boxes, box_confidence, box_class_probs, filterBoxesThreshold);
// If all boxes have been filtered out
if (boxes == null) {
return null;
}
const width = tf.scalar(widthPx);
const height = tf.scalar(heightPx);
const image_dims = tf.stack([height, width, height, width]).reshape([1,4]);
boxes = tf.mul(boxes, image_dims);
return [boxes, scores, classes];
});
public fit(X: Type2DMatrix | Type1DMatrix = null, y: Type1DMatrix = null): void {
const xWrapped = ensure2DMatrix(X);
validateFitInputs(xWrapped, y);
this.initWeights(xWrapped);
const tensorX = tf.tensor2d(xWrapped);
const tensorY = tf.tensor1d(y);
for (let i = 0; i < this.numIterations; ++i) {
const predictions: tf.Tensor = tf.sigmoid(tensorX.dot(this.weights));
const gradient: tf.Tensor = tf.mul(tensorY.sub(predictions).dot(tensorX), -1);
this.weights = this.weights.sub(tf.mul(this.learningRate, gradient));
}
}
export function add_dummy_feature(
X: Type2DMatrix = null,
value: number = 1.0
): number[][] {
if (Array.isArray(X) && X.length === 0) {
throw new TypeError('X cannot be empty');
}
validateMatrix2D(X);
const tensorX = tf.tensor2d(X) as tf.Tensor;
const [nSamples] = tensorX.shape;
const ones = tf.ones([nSamples, 1]) as tf.Tensor;
const sValue = tf.scalar(value) as tf.Tensor;
const multipledOnes = tf.mul(ones, sValue);
const hStacked = tf.concat([multipledOnes, tensorX], 1);
return reshape(Array.from(hStacked.dataSync()), hStacked.shape) as number[][];
}
const res = sWeights.map((s, i) =>
tf.mul(s, tau).add(tf.mul(tWeights[i], 1-tau)));
layer.setWeights(res);
const frame = tf.tensor(resampled.slice(0, 1024));
const zeromean = tf.sub(frame, tf.mean(frame));
const framestd = tf.tensor(tf.norm(zeromean).dataSync() / Math.sqrt(1024));
const normalized = tf.div(zeromean, framestd);
const input = normalized.reshape([1, 1024]);
const activation = this.model.predict([input]).reshape([360]);
const confidence = activation.max().dataSync()[0];
const center = activation.argMax().dataSync()[0];
this.results.confidence = confidence.toFixed(3);
const start = Math.max(0, center - 4);
const end = Math.min(360, center + 5);
const weights = activation.slice([start], [end - start]);
const cents = centMapping.slice([start], [end - start]);
const products = tf.mul(weights, cents);
const productSum = products.dataSync().reduce((a, b) => a + b, 0);
const weightSum = weights.dataSync().reduce((a, b) => a + b, 0);
const predictedCent = productSum / weightSum;
const predictedHz = 10 * (2 ** (predictedCent / 1200.0));
const frequency = (confidence > 0.5) ? predictedHz : null;
this.frequency = frequency;
});
});
function yoloBoxesAndScores(
isV3,
feats,
anchors,
numClasses,
inputShape,
imageShape
) {
const [boxXy, boxWh, boxConfidence, boxClassProbs] = yoloHead(isV3, feats, anchors, numClasses, inputShape);
let boxes = yoloCorrectBoxes(boxXy, boxWh, imageShape);
boxes = boxes.reshape([-1, 4]);
let boxScores = tf.mul(boxConfidence, boxClassProbs);
boxScores = tf.reshape(boxScores, [-1, numClasses]);
return [boxes, boxScores];
}