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

Resizing Layer #6879

Merged
merged 13 commits into from Oct 11, 2022
Merged

Conversation

koyykdy
Copy link
Contributor

@koyykdy koyykdy commented Sep 29, 2022

The branch and commit histories have been cleaned up for the PR.

IMPORT NOTE:
The lower-level op implementation of image resizing-nearest neighbor in TensorFlow.js differs from the implementation of the comparable op in Keras-Python.

While the Python version of the op function always selects the bottom right cell of the sub-matrix to be used as the representative value of that region in the downscaled matrix, the JavaScript implementation defaults to the top left cell of the sub-matrix, and then preferentially shifts to the right side of the sub-matrix in all sub-matrices past the lateral halfway point ( calculated by floor((length-1)/2) ), and the bottom side of the sub-matrix in all sub-matrices past the vertical halfway point, when considering the top-left side of the parent matrix as the origin.

This causes a slight variation in the output values from nearest neighbor downscaling between the Python and JavaScript versions of the code as it currently stands, and the unit tests for the resizing layer has been implemented to reflect this difference in op-function behavior.

Co-authored-by: Adam Lang (@AdamLang96) adamglang96@gmail.com
Co-authored-by: Brian Zheng (@Brianzheng123) brianzheng345@gmail.com

To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.


This change is Reviewable

koyykdy and others added 3 commits September 29, 2022 11:10
* Update jasmine_util.ts (tensorflow#6872)

FIX

* webgl: Fix NaN issue (tensorflow#6828)

Fix tensorflow#6822

Problem
1: On some GPUs, even if a and b are both non-NaN, the value of isNaN in vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0)); are still larger than 0., which misleads all values become NAN.
2: After resolving NAN issue, the result is still incorrect. It seems that the isnan_custom is not well supported on the problem GPU. After switching back to builtin isnan, everything works well.

Solution:
Use the bool type bvec4 instead of float type vec4 to calculate isNaN to avoid the the float precision issue when comparing with zero.
Meanwhile, add an env flag WEBGL2_ISNAN_CUSTOM to allow user to specify which isnan to use.

* Upgrade nodejs to 18.7.0 (tensorflow#6863)

* Upgrade nodejs to 18.7.0

* Fix hash table test string not passed as base64

* fixed prelu fusing code that pre-maturely neg the const on multiply (tensorflow#6876)

Co-authored-by: RajeshT <43972606+rthadur@users.noreply.github.com>

Co-authored-by: Linchenn <40653845+Linchenn@users.noreply.github.com>
Co-authored-by: Jiajia Qin <jiajia.qin@intel.com>
Co-authored-by: Matthew Soulanille <msoulanille@google.com>
Co-authored-by: Ping Yu <4018+pyu10055@users.noreply.github.com>
Co-authored-by: RajeshT <43972606+rthadur@users.noreply.github.com>
* Implementation of preprocessing image resizing layer
Important Note:
The lower-level op implementation of image resizing-nearest neighbor in TensorFlow.js differs from the implementation of the comparable op in Keras-Python.

While the Python version of the op function always selects the bottom right cell of the sub-matrix to be used as the representative value of that region in the downscaled matrix, the JavaScript implementation defaults to the top left cell of the sub-matrix, and then preferentially shifts to the right side of the sub-matrix in all sub-matrices past the lateral halfway point ( calculated by floor((length-1)/2) ), and the bottom side of the sub-matrix in all sub-matrices past the vertical halfway point, when considering the top-left side of the parent matrix as the origin.

This causes a slight variation in the output values from nearest neighbor downscaling between the Python and JavaScript versions of the code as it currently stands, and the unit tests for the resizing layer has been implemented to reflect this difference in op-function behavior.

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>

* Updating image preprocessing test file to include approved usage of CodeSmith licensing header

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>
* Update jasmine_util.ts (tensorflow#6872)

FIX

* webgl: Fix NaN issue (tensorflow#6828)

Fix tensorflow#6822

Problem
1: On some GPUs, even if a and b are both non-NaN, the value of isNaN in vec4 isNaN = min(vec4(isnan(a)) + vec4(isnan(b)), vec4(1.0)); are still larger than 0., which misleads all values become NAN.
2: After resolving NAN issue, the result is still incorrect. It seems that the isnan_custom is not well supported on the problem GPU. After switching back to builtin isnan, everything works well.

Solution:
Use the bool type bvec4 instead of float type vec4 to calculate isNaN to avoid the the float precision issue when comparing with zero.
Meanwhile, add an env flag WEBGL2_ISNAN_CUSTOM to allow user to specify which isnan to use.

* Upgrade nodejs to 18.7.0 (tensorflow#6863)

* Upgrade nodejs to 18.7.0

* Fix hash table test string not passed as base64

* fixed prelu fusing code that pre-maturely neg the const on multiply (tensorflow#6876)

Co-authored-by: RajeshT <43972606+rthadur@users.noreply.github.com>

Co-authored-by: Linchenn <40653845+Linchenn@users.noreply.github.com>
Co-authored-by: Jiajia Qin <jiajia.qin@intel.com>
Co-authored-by: Matthew Soulanille <msoulanille@google.com>
Co-authored-by: Ping Yu <4018+pyu10055@users.noreply.github.com>
Co-authored-by: RajeshT <43972606+rthadur@users.noreply.github.com>
@koyykdy koyykdy changed the title Resizing Layer Implemented and Refactored Resizing Layer Sep 29, 2022
@koyykdy
Copy link
Contributor Author

koyykdy commented Sep 29, 2022

@ahmedsabie @mattsoulanille Suggestions have been implemented, requesting review. Thank you!

@mattsoulanille
Copy link
Member

This new PR has changes from packages other than tfjs-layers. Is that intentional?

We don't mind PRs having a messy commit history since we squash all commits into a single one when merging. Can we re-open your original PR #6858? This also helps me review your code since I can see changes since my last review.

If you want help with git history (e.g. getting just your changes in the PR), I can reset the resizing branch so it includes only files from tfjs-layers.

@koyykdy
Copy link
Contributor Author

koyykdy commented Sep 30, 2022

This new PR has changes from packages other than tfjs-layers. Is that intentional?

The only changes aside from the tfjs-layers/src/layers/preprocessing should be in tfjs-layers/src/exports-layers.ts for registering the new layer. Any other changes would be from pulling in the new commits made by third parties to the tfjs master repository late last evening and this morning, which made the base branch for this PR outdated.

We don't mind PRs having a messy commit history since we squash all commits into a single one when merging. Can we re-open your original PR #6858? This also helps me review your code since I can see changes since my last review.

If you want help with git history (e.g. getting just your changes in the PR), I can reset the resizing branch so it includes only files from tfjs-layers.

There was a typo made to one of the commits in that branch where the Co-authored-by commit field had incorrectly applied brackets to the github username of a co-author instead of their e-mail address, which was causing the automatic CI tool to fail during checking. I deleted the forks and branches and recreated this one in order to address that issue.

@koyykdy
Copy link
Contributor Author

koyykdy commented Sep 30, 2022

The only changes aside from the tfjs-layers/src/layers/preprocessing should be in tfjs-layers/src/exports-layers.ts for registering the new layer. Any other changes would be from pulling in the new commits made by third parties to the tfjs master repository late last evening and this morning, which made the base branch for this PR outdated.

These are the relevant commits in the pull request:
These commits are pulling in changes to the master branch to silence This branch is out-of-date with the base branch warning in GitHub:
CodeSmithDSMLProjects#41
CodeSmithDSMLProjects#43
Pull 43 is effectively the same as pull 41, I accidentally did it twice because doing it the first time did not make the This branch is out-of-date with the base branch warning go away.

This is the only commit containing the resizing layer changes:
CodeSmithDSMLProjects#42

Copy link
Member

@mattsoulanille mattsoulanille left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@mattsoulanille
Copy link
Member

@ahmedsabie Please take a look at this PR when you get a chance. Thanks!

* Implementation of preprocessing image resizing layer
Important Note:
The lower-level op implementation of image resizing-nearest neighbor in TensorFlow.js differs from the implementation of the comparable op in Keras-Python.

While the Python version of the op function always selects the bottom right cell of the sub-matrix to be used as the representative value of that region in the downscaled matrix, the JavaScript implementation defaults to the top left cell of the sub-matrix, and then preferentially shifts to the right side of the sub-matrix in all sub-matrices past the lateral halfway point ( calculated by floor((length-1)/2) ), and the bottom side of the sub-matrix in all sub-matrices past the vertical halfway point, when considering the top-left side of the parent matrix as the origin.

This causes a slight variation in the output values from nearest neighbor downscaling between the Python and JavaScript versions of the code as it currently stands, and the unit tests for the resizing layer has been implemented to reflect this difference in op-function behavior.

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>

* Updating image preprocessing test file to include approved usage of CodeSmith licensing header

* refactored interpolation type declaration

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>
@koyykdy
Copy link
Contributor Author

koyykdy commented Oct 6, 2022

@ahmedsabie Hello, the build is failing for an unknown reason, could you check it over if you can?

koyykdy and others added 3 commits October 6, 2022 14:29
* Implementation of preprocessing image resizing layer
Important Note:
The lower-level op implementation of image resizing-nearest neighbor in TensorFlow.js differs from the implementation of the comparable op in Keras-Python.

While the Python version of the op function always selects the bottom right cell of the sub-matrix to be used as the representative value of that region in the downscaled matrix, the JavaScript implementation defaults to the top left cell of the sub-matrix, and then preferentially shifts to the right side of the sub-matrix in all sub-matrices past the lateral halfway point ( calculated by floor((length-1)/2) ), and the bottom side of the sub-matrix in all sub-matrices past the vertical halfway point, when considering the top-left side of the parent matrix as the origin.

This causes a slight variation in the output values from nearest neighbor downscaling between the Python and JavaScript versions of the code as it currently stands, and the unit tests for the resizing layer has been implemented to reflect this difference in op-function behavior.

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>

* Updating image preprocessing test file to include approved usage of CodeSmith licensing header

* refactored interpolation type declaration

* minor spacing correction

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>
@mattsoulanille
Copy link
Member

@koyykdy Sorry about that build failure. We had changed some cloudbuild permissions on our CI runner, and it broke presubmits. It should be fixed now, though.

@koyykdy
Copy link
Contributor Author

koyykdy commented Oct 6, 2022

@mattsoulanille It seems to be failing still, could you please check what the output from the presubmit failure is? Thank you.

@mattsoulanille
Copy link
Member

Looks like it failed to compile:

tfjs-layers/src/exports_layers.ts:1803:17 - error TS6133: 'categoryEncoding' is declared but its value is never read.

1803 export function categoryEncoding(args: CategoryEncodingArgs) {
                     ~~~~~~~~~~~~~~~~

That's strange because usually exporting a value makes typescript not try to check if it's used.

@koyykdy
Copy link
Contributor Author

koyykdy commented Oct 7, 2022

Looks like it failed to compile:

tfjs-layers/src/exports_layers.ts:1803:17 - error TS6133: 'categoryEncoding' is declared but its value is never read.

1803 export function categoryEncoding(args: CategoryEncodingArgs) {
                     ~~~~~~~~~~~~~~~~

That's strange because usually exporting a value makes typescript not try to check if it's used.

This seems to be from the updates brought in by merging in from the master branch, could there be an issue with regards to this?

@koyykdy
Copy link
Contributor Author

koyykdy commented Oct 7, 2022

I attempted to update the branch, and resolved a merge conflict in exports_layers, but the rest of the changes from the main branch merge request might not have gone through.

@mattsoulanille
Copy link
Member

Fixed it. Missing a close curly brace.

@mattsoulanille
Copy link
Member

The build succeeds now, but the tests still have a compilation error:

tfjs-layers/src/layers/preprocessing/image_resizing_test.ts:113:31 - error TS2345: Argument of type '{ height: number; width: number; interpolation: string; }' is not assignable to parameter of type 'ResizingArgs'.
  Types of property 'interpolation' are incompatible.
    Type 'string' is not assignable to type 'InterpolationFormat'.

113     expect(() => new Resizing(incorrectArgs)).toThrowError(expectedError);
                                  ~~~~~~~~~~~~~

Co-authored-by: Matthew Soulanille <matthew@soulanille.net>
koyykdy and others added 2 commits October 7, 2022 13:16
…57)

* Implementation of preprocessing image resizing layer
Important Note:
The lower-level op implementation of image resizing-nearest neighbor in TensorFlow.js differs from the implementation of the comparable op in Keras-Python.

While the Python version of the op function always selects the bottom right cell of the sub-matrix to be used as the representative value of that region in the downscaled matrix, the JavaScript implementation defaults to the top left cell of the sub-matrix, and then preferentially shifts to the right side of the sub-matrix in all sub-matrices past the lateral halfway point ( calculated by floor((length-1)/2) ), and the bottom side of the sub-matrix in all sub-matrices past the vertical halfway point, when considering the top-left side of the parent matrix as the origin.

This causes a slight variation in the output values from nearest neighbor downscaling between the Python and JavaScript versions of the code as it currently stands, and the unit tests for the resizing layer has been implemented to reflect this difference in op-function behavior.

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>

* Updating image preprocessing test file to include approved usage of CodeSmith licensing header

* refactored interpolation type declaration

* minor spacing correction

* resizing layer test assertion statement argument casting refactored

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>
* Implementation of preprocessing image resizing layer
Important Note:
The lower-level op implementation of image resizing-nearest neighbor in TensorFlow.js differs from the implementation of the comparable op in Keras-Python.

While the Python version of the op function always selects the bottom right cell of the sub-matrix to be used as the representative value of that region in the downscaled matrix, the JavaScript implementation defaults to the top left cell of the sub-matrix, and then preferentially shifts to the right side of the sub-matrix in all sub-matrices past the lateral halfway point ( calculated by floor((length-1)/2) ), and the bottom side of the sub-matrix in all sub-matrices past the vertical halfway point, when considering the top-left side of the parent matrix as the origin.

This causes a slight variation in the output values from nearest neighbor downscaling between the Python and JavaScript versions of the code as it currently stands, and the unit tests for the resizing layer has been implemented to reflect this difference in op-function behavior.

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>

* Updating image preprocessing test file to include approved usage of CodeSmith licensing header

* refactored interpolation type declaration

* minor spacing correction

* resizing layer test assertion statement argument casting refactored

* image resizing layer unit test assertion line max length exceeded refactored

Co-authored-by: Adam Lang (@AdamLang96) <adamglang96@gmail.com>
Co-authored-by: Brian Zheng (@Brianzheng123) <brianzheng345@gmail.com>
@mattsoulanille
Copy link
Member

@ahmedsabie Please take another look when you get a chance. Thanks!

@mattsoulanille mattsoulanille merged commit 9752734 into tensorflow:master Oct 11, 2022
@mattsoulanille mattsoulanille deleted the resizing branch October 11, 2022 20:33
@koyykdy
Copy link
Contributor Author

koyykdy commented Dec 22, 2022

Hello, I do not currently show up under contributors to the tfjs repository on GitHub, is there a way to address this?

@mattsoulanille
Copy link
Member

I'm out of office at the moment, so I'll refer you to @Linchenn.

@Linchenn
Copy link
Collaborator

I think you are a contributor since you have merged a commit to our default branch. Probably you are not displayed in the contributor list. https://docs.github.com/en/repositories/viewing-activity-and-data-for-your-repository/viewing-a-projects-contributors @koyykdy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants