question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to use the @tensorflow/tfjs-tflite package outside of a web browser

See original GitHub issue

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/tfjs-tflite@0.0.1-alpha.4
  • 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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

6reactions
jinjingforevercommented, Aug 26, 2021

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!

2reactions
jinjingforevercommented, Apr 14, 2022

Hi,

Please try the tfjs-tflite-node package that @mattsoulanille has been working on. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Tensorflowjs conversion error from tflite-model-maker ...
It seems some of the ops your TFLite model is using are not supported in TensorFlow.js. The only way to get around this...
Read more >
TFLite gives Blob is not defined error when it is required in Node
1 Answer 1 · If you really want to use TF primarily in Node, you are using the wrong package. · If you...
Read more >
@tensorflow/tfjs-tflite - npm
Start using @tensorflow/tfjs-tflite in your project by running `npm i ... TypeScript icon, indicating that this package has built-in type ...
Read more >
Build a machine learning node for Node-RED using ...
Learn how to combine TensorFlow.js with Node-RED to create AI-enabled Node-RED applications in various environments.
Read more >
@tensorflow/tfjs-converter - npm package | Snyk
A python pip package to convert a TensorFlow SavedModel or TensorFlow Hub module to a web friendly format. If you already have a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found