Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tf-core] Cannot pass ImageBitmap to model in worker #5743

Closed
AlexShafir opened this issue Oct 19, 2021 · 8 comments · Fixed by #5752 or #5773
Closed

[tf-core] Cannot pass ImageBitmap to model in worker #5743

AlexShafir opened this issue Oct 19, 2021 · 8 comments · Fixed by #5752 or #5773
Assignees
Labels
type:bug Something isn't working

Comments

@AlexShafir
Copy link
Contributor

AlexShafir commented Oct 19, 2021

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 7
  • TensorFlow.js installed from (npm or script link): https://www.jsdelivr.com/package/npm/@tensorflow/tfjs-core
  • TensorFlow.js version (use command below): 3.9.0
  • Browser version: Chrome 94.0.4606.81
  • Tensorflow.js Converter Version: 3.9.0

Model: face-landmarks-detection v. 0.0.4, with wasm-only backend.

Preconditions

  1. Put all face-landmarks-detection code into web-worker
  2. Pass current ImageBitmap

Describe the current behavior
ReferenceError: document is not defined.

Describe the expected behavior
ImageBitmap gets processed.

Standalone code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate
the problem. If possible, please share a link to Colab/CodePen/any notebook.
https://github.com/AlexShafir/Sensoria/blob/tfjs5743
Message passed here, received here

Other info / logs Include any logs or source code that would be helpful to
diagnose the problem. If including tracebacks, please include the full
traceback. Large logs and files should be attached.

Uncaught (in promise) ReferenceError: document is not defined
    at fromPixels_ (:8887/fld/tf-core.js:8911)
    at Object.fromPixels__op [as fromPixels] (:8887/fld/tf-core.js:5406)
    at :8887/fld/face-landmarks-detection.js:1350
    at :8887/fld/tf-core.js:4394
    at Engine.scopedRun (:8887/fld/tf-core.js:4404)
    at Engine.tidy (:8887/fld/tf-core.js:4393)
    at Object.tidy (:8887/fld/tf-core.js:10072)
    at FaceMesh.<anonymous> (:8887/fld/face-landmarks-detection.js:1348)
    at step (:8887/fld/face-landmarks-detection.js:81)
    at Object.next (:8887/fld/face-landmarks-detection.js:62)

Probably related to #4218

@AlexShafir AlexShafir added the type:bug Something isn't working label Oct 19, 2021
@AlexShafir AlexShafir changed the title Cannot pass ImageBitmap to face-landmarks-detection in worker [face-landmarks-detection] Cannot pass ImageBitmap to model in worker Oct 19, 2021
@vladmandic
Copy link
Contributor

vladmandic commented Oct 19, 2021

This is not a bug in TFJS, this is how web workers work in general - there is no DOM in a worker, so you cannot rely on any DOM objects. TFJS could print more informative error message, but root cause is the same.

What you can do is pass pixel data (as value or actually transfer ownership of an array to worker) such as ImageData.data (which is just an array so that can be used by web worker) that you've already prepared in the main thread.

@AlexShafir
Copy link
Contributor Author

AlexShafir commented Oct 19, 2021

@vladmandic

  • Sure, web workers do not have access to document.
  • Thanks for tip with ImageData.data! I ended up using ImageData.data.buffer as ArrayBuffer is transferable.

@vladmandic
Copy link
Contributor

@AlexShafir yup, thats the best approach for web workers...
and inside web worker you can either reconstruct ImageData and then use fromPixels or just create tensor directly (but then you need to slice off alpha channel manually since ImageData is in RGBA format)

@rthadur
Copy link
Contributor

rthadur commented Oct 19, 2021

Thank you @vladmandic @AlexShafir closing this issue as this is not a bug and more support issue. Feel free to @mention to reopen.

@rthadur rthadur closed this as completed Oct 19, 2021
@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@AlexShafir
Copy link
Contributor Author

AlexShafir commented Oct 19, 2021

@rthadur if API says that one can enter ImageBitmap, you pass it (in worker context) and it crashes, I expect this behavior is called a bug?
In any case, what is the process for this kind of problems?

@rthadur rthadur reopened this Oct 19, 2021
@AlexShafir AlexShafir changed the title [face-landmarks-detection] Cannot pass ImageBitmap to model in worker Cannot pass ImageBitmap to model in worker Oct 20, 2021
@AlexShafir AlexShafir changed the title Cannot pass ImageBitmap to model in worker [tf-core] Cannot pass ImageBitmap to model in worker Oct 20, 2021
pyu10055 pushed a commit that referenced this issue Oct 26, 2021
FEATURE
* Allow passing ImageBitmap in worker context

* Fix TS type check

* Update browser.ts

* Add check for OffscreenCanvasRenderingContext2D

* Add test for passing ImageBitmap

* Strenghten check

* Simplify check

* Fix test

* Remove test

* Add test

* Improve test description

* Make test worker-centric

* Improve preconditions, add canvas mock

* Add backend import

* Relax type check for the mock

* Fix code style

* Fix typo

* Revert mock addition
@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

pyu10055 added a commit that referenced this issue Oct 28, 2021
* Allow passing ImageBitmap in worker context

* Fix TS type check

* Update browser.ts

* Add check for OffscreenCanvasRenderingContext2D

* Add test for passing ImageBitmap

* Strenghten check

* Simplify check

* Fix test

* Remove test

* Add test

* Improve test description

* Make test worker-centric

* Improve preconditions, add canvas mock

* Add backend import

* Relax type check for the mock

* Fix code style

* Fix typo

* Revert mock addition

* Fix test

* Fix test

* Relax type check

* Try error for sanity

* Fix typo

* Undo sanity error

* Fix test

* Limit line lengths

* Refactoring

* Add worker termination

* Fix test

* Shorten lines

* Exclude webgl test

* Fix typo

* Fix typo

Co-authored-by: Ping Yu <4018+pyu10055@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
4 participants