-
Notifications
You must be signed in to change notification settings - Fork 7.1k
extend support of posterize to all integer and floating dtypes #6847
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37f29d7
extend support of posterize to all integer and floating dtypes
pmeier 71a2042
remove raise
pmeier cc84f67
Merge branch 'main' into posterize-dtypes
pmeier 562028c
revert to fixed value range for integer dtypes
pmeier ac7fc7d
Merge branch 'main' into posterize-dtypes
pmeier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain the clamp at levels-1? This is the kind of implementation reference I had in mind. Which reference did you use? Also why are we multiplying by 2^bits instead of 2^bits-1 which is supposed to be the max for the specific type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I touched on this in 3. in my top comment. Since the input range for float images is inclusive on the edges, i.e.
[0.0, 1.0]
,image.mul(levels).floor_()
gives uslevels + 1
values, i.e.{0.0, 1.0, 2.0, ..., levels - 1.0, levels}
.However, we want the kernel to quantize to
levels
levels. Thus, we need to remove one level. For integer dtypes, the higher values are removed, i.e. the remaining values are{i * 2 ** (bit_depth - bits) for i in range(2 ** bits)}
. For exampleAs you can see the
255
/256
corresponding to1.0
in floating point images is missing. Thus, we also clamp that away for floating images.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read your PR description but I still had questions on this. Why not do
image.mul(levels-1)
to begin with? Multiplying bylevels
means that upper bound of1
will go outside of the permitted range of the type. What am I missing here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make this more concrete, let's look at an example:
The proposal in this is not perfect, but the
.byte()
call above eliminates some nuances.In contrast if we do what you propose we get
This is of course also a valid way to posterize an image to a bit depth of
3
, but the behavior is divergent from what we and PIL do for integers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation @pmeier. This makes sense. @vfdev-5 thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pmeier I think you can skip clamp with the following:
EDIT: for bits=1 above method gets something unexpected:
EDIT2: a better quantization formula skipping clamp
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, this does not work. While the individual level values look good
the posterized values do not match what we do for the integers:
This comes from the asymmetry of the multiplication and division and is also what you observed for
bits=1
above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re EDIT 2: formula still produces different values:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, using clamp you still have a difference of +/- 1 but OK, let's have clamp. Probably, it is not a big deal in terms of runtime perfs.