Skip to content

Commit

Permalink
Merge pull request #288 from bghira/main
Browse files Browse the repository at this point in the history
v0.9.5
  • Loading branch information
bghira authored Jan 29, 2024
2 parents a2020fa + e771031 commit 3335094
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
2 changes: 0 additions & 2 deletions helpers/caching/vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,6 @@ def _process_images_in_batch(self) -> None:
if self.minimum_image_size is not None:
if not self.bucket_manager.meets_resolution_requirements(
image_path=filepath,
minimum_image_size=self.minimum_image_size,
resolution_type=self.resolution_type,
):
self.debug_log(
f"Skipping {filepath} because it does not meet the minimum image size requirement of {self.minimum_image_size}"
Expand Down
30 changes: 19 additions & 11 deletions helpers/multiaspect/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ def _bucket_worker(
local_aspect_ratio_bucket_indices,
metadata_updates=local_metadata_updates,
delete_problematic_images=self.delete_problematic_images,
minimum_image_size=self.minimum_image_size,
resolution_type=self.resolution_type,
)
processed_file_count += 1
processed_file_list.add(file)
Expand Down Expand Up @@ -467,8 +465,6 @@ def meets_resolution_requirements(
self,
image_path: str = None,
image: Image = None,
minimum_image_size: int = None,
resolution_type: str = None,
):
"""
Check if an image meets the resolution requirements.
Expand All @@ -488,22 +484,34 @@ def meets_resolution_requirements(
f" ({image_path}) or Image object ({image}), but received neither."
)

if minimum_image_size is None:
if self.minimum_image_size is None:
return True

if resolution_type == "pixel":
return minimum_image_size <= width and minimum_image_size <= height
elif resolution_type == "area":
if self.resolution_type == "pixel":
return (
self.minimum_image_size <= width and self.minimum_image_size <= height
)
elif self.resolution_type == "area":
# We receive megapixel integer value, and then have to compare here by converting minimum_image_size MP to pixels.
if minimum_image_size > 5:
if self.minimum_image_size > 5:
raise ValueError(
f"--minimum_image_size was given with a value of {minimum_image_size} but resolution_type is area, which means this value is most likely too large. Please use a value less than 5."
)
minimum_image_size = minimum_image_size * 1_000_000
# We need to find the square image length if crop_style = square.
minimum_image_size = self.minimum_image_size * 1_000_000
if self.crop and self.crop_style == "square":
# When comparing the 'area' of an image but cropping to square area, one side might be too small.
# So we have to convert our megapixel value to a 1.0 aspect square image size.
# We do this by taking the square root of the megapixel value.
pixel_edge_len = floor(np.sqrt(minimum_image_size))
if not (pixel_edge_len <= width and pixel_edge_len <= height):
# If the square edge length is too small, then the image is too small.
return False
# Since we've now tested whether a square-cropped image will be adequate, we can calculate the area of the image.
return minimum_image_size <= width * height
else:
raise ValueError(
f"BucketManager.meets_resolution_requirements received unexpected value for resolution_type: {resolution_type}"
f"BucketManager.meets_resolution_requirements received unexpected value for resolution_type: {self.resolution_type}"
)

def handle_incorrect_bucket(self, image_path: str, bucket: str, actual_bucket: str):
Expand Down
23 changes: 19 additions & 4 deletions helpers/multiaspect/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def process_for_bucket(
aspect_ratio_rounding: int = 2,
metadata_updates=None,
delete_problematic_images: bool = False,
minimum_image_size: int = None,
resolution_type: str = "pixel",
):
try:
image_metadata = {}
Expand Down Expand Up @@ -63,8 +61,6 @@ def process_for_bucket(
)
if not bucket_manager.meets_resolution_requirements(
image=image,
minimum_image_size=minimum_image_size,
resolution_type=resolution_type,
):
logger.debug(
f"Image {image_path_str} does not meet minimum image size requirements. Skipping image."
Expand Down Expand Up @@ -183,6 +179,25 @@ def prepare_image(
image = MultiaspectImage._resize_image(
image, target_width, target_height
)
if resolution_type == "area":
# Convert original_resolution back from eg. 1024 pixels to 1.0 mp
original_megapixel_resolution = original_resolution / 1e3
(
target_width,
target_height,
) = MultiaspectImage.calculate_new_size_by_pixel_area(
original_width, original_height, original_megapixel_resolution
)
elif resolution_type == "pixel":
(
target_width,
target_height,
) = MultiaspectImage.calculate_new_size_by_pixel_edge(
original_width, original_height, original_resolution
)
logger.debug(
f"Recalculated target_width and target_height {target_width}x{target_height} based on original_resolution: {original_resolution}"
)

logger.debug(f"We are cropping the image. Data backend: {id}")
crop_width, crop_height = (
Expand Down

0 comments on commit 3335094

Please sign in to comment.