Published
Edited
Jun 14, 2019
5 forks
6 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@0.15.3')
Insert cell
Insert cell
model = await tf
.loadModel("https://epfl-exts.github.io/react-course-project/public/model/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
.fromPixels(canvas) // Shape: (300, 300, 3) - RGB image
.resizeNearestNeighbor([28, 28]) // Shape: (28, 28, 3) - RGB image
.mean(2) // Shape: (28, 28) - grayscale
.expandDims(2) // Shape: (28, 28, 1) - network expects 3d values with channels in the last dimension
.expandDims() // Shape: (1, 28, 28, 1) - network makes predictions for "batches" of images
.toFloat(); // Network works with floating points inputs
return tensor.div(255.0); // Normalize [0..255] values into [0..1] range
}
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