Package Exports
- @tensorflow/tfjs-backend-wasm
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@tensorflow/tfjs-backend-wasm) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Usage
This package adds a WebAssembly backend to TensorFlow.js. This is currently in alpha and subject to change. Not every op in TensorFlow.js is supported on this backend.
Importing this package augments the TensorFlow.js package (@tensorflow/tfjs-core) by registering a new backend meaning existing TensorFlow.js code, models, and dependent packages will work with only a few lines of code changed.
Importing the backend
Via NPM
// Import @tensorflow/tfjs or @tensorflow/tfjs-core
import * as tf from '@tensorflow/tfjs';
// Import the WASM backend.
import '@tensorflow/tfjs-backend-wasm';
Via a script tag
<!-- Import @tensorflow/tfjs or @tensorflow/tfjs-core -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<!-- Import the WASM backend. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm"></script>
Using the backend with MobileNet
async function main() {
// Set the backend to WASM and wait for the module to be ready.
await tf.setBackend('wasm');
let img = tf.browser.fromPixels(document.getElementById('img'))
.resizeBilinear([224, 224])
.expandDims(0)
.toFloat();
let model = await tf.loadGraphModel(
'https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/2',
{fromTFHub: true});
const y = model.predict(img);
y.print();
}
main();
Development
Emscripten installation
Install the Emscripten SDK (version 1.39.1):
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install 1.39.1
./emsdk activate 1.39.1
Prepare the environment
Before developing, make sure the environment variable EMSDK
points to the
emscripten directory (e.g. ~/emsdk
). Emscripten provides a script that does
the setup for you:
Cd into the emsdk directory and run:
source ./emsdk_env.sh
For details, see instructions here.
Building
yarn build
Testing
yarn test
Deployment
./scripts/build-npm.sh
npm publish