Skip to content

Instantly share code, notes, and snippets.

@vrxacs

vrxacs/tfModelLoading.js

Last active Oct 26, 2018
Embed
What would you like to do?
Loading a TensorFlow.js model and doing classification
var wait = ms => new Promise((r, j)=>setTimeout(r, ms));
async function main() {
const model = await tf.loadModel('./model/model.json');
document.getElementById('image_upload').onchange = function(ev) {
var f = ev.target.files[0];
var fr = new FileReader();
var makePrediction = async function(img) {
// We need to ensure that the image is actually loaded before we proceed.
while(!img.complete) {
await wait(100);
}
var tensor = tf.fromPixels(img)
.resizeNearestNeighbor([150,150])
.toFloat().expandDims();
const prediction = model.predict(tensor);
var data = prediction.dataSync();
document.getElementById('result').innerHTML =
data[0] == 0 ? "Now, that's a hotdog! :)" : "Not hotdog! :(";
}
var fileReadComplete = function(ev2) {
document.getElementById('image').src = ev2.target.result;
var img = new Image();
img.src = ev2.target.result;
makePrediction(img);
};
fr.onload = fileReadComplete;
fr.readAsDataURL(f);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.