Skip to content

Instantly share code, notes, and snippets.

@vabarbosa
vabarbosa / load-model-run-prediction.js
Last active January 15, 2020 14:54
load tfjs model and run prediction
// load a graph model from a URL
const model = await tf.loadGraphModel(MODEL_URL)
// create a tensor from an image
const inputTensor = tf.browser.fromPixels(imageNode)
// run prediction
const prediction = model.predict(inputTensor)
// process output/prediction
// create a tensor from an image - tensorflow.js 1.0.0
// https://js.tensorflow.org/api/1.0.0/#browser.fromPixels
const imageTensor = tf.browser.fromPixels(imageElement)
// insert a dimension into the tensor's shape
const preprocessedInput = imageTensor.expandDims()
// tensorflow.js 1.0.0
const MODEL_URL = '/model/model.json'
// https://js.tensorflow.org/api/1.0.0/#loadGraphModel
const model = await tf.loadGraphModel(MODEL_URL)
let midiOutputs
let midiInputs
// check MIDI support/availability
if (navigator.requestMIDIAccess) {
navigator.requestMIDIAccess().then(function (access) {
// get output devices
midiOutputs = Array.from(access.outputs.values())
// get input devices
// instantiate audio context
let audioCtx = new (window.AudioContext || window.webkitAudioContext)()
// create gain node (volume)
let gainNode = audioCtx.createGain()
// create oscillator
let oscillator = audioCtx.createOscillator()
// connect audio nodes
const colorMapUrl = '/assets/color-map.json'
// load the color-map file
const response = await fetch(colorMapUrl)
const colorMap = await response.json()
// get image width, height from output
const imageWidth = prediction.shape[2]
const imageHeight = prediction.shape[1]
// generates output prediction
const prediction = model.predict(preprocessedInput)
// create a tensor from an image - tensorflow.js 0.15.1
// https://js.tensorflow.org/api/0.15.1/#fromPixels
const imageTensor = tf.fromPixels(imageElement)
// insert a dimension into the tensor's shape
const preprocessedInput = imageTensor.expandDims()
<!-- load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
from tensorflow.python.tools import strip_unused_lib
from tensorflow.python.framework import dtypes
import tensorflow as tf
# set the appropriate input and output nodes
input_node_names = ['decode/DecodeJpeg']
output_node_names = ['softmax']
# set the appropriate path to the frozen graph and directory to output stripped graph
frozen_graph_path = '/Users/va/models/frozen_graph.pb'