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

[core, wasm] Modularize FromPixels and make it work for the wasm backend. #2429

Merged
merged 12 commits into from
Nov 25, 2019

Conversation

dsmilkov
Copy link
Contributor

@dsmilkov dsmilkov commented Nov 22, 2019

Remove backend.fromPixels(). The user-facing tf.browser.fromPixels() will call the current backend's FromPixels kernel IF it is registered. Else it will fallback to a platform-agnostic and backend-agnostic logic in a centralized place.

The cpu, wasm and node backends do not need to have FromPixels kernel registered since the centralized logic in tf.browser.fromPixels() will work.

The desired outcome of this is that now tf.browser.fromPixels() just works with the WASM backend without any additional code.

FEATURE


This change is Reviewable

Copy link
Contributor

@annxingyuan annxingyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 1 approvals obtained (waiting on @annxingyuan and @dsmilkov)


tfjs-core/src/ops/browser.ts, line 64 at r2 (raw file):

  }
  const isPixelData = (pixels as PixelData).data instanceof Uint8Array;
  const isImageData =

Should we wrap these checks in if / else statements so we can stop checking once we find a type match?


tfjs-core/src/ops/browser.ts, line 68 at r2 (raw file):

  const isVideo = typeof (HTMLVideoElement) !== 'undefined' &&
      pixels instanceof HTMLVideoElement;
  const isImage = typeof (HTMLImageElement) !== 'undefined' &&

Seems like we prefer the "HTMLImageElement != null" style elsewhere, as opposed to checking for typeof? Or maybe that doesn't work in node?


tfjs-core/src/ops/browser.ts, line 95 at r2 (raw file):

  const kernel = getKernel('FromPixels', ENGINE.backendName);
  if (kernel != null) {
    return ENGINE.runKernel('FromPixels', {pixels} as {}, {numChannels}) as

Does it make sense to have the fromPixels kernel take pixel data directly, so the pixel type checking and the rendering --> pixels processing can live just in this file?


tfjs-core/src/ops/browser.ts, line 136 at r2 (raw file):

  }
  const outShape: [number, number, number] = [height, width, numChannels];
  return tensor3d(values, outShape, 'int32');

Awesome cleanup!

Copy link
Contributor Author

@dsmilkov dsmilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 1 approvals obtained (waiting on @annxingyuan and @dsmilkov)


tfjs-core/src/ops/browser.ts, line 68 at r2 (raw file):

Previously, annxingyuan (Ann Yuan) wrote…

Seems like we prefer the "HTMLImageElement != null" style elsewhere, as opposed to checking for typeof? Or maybe that doesn't work in node?

can't do that since it will throw Uncaught ReferenceError: HTMLImageElement is not defined`. typeof() is the way to test if things are defined.


tfjs-core/src/ops/browser.ts, line 95 at r2 (raw file):

Previously, annxingyuan (Ann Yuan) wrote…

Does it make sense to have the fromPixels kernel take pixel data directly, so the pixel type checking and the rendering --> pixels processing can live just in this file?

WebGL backend wants the original pixels object, since it does different things to it depending on the type, so it's best to give the backend the original arguments.


tfjs-core/src/ops/browser.ts, line 136 at r2 (raw file):

Previously, annxingyuan (Ann Yuan) wrote…

Awesome cleanup!

Thanks!

Copy link
Contributor Author

@dsmilkov dsmilkov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! 1 of 1 approvals obtained (waiting on @annxingyuan)


tfjs-core/src/ops/browser.ts, line 64 at r2 (raw file):

Previously, annxingyuan (Ann Yuan) wrote…

Should we wrap these checks in if / else statements so we can stop checking once we find a type match?

Good idea. Done.

@dsmilkov dsmilkov requested a review from tafsiri November 25, 2019 14:22
Copy link
Contributor

@tafsiri tafsiri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 6 of 11 files at r1, 4 of 7 files at r2, 1 of 1 files at r3.
Reviewable status: :shipit: complete! 2 of 1 approvals obtained (waiting on @annxingyuan and @tafsiri)

@dsmilkov dsmilkov merged commit 025dc22 into master Nov 25, 2019
@dsmilkov dsmilkov deleted the from-pixels-wasm branch November 25, 2019 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants