Skip to content

Commit

Permalink
SyntheticImageGenerator doesn't load files
Browse files Browse the repository at this point in the history
  • Loading branch information
mwawrzos committed Jul 15, 2024
1 parent 287edba commit af0a93a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum, auto
from io import BytesIO
from pathlib import Path
from typing import Generator, List, Optional, Tuple, cast
from typing import Generator, Optional, Tuple, cast

import numpy as np
from genai_perf.exceptions import GenAIPerfException
Expand All @@ -25,57 +25,15 @@ def __call__(self, image):
return f"data:image/{self.image_format.name.lower()};base64,{data}"


def images_from_file_generator(image_path: Path) -> Generator[Image.Image, None, None]:
if not image_path.exists():
raise GenAIPerfException(f"File not found: {image_path}")

image = Image.open(image_path)
while True:
yield image


def white_images_generator() -> Generator[Image.Image, None, None]:
white_image = Image.new("RGB", (100, 100), color="white")
while True:
yield white_image


def build_synthetic_image_generator(
image_width_mean: int,
image_height_mean: int,
image_width_stddev: int,
image_height_stddev: int,
image_path: Optional[Path] = None,
image_format: ImageFormat = ImageFormat.PNG,
):
if image_path is None:
image_iterator = white_images_generator()
else:
image_path = cast(Path, image_path)
image_iterator = images_from_file_generator(image_path)

image_generator = SyntheticImageGenerator(
image_width_mean=image_width_mean,
image_height_mean=image_height_mean,
image_width_stddev=image_width_stddev,
image_height_stddev=image_height_stddev,
image_iterator=image_iterator,
)
base64_encode = Base64Encoder(image_format)
return (base64_encode(image) for image in image_generator)


class SyntheticImageGenerator:
def __init__(
self,
image_width_mean: int,
image_height_mean: int,
image_width_stddev: int,
image_height_stddev: int,
image_iterator: Generator[Image.Image, None, None],
rng: Optional[np.random.Generator] = None,
):
self.image_iterator = image_iterator
self._image_width_mean = image_width_mean
self._image_height_mean = image_height_mean
self._image_width_stddev = image_width_stddev
Expand All @@ -101,7 +59,10 @@ def random_resize(self, image):
)
return image.resize((width, height))

def get_next_image(self):
return Image.new("RGB", (100, 100), color="white")

def __next__(self):
image = next(self.image_iterator)
image = self.get_next_image()
image = self.random_resize(image)
return image
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
Base64Encoder,
ImageFormat,
SyntheticImageGenerator,
images_from_file_generator,
white_images_generator,
)
from PIL import Image

Expand All @@ -30,7 +28,6 @@ def test_different_image_size(expected_image_size):
image_height_mean=expected_height,
image_width_stddev=0,
image_height_stddev=0,
image_iterator=white_images_generator(),
)

image = next(sut)
Expand All @@ -45,7 +42,6 @@ def test_negative_size_is_not_selected():
image_height_mean=-1,
image_width_stddev=10,
image_height_stddev=10,
image_iterator=white_images_generator(),
)

# exception is raised, when PIL.Image.resize is called with negative values
Expand All @@ -56,52 +52,15 @@ def test_generator_deterministic():
IMAGE_SIZE = 100, 100
STDDEV = 100, 100
SEED = 44
img_gen1 = white_images_generator()
img_gen2 = white_images_generator()
rng1 = np.random.default_rng(seed=SEED)
rng2 = np.random.default_rng(seed=SEED)
sut1 = SyntheticImageGenerator(*IMAGE_SIZE, *STDDEV, img_gen1, rng=rng1)
sut2 = SyntheticImageGenerator(*IMAGE_SIZE, *STDDEV, img_gen2, rng=rng2)
sut1 = SyntheticImageGenerator(*IMAGE_SIZE, *STDDEV, rng=rng1)
sut2 = SyntheticImageGenerator(*IMAGE_SIZE, *STDDEV, rng=rng2)

for _, img1, img2 in zip(range(5), sut1, sut2):
assert img1 == img2, "generator is nondererministic"


@patch("pathlib.Path.exists", return_value=False)
def test_images_from_file_raises_when_file_not_found(mock_exists):
DUMMY_PATH = Path("dummy-image.png")
sut = images_from_file_generator(DUMMY_PATH)

with pytest.raises(GenAIPerfException):
next(sut)


@patch("pathlib.Path.exists", return_value=True)
def test_images_from_file_generates_multiple_times(mock_exists):
DUMMY_IMAGE = Image.new("RGB", (100, 100), color="blue")
DUMMY_PATH = Path("dummy-image.png")
with patch("PIL.Image.open", return_value=DUMMY_IMAGE) as mock_file:
sut = images_from_file_generator(DUMMY_PATH)

image = next(sut)

mock_exists.assert_called_once()
mock_file.assert_called_once_with(DUMMY_PATH)
assert image == DUMMY_IMAGE, "unexpected image produced"

image = next(sut)
assert image == DUMMY_IMAGE, "unexpected image produced"


def test_white_images_generator():
sut = white_images_generator()

image = next(sut)
assert isinstance(image, Image.Image), "generator produces unexpected type of data"
white_pixel = np.array([[[255, 255, 255]]])
assert (np.array(image) == white_pixel).all(), "not all pixels are white"


@pytest.mark.parametrize("image_format", [ImageFormat.PNG, ImageFormat.JPEG])
def test_base64_encoding_with_different_formats(image_format):
image = Image.new("RGB", (100, 100))
Expand Down

0 comments on commit af0a93a

Please sign in to comment.