Published
Edited
Feb 25, 2020
2 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
tf = require('@tensorflow/tfjs@1.5.1')
Insert cell
Insert cell
model = await tf
.loadLayersModel("https://karlo-emilo.github.io/drawingClassifiers/model.json");
Insert cell
Insert cell
predictions = {
predictButton; // Wait for the predicton button to be clicked!
return await model.predict(preprocessCanvas(canvas)).data(); // this will run whenever "tensor" updates
}
Insert cell
Insert cell
preprocessCanvas = (canvas) => {
// Preprocess image for the network
let tensor = tf.browser.fromPixels(canvas); // Shape: (300, 300, 3) - RGB image
//tensor = await tensor.data().forEachAsync(greyScale => Math.abs(greyScale - 255))
console.log(tensor.data());
tensor = tf.image.resizeNearestNeighbor(tensor, [28, 28]) // Shape: (28, 28, 3) - RGB image
tensor = tensor.mean(2) // Shape: (28, 28) - grayscale
// tensor.print()
// tensor = tensor.flatten() // to 1D array
// tensor = tensor.reshape(784)
tensor = tf.reshape(tensor, [1, 28*28])
tensor = tensor.toFloat(); // Network works with floating points inputs
console.log('input:', tensor.data());
return tensor;
}
Insert cell
Insert cell
result = tf.argMax(predictions).data(); // this will run whenever "predictions" updates
Insert cell
Insert cell
bestGuess = labels[result]; // this will run whenever "result" updates
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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more