-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added image_pil and image_numpy * Use DATA_TYPES list in metrics * Use subdir structure * Use data subdirs in init_path * Fix test_logging * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added test_image * pre-commit * Fix catalyst and fastai * Make pillow optional dep * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Renamed scalar -> scalars * Raise exception * Fix pylint * Old summary * Removed subdirs * Add image summary * Fix test subdirs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Include step in image summary * lint * Raise Error on lazy PIL import * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix setup * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fixed merge * Fixed tests * Fixed step formatting Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f6739c5
commit 5227d6e
Showing
8 changed files
with
148 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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