-
Notifications
You must be signed in to change notification settings - Fork 39
Image support #166
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
Image support #166
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
52f99ec
Added image_pil and image_numpy
daavoo 98803a2
Use DATA_TYPES list in metrics
daavoo 4cf1df5
Use subdir structure
daavoo 60f944d
Use data subdirs in init_path
daavoo 062c8a2
Fix test_logging
daavoo 51f67f2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ac22e98
Fix tests
daavoo 23078f5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4fa57e0
Added test_image
daavoo 979c55d
pre-commit
daavoo c911db2
Fix catalyst and fastai
daavoo 01d1010
Make pillow optional dep
daavoo 440f8a9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cb2962e
Renamed scalar -> scalars
daavoo 811b8c9
Merge branch 'image-support' of https://github.com/iterative/dvclive β¦
daavoo c2bda79
Raise exception
daavoo 79f99a0
Fix pylint
daavoo 9280015
Old summary
daavoo 523d135
Removed subdirs
daavoo 404beaa
Add image summary
daavoo 48c8f9c
Fix test subdirs
daavoo 5441e44
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7295c9f
Include step in image summary
daavoo c1fccee
Merge branch 'image-support' of https://github.com/iterative/dvclive β¦
daavoo 2b9cc80
lint
daavoo af2c270
Raise Error on lazy PIL import
daavoo 720890c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5ca8323
Merge branch 'master' into image-support
daavoo 18052a9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5751203
Fix setup
daavoo dc6802c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 63fb269
Merge branch 'master' into image-support
daavoo 91ba34e
Fixed merge
daavoo 2b90ab0
Merge branch 'master' into image-support
daavoo 1cb1883
Fixed tests
daavoo 69beabc
Fixed step formatting
daavoo 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| from .scalar import Scalar # noqa: F401 | ||
| from .image_numpy import ImageNumpy | ||
| from .image_pil import ImagePIL | ||
| from .scalar import Scalar | ||
|
|
||
| DATA_TYPES = [ImageNumpy, ImagePIL, Scalar] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from dvclive.error import DvcLiveError | ||
|
|
||
| from .image_pil import ImagePIL | ||
|
|
||
|
|
||
| class ImageNumpy(ImagePIL): | ||
| @staticmethod | ||
| def could_log(val: object) -> bool: | ||
| if val.__class__.__module__ == "numpy": | ||
| return True | ||
| return False | ||
|
|
||
| def dump(self, val, step) -> None: | ||
| try: | ||
| from PIL import Image | ||
| except ImportError as e: | ||
| raise DvcLiveError( | ||
| "'pillow' is required for logging images." | ||
| " You can install it by running" | ||
| " 'pip install pillow'" | ||
| ) from e | ||
|
|
||
| val = Image.fromarray(val) | ||
| super().dump(val, step) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from pathlib import Path | ||
|
|
||
| from .base import Data | ||
|
|
||
|
|
||
| class ImagePIL(Data): | ||
| suffixes = [".jpg", ".jpeg", ".gif", ".png"] | ||
|
|
||
| @staticmethod | ||
| def could_log(val: object) -> bool: | ||
| if val.__class__.__module__ == "PIL.Image": | ||
| return True | ||
| return False | ||
|
|
||
| @property | ||
| def output_path(self) -> Path: | ||
| if Path(self.name).suffix not in self.suffixes: | ||
| raise ValueError( | ||
| f"Invalid image suffix '{Path(self.name).suffix}'" | ||
| f" Must be one of {self.suffixes}" | ||
| ) | ||
| return self.output_folder / "{step}" / self.name | ||
|
|
||
| def dump(self, val, step) -> None: | ||
| super().dump(val, step) | ||
| output_path = Path(str(self.output_path).format(step=step)) | ||
| output_path.parent.mkdir(exist_ok=True, parents=True) | ||
|
|
||
| val.save(output_path) | ||
|
|
||
| @property | ||
| def summary(self): | ||
| return {self.name: str(self.output_path).format(step=self.step)} |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import os | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from PIL import Image | ||
|
|
||
| # pylint: disable=unused-argument | ||
| from dvclive import Live | ||
| from tests.test_main import _parse_json | ||
|
|
||
|
|
||
| def test_PIL(tmp_dir): | ||
| dvclive = Live() | ||
| img = Image.new("RGB", (500, 500), (250, 250, 250)) | ||
| dvclive.log("image.png", img) | ||
|
|
||
| assert (tmp_dir / dvclive.dir / "0" / "image.png").exists() | ||
| summary = _parse_json("dvclive.json") | ||
|
|
||
| assert summary["image.png"] == os.path.join(dvclive.dir, "0", "image.png") | ||
|
|
||
|
|
||
| def test_invalid_extension(tmp_dir): | ||
| dvclive = Live() | ||
| img = Image.new("RGB", (500, 500), (250, 250, 250)) | ||
| with pytest.raises(ValueError): | ||
| dvclive.log("image.foo", img) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("shape", [(500, 500), (500, 500, 3), (500, 500, 4)]) | ||
| def test_numpy(tmp_dir, shape): | ||
| dvclive = Live() | ||
| img = np.ones(shape, np.uint8) * 255 | ||
| dvclive.log("image.png", img) | ||
|
|
||
| assert (tmp_dir / dvclive.dir / "0" / "image.png").exists() | ||
|
|
||
|
|
||
| def test_step_formatting(tmp_dir): | ||
| dvclive = Live() | ||
| img = np.ones((500, 500, 3), np.uint8) | ||
| for _ in range(3): | ||
| dvclive.log("image.png", img) | ||
| dvclive.next_step() | ||
|
|
||
| for step in range(3): | ||
| assert (tmp_dir / dvclive.dir / str(step) / "image.png").exists() | ||
|
|
||
| summary = _parse_json("dvclive.json") | ||
|
|
||
| assert summary["image.png"] == os.path.join( | ||
| dvclive.dir, str(step), "image.png" | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.