-
Notifications
You must be signed in to change notification settings - Fork 0
Fix JPEG 2000 float->int quantization #26
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
Changes from 2 commits
becec7f
6fe1c0c
85e67ea
2373a7a
28a10e6
44653bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,29 +16,44 @@ class Jpeg2000(Compressor): | |
| description = "JPEG 2000" | ||
|
|
||
| @staticmethod | ||
| def abs_bound_codec(dtype, error_bound): | ||
| # Currently, the input is transformed into the range | ||
| # round(min_pixel_val/ error_bound) <= x <= round(max_pixel_val / error_bound) | ||
| # This means any values outside this range will incur a larger error. | ||
| precision = error_bound | ||
| def abs_bound_codec( | ||
| error_bound, | ||
| *, | ||
| data_abs_min=None, | ||
| data_abs_max=None, | ||
| **kwargs, | ||
| ): | ||
| assert data_abs_min is not None, "data_abs_min must be provided" | ||
| assert data_abs_max is not None, "data_abs_max must be provided" | ||
|
|
||
| max_pixel_val = 2**25 - 1 # maximum pixel value for our integer encoding. | ||
|
|
||
| data_range = data_abs_max - data_abs_min | ||
|
|
||
| # Here we use the formula for the PSNR (https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio) | ||
| # to convert between the absolute error and the PSNR value. | ||
| # The original PSNR formula uses the root mean square error (RMSE), | ||
| # therefore JPEG does not guaruantee pointwise error bounds but only | ||
| # average error bounds. | ||
| psnr = 20 * (math.log10(max_pixel_val) - math.log10(error_bound)) | ||
| psnr = 20 * (math.log10(data_range) - math.log10(error_bound)) | ||
|
|
||
| return CodecStack( | ||
| # increase precision for better rounding during linear quantization | ||
| numcodecs.astype.AsType( | ||
| encode_dtype="float64", | ||
| decode_dtype="float32", | ||
| ), | ||
| # remap from [min, max] to [0, max_pixel_val] | ||
| numcodecs_wasm_fixed_offset_scale.FixedOffsetScale( | ||
| offset=0, | ||
| scale=precision, | ||
| offset=data_abs_min, | ||
| scale=data_range / max_pixel_val, | ||
| ), | ||
| # round and truncate to integer values | ||
| numcodecs_wasm_round.Round(precision=1), | ||
| numcodecs.astype.AsType( | ||
| encode_dtype="int32", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's the problem, you're right. We're going to signed int32, which supports [-2^24, 2^24 - 1]. If you change this to uint32 (since we now scale to [0, ...]), the max pixel value of 2^25 - 1 should work.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checked it, you're right! I know switched back to max pixel val 2^25 - 1 but using uint32 because it should give more range!
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for finding what our issue was! |
||
| decode_dtype="float32", | ||
| decode_dtype="float64", | ||
| ), | ||
| # apply the PSNR error bound | ||
| numcodecs_wasm_jpeg2000.Jpeg2000(mode="psnr", psnr=psnr), | ||
| ) | ||
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.
The JPEG codec needs the actual minimum and maximum, not the minimum absolute value.
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.
Ah sorry, yes that's a bug. It won't be the cause of the issue though because for CAMS all the values are positive.
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'll fix this though