Skip to content

docs: enhance wandb.Image docstrings for better normalization documentation#10380

Open
mdlinville wants to merge 19 commits intomainfrom
docs/DOCS-1016-image-normalization-docs
Open

docs: enhance wandb.Image docstrings for better normalization documentation#10380
mdlinville wants to merge 19 commits intomainfrom
docs/DOCS-1016-image-normalization-docs

Conversation

@mdlinville
Copy link
Copy Markdown
Contributor

@mdlinville mdlinville commented Aug 22, 2025

Summary

This PR addresses DOCS-1016 and the related GitHub issue wandb/wandb#7377 by enhancing the docstrings in the wandb.Image implementation to provide better auto-generated documentation for image normalization behavior.

This is part of a three-part project. The other two PRs are:

Changes Made

Enhanced Docstrings (wandb/sdk/data_types/image.py)

  1. _guess_and_rescale_to_0_255 function:

    • Added comprehensive docstring explaining the normalization algorithm
    • Detailed explanation of supported input ranges: [0,1], [-1,1], and other ranges
    • Clear documentation of arguments, return values, and behavior
    • Examples of when normalization is applied vs avoided
  2. Image.__init__ method:

    • Added "Normalization behavior" section to the docstring
    • Detailed explanation of when normalization occurs (PyTorch tensors, NumPy arrays) vs when it doesn't (PIL Images, file paths)
    • Examples demonstrating normalization effects for different input ranges
    • Instructions on how to avoid normalization using PIL Images or the normalize parameter

Problem Solved

The original issue was that users found the existing documentation about image normalization to be "cryptic" and unclear. Users were wasting computation time due to misunderstandings about:

  • When normalization occurs
  • How the normalization algorithm works
  • What input ranges are supported
  • How to avoid unwanted normalization

These enhanced docstrings will improve the auto-generated API documentation and provide clearer guidance to users.

Related PRs

JIRA Reference

  • DOCS-1016: "Productboard: [Docs] Better explanation for the section: when logging a PyTorch tensor as a PIL Image"

- Add detailed docstring for _guess_and_rescale_to_0_255 function explaining normalization algorithm
- Enhance Image.__init__ docstring with normalization behavior details
- Add examples showing different normalization scenarios
- Clarify when normalization is applied vs when it's not
- Provide guidance on how to avoid unwanted normalization

Addresses DOCS-1016
@mdlinville
Copy link
Copy Markdown
Contributor Author

Update: Companion PR in wandb/docs repository: wandb/docs#1559

@mdlinville mdlinville requested a review from ngrayluna August 22, 2025 19:07
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mdlinville mdlinville marked this pull request as draft August 22, 2025 19:18
@mdlinville mdlinville marked this pull request as ready for review August 22, 2025 19:32
@@ -59,17 +59,34 @@ def _warn_on_invalid_data_range(
def _guess_and_rescale_to_0_255(data: "np.ndarray") -> "np.ndarray":
Copy link
Copy Markdown
Contributor

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 normalize parameter to the wandb.Image constructor.

Copy link
Copy Markdown
Contributor Author

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:

  1. I am not sure why the _guess_and_rescale_to_0_255 function is showing up. @ngrayluna can you take a look? Is there some magic that prevents things starting with an underscore from showing up? Though, I think that this function doesn't show up in the docs, so maybe this is fine. https://docs.wandb.ai/ref/python/sdk/data-types/image/ is the currently published reference, without the changes in this PR.
  2. Preferring more verbose: Is this about lines 66-72 vs lines 60-64 ? Would you like me to revert the change here?

Comment thread wandb/sdk/data_types/image.py Outdated
tensor_0_1 = torch.rand(3, 64, 64) # Random values between 0 and 1
image1 = wandb.Image(tensor_0_1, caption="Normalized from [0,1] range")

# Example 2: [-1, 1] range tensor - values will be rescaled
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not necessarily. If the values all happen to be nonnegative, this will be rescaled using the [0, 1] rules.

Comment on lines 186 to 188
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.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this still be here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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?

@mdlinville mdlinville requested a review from timoffex August 25, 2025 21:22
@mdlinville
Copy link
Copy Markdown
Contributor Author

@timoffex PTAL, I think that your feedback in this PR is addressed, but let me know if there is more to do. Note that there is a related Docs PR and a related Examples PR to add a demo notebook.

@dmitryduev
Copy link
Copy Markdown
Member

@mdlinville is this still relevant or we can close?

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.

3 participants