tensorflow / tfjs

A WebGL accelerated JavaScript library for training and deploying ML models.
https://js.tensorflow.org
Apache License 2.0
18.47k stars 1.93k forks source link

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

Closed Michael-F-Bryan closed 2 years ago

Michael-F-Bryan commented 3 years ago

System information

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.

rthadur commented 3 years ago

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

Michael-F-Bryan commented 3 years ago

In the meantime, @rthadur do you know if it is possible to add the @tensorflow/tfjs-tflite package directly as a git dependency?

jinjingforever commented 3 years ago

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!

has-n commented 2 years ago

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.

Michael-F-Bryan commented 2 years ago

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 commented 2 years ago

Hi,

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

Michael-F-Bryan commented 2 years ago

@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.

google-ml-butler[bot] commented 2 years ago

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.

google-ml-butler[bot] commented 2 years ago

Closing as stale. Please @mention us if this needs more attention.