-
Notifications
You must be signed in to change notification settings - Fork 863
docs: enhance wandb.Image docstrings for better normalization documentation #10380
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
base: main
Are you sure you want to change the base?
Changes from all commits
2a5ffb5
f62e865
3e2fb86
5b3431f
fd0b4e1
f65e93b
94b946d
ccb43f1
d5dd9b9
bde72fa
99b399a
db648dc
adc9ca9
5051174
37207c6
615a78c
b7e30ff
10dd4b7
4d7290b
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 |
|---|---|---|
|
|
@@ -57,19 +57,11 @@ def _warn_on_invalid_data_range( | |
|
|
||
|
|
||
| def _guess_and_rescale_to_0_255(data: "np.ndarray") -> "np.ndarray": | ||
| """Guess the image's format and rescale its values to the range [0, 255]. | ||
| """Rescale image data to [0, 255] range based on detected format. | ||
|
|
||
| This is an unfortunate design flaw carried forward for backward | ||
| compatibility. A better design would have been to document the expected | ||
| data format and not mangle the data provided by the user. | ||
|
|
||
| If given data in the range [0, 1], we multiply all values by 255 | ||
| and round down to get integers. | ||
|
|
||
| If given data in the range [-1, 1], we rescale it by mapping -1 to 0 and | ||
| 1 to 255, then round down to get integers. | ||
|
|
||
| We clip and round all other data. | ||
| If data is in [0, 1] range, multiply by 255. | ||
| If data is in [-1, 1] range, rescale to [0, 255]. | ||
| Otherwise, clip to [0, 255]. | ||
| """ | ||
| try: | ||
| import numpy as np | ||
|
|
@@ -169,8 +161,10 @@ def __init__( | |
| If the values are not in the range [0, 255] or all values are in the range [0, 1], | ||
| the image pixel values will be normalized to the range [0, 255] | ||
| unless `normalize` is set to False. | ||
|
Comment on lines
161
to
163
Contributor
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. Should this still be here?
Contributor
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. I'm not sure what you are specifically referring to. It looks to me like this is missing the -1-1 range case. Do you mean that since we have lines 62-64, the detail in 161-3 should be removed? |
||
| - pytorch tensor should be in the format (channel, height, width) | ||
| - NumPy array should be in the format (height, width, channel) | ||
|
|
||
| **Data format requirements:** | ||
| - PyTorch tensor should be in the format (channel, height, width) | ||
| - NumPy array should be in the format (height, width, channel) | ||
| mode: The PIL mode for an image. Most common are "L", "RGB", | ||
| "RGBA". Full explanation at https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes | ||
| caption: Label for display of image. | ||
|
|
@@ -237,6 +231,24 @@ def __init__( | |
| examples.append(image) | ||
| run.log({"examples": examples}) | ||
| ``` | ||
|
|
||
| Normalization example | ||
| ```python | ||
| import torch | ||
| import wandb | ||
|
|
||
| # Values in [0, 1] range will be multiplied by 255 | ||
| tensor_0_1 = torch.rand(3, 64, 64) # Values in [0, 1] | ||
| image = wandb.Image(tensor_0_1, caption="Normalized from [0,1] range") | ||
|
|
||
| # Values in [-1, 1] range will be rescaled to [0, 255] | ||
| tensor_neg1_1 = torch.rand(3, 64, 64) * 2 - 1 # Values in [-1, 1] | ||
| image = wandb.Image(tensor_neg1_1, caption="Normalized from [-1,1] range") | ||
|
|
||
| # Disable normalization | ||
| image = wandb.Image(tensor_0_1, normalize=False, caption="No normalization") | ||
| ``` | ||
|
|
||
| """ | ||
| super().__init__(caption=caption) | ||
| # TODO: We should remove grouping, it's a terrible name and I don't | ||
|
|
||
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.
This function is private and shouldn't appear in our documentation; why update its docstring here? I prefer the original for conciseness and developer context. The new version is too verbose and adds irrelevant context about PyTorch, PIL and the
normalizeparameter to thewandb.Imageconstructor.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'm a little confused about this feedback: