Unable to use the @tensorflow/tfjs-tflite package outside of a web browser
System information
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Arch Linux
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
- TensorFlow.js installed from (npm or script link):
yarn add @tensorflow/[email protected] - TensorFlow.js version: N/A
- CUDA/cuDNN version: N/A
Describe the problem
The @tensorflow/tfjs-tflite package can't be used outside of a web browser due to its use of browser-specific APIs.
Provide the exact sequence of commands / steps that you executed before running into the problem
Create an empty NPM package and populate it with the following:
$ mkdir tensorflow-lite-repro && cd tensorflow-lite-repro
$ yarn init
$ cat package.json
{
"name": "tensorflow-lite-repro",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"type": "module",
"dependencies": {
"@tensorflow/tfjs-backend-cpu": "^3.8.0",
"@tensorflow/tfjs-core": "^3.8.0",
"@tensorflow/tfjs-tflite": "^0.0.1-alpha.4"
}
}
$ cat index.js
// Adds the CPU backend.
import '@tensorflow/tfjs-backend-cpu';
// Import @tensorflow/tfjs-core
import * as tf from '@tensorflow/tfjs-core';
// Import @tensorflow/tfjs-tflite.
import {loadTFLiteModel, TFLiteModel} from '@tensorflow/tfjs-tflite';
Next try to execute index.js using Node.
$ node --version
v16.7.0
$ yarn --version
1.22.5
$ yarn install
(snip)
$ node index.js
/tmp/tensorflow-lite-repro/node_modules/@tensorflow/tfjs-tflite/dist/tf-tflite.node.js:1261
module$exports$google3$third_party$tensorflow_lite_support$web$task$codegen$common$emscripten_module_loader.EmscriptenModuleLoader.getInstance=function(a,b,c){var d=a+b;c=URL.createObjectURL(new Blob([c],{type:"application/javascript"}));module$exports$google3$third_party$tensorflow_lite_support$web$task$codegen$common$emscripten_module_loader.EmscriptenModuleLoader.instances.has(d)||(a=new module$exports$google3$third_party$tensorflow_lite_support$web$task$codegen$common$emscripten_module_loader.EmscriptenModuleLoader(a,
^
ReferenceError: Blob is not defined
at Function.module$exports$google3$third_party$tensorflow_lite_support$web$task$codegen$common$emscripten_module_loader.EmscriptenModuleLoader.getInstance (/tmp/tensorflow-lite-repro/node_modules/@tensorflow/tfjs-tflite/dist/tf-tflite.node.js:1261:196)
at Object.<anonymous> (/tmp/tensorflow-lite-repro/node_modules/@tensorflow/tfjs-tflite/dist/tf-tflite.node.js:1331:447)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:196:29)
at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
You can monkey-patch Blob (e.g. by adding window.Blob = require("blob-polyfill") before the @tensorflow/tfjs-tflite import), but it then fails because Window.self doesn't exist.
There is an older version on npm which is 3 months old , we have a bunch of similar issues where users are using older version via npm , requesting @jinjingforever to push newer version to npm. Thank you
In the meantime, @rthadur do you know if it is possible to add the @tensorflow/tfjs-tflite package directly as a git dependency?
Thank you @Michael-F-Bryan for the report. Your are right. We actually haven't worked on nodejs support for tfjs-tflite yet (hence the alpha version). I will take a look at this soon (already in my todo list).
Thanks!
Can I add a +1 to @Michael-F-Bryan's requirement of using tf-lite in a non-browser environment? We're trying to use it in Node.js.
Note that "non-browser environment" is more than just trying to use @tensorflow/tfjs-tflite on your backend.
It also means nobody can run tests if the code under test transitively imports @tensorflow/tfjs-tflite because you'll get a bunch of reference errors when the test runner tries to start.
@jinjingforever can you reopen this ticket? Having to install a separate dependency doesn't really solve the problem of testing code that may transitively depend on @tensorflow/tfjs-tflite.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you.
Closing as stale. Please @mention us if this needs more attention.