-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Fix BaseOutput initialization from dict #570
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 all commits
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import unittest | ||
| from dataclasses import dataclass | ||
| from typing import List, Union | ||
|
|
||
| import numpy as np | ||
|
|
||
| import PIL.Image | ||
| from diffusers.utils.outputs import BaseOutput | ||
|
|
||
|
|
||
| @dataclass | ||
| class CustomOutput(BaseOutput): | ||
| images: Union[List[PIL.Image.Image], np.ndarray] | ||
|
|
||
|
|
||
| class ConfigTester(unittest.TestCase): | ||
| def test_outputs_single_attribute(self): | ||
| outputs = CustomOutput(images=np.random.rand(1, 3, 4, 4)) | ||
|
|
||
| # check every way of getting the attribute | ||
| assert isinstance(outputs.images, np.ndarray) | ||
| assert outputs.images.shape == (1, 3, 4, 4) | ||
| assert isinstance(outputs["images"], np.ndarray) | ||
|
Member
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. The transformers version (initial commit from this PR) was failing on this line |
||
| assert outputs["images"].shape == (1, 3, 4, 4) | ||
| assert isinstance(outputs[0], np.ndarray) | ||
| assert outputs[0].shape == (1, 3, 4, 4) | ||
|
|
||
| # test with a non-tensor attribute | ||
| outputs = CustomOutput(images=[PIL.Image.new("RGB", (4, 4))]) | ||
|
|
||
| # check every way of getting the attribute | ||
| assert isinstance(outputs.images, list) | ||
|
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. Cool that this is all tested now! |
||
| assert isinstance(outputs.images[0], PIL.Image.Image) | ||
| assert isinstance(outputs["images"], list) | ||
| assert isinstance(outputs["images"][0], PIL.Image.Image) | ||
| assert isinstance(outputs[0], list) | ||
| assert isinstance(outputs[0][0], PIL.Image.Image) | ||
|
|
||
| def test_outputs_dict_init(self): | ||
| # test output reinitialization with a `dict` for compatibility with `accelerate` | ||
| outputs = CustomOutput({"images": np.random.rand(1, 3, 4, 4)}) | ||
|
|
||
| # check every way of getting the attribute | ||
| assert isinstance(outputs.images, np.ndarray) | ||
|
Member
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. the pre-PR diffusers version was failing on this line |
||
| assert outputs.images.shape == (1, 3, 4, 4) | ||
| assert isinstance(outputs["images"], np.ndarray) | ||
| assert outputs["images"].shape == (1, 3, 4, 4) | ||
| assert isinstance(outputs[0], np.ndarray) | ||
| assert outputs[0].shape == (1, 3, 4, 4) | ||
|
|
||
| # test with a non-tensor attribute | ||
| outputs = CustomOutput({"images": [PIL.Image.new("RGB", (4, 4))]}) | ||
|
|
||
| # check every way of getting the attribute | ||
| assert isinstance(outputs.images, list) | ||
| assert isinstance(outputs.images[0], PIL.Image.Image) | ||
| assert isinstance(outputs["images"], list) | ||
| assert isinstance(outputs["images"][0], PIL.Image.Image) | ||
| assert isinstance(outputs[0], list) | ||
| assert isinstance(outputs[0][0], PIL.Image.Image) | ||
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.
Very nice!