Published
Edited
Sep 21, 2018
15 forks
Importers
158 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
addExample = classId => {
/**
* Reads a frame from the webcam, feeds it through MobileNet, normalizes it
*/
tf.tidy(() => {
// Compute the logits vector from the current webcam frame.
const logits = getNormalizedLogitsFromWebcam();
// Reshape the logits so its a [1, 1000] matrix instead of a [1000] vector. This allows us to concatenate it
// to the dataset.
const logits2d = logits.expandDims(0);

if (dataset[classId] == null) {
// No matrix has been defined for the class yet, store a [1, 1000] matrix.
dataset[classId] = tf.keep(logits2d);
} else {
// Concatenate the logits with the matrix for the class, creating an [N_prev + 1, 1000] matrix.
const oldDataset = dataset[classId];
dataset[classId] = tf.keep(dataset[classId].concat(logits2d, 0));
}

// Observable hack to update the dataset.
const res = dataset;
mutable dataset = res;
});
}
Insert cell
predict = async () => {
const similarities = tf.tidy(() => {
// Get the logits from the webcam and reshape it to a matrix of [1, 1000].
const logits = getNormalizedLogitsFromWebcam().expandDims(1);
// Compte the matrix multiply of the dataset and the logits to compute similarities.
// This is a vector of shape [N].
return datasetConcatenated.matMul(logits);
});

const values = await similarities.data();
// Compute the top k indices and values in our similarities vector.
const top = topk(values, Math.min(totalExamples, K_demo));
// Compute the winner.
const topClass = calculateTopClass(top.indices, K_demo);
similarities.dispose();
return topClass;
}
Insert cell
topk = (values, k) => {
/**
* Given an unsorted array of values, compute the top k indices and values.
*/
const valuesAndIndices = [];
for (let i = 0; i < values.length; i++) {
valuesAndIndices.push({value: values[i], index: i});
}
valuesAndIndices.sort((a, b) => {
return b.value - a.value;
});
const topkValues = new Float32Array(k);
const topkIndices = new Int32Array(k);
for (let i = 0; i < k; i++) {
topkValues[i] = valuesAndIndices[i].value;
topkIndices[i] = valuesAndIndices[i].index;
}
return {values: topkValues, indices: topkIndices};
}
Insert cell
calculateTopClass = (topKIndices, kVal) => {
/**
* Computes the most likely class from a topk calculation.
*/
let imageClass = -1;
const confidences = {};

if (topKIndices == null) {
// No class predicted
return {classIndex: imageClass, confidences};
}

const indicesForClasses = [];
const topKCountsForClasses = [];
for (const i in dataset) {
topKCountsForClasses.push(0);
let num = classExampleCount[i];
if (+i > 0) {
num += indicesForClasses[+i - 1];
}
indicesForClasses.push(num);
}

for (let i = 0; i < topKIndices.length; i++) {
for (let classForEntry = 0; classForEntry < indicesForClasses.length;
classForEntry++) {
if (topKIndices[i] < indicesForClasses[classForEntry]) {
topKCountsForClasses[classForEntry]++;
break;
}
}
}

let topConfidence = 0;
for (const i in dataset) {
const probability = topKCountsForClasses[i] / kVal;
if (probability > topConfidence) {
topConfidence = probability;
imageClass = +i;
}
confidences[i] = probability;
}

return {classIndex: imageClass, confidences};
}
Insert cell
getNormalizedLogitsFromWebcam = () => {
return tf.tidy(() => {
// Make a prediction through mobilenet and flatten to a vector.
const result = mobilenet.infer(tf.fromPixels(webcam2), 'conv_preds').flatten();
// Normalize the result to unit length.
return result.div(result.norm());
});
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more