Skip to content

⚡️ Speed up method BlipImageProcessor.postprocess by 51% #11666

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 8 additions & 5 deletions src/diffusers/pipelines/blip_diffusion/blip_image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,20 +299,23 @@ def preprocess(

# Follows diffusers.VaeImageProcessor.postprocess
def postprocess(self, sample: torch.Tensor, output_type: str = "pil"):
if output_type not in ["pt", "np", "pil"]:
if output_type not in {"pt", "np", "pil"}:
raise ValueError(
f"output_type={output_type} is not supported. Make sure to choose one of ['pt', 'np', or 'pil']"
)

# Equivalent to diffusers.VaeImageProcessor.denormalize
sample = (sample / 2 + 0.5).clamp(0, 1)
sample = (sample / 2 + 0.5).clamp_(0, 1)
if output_type == "pt":
return sample

# Only move to CPU and numpy if necessary
if sample.device.type != "cpu":
sample = sample.cpu()
# Equivalent to diffusers.VaeImageProcessor.pt_to_numpy
sample = sample.cpu().permute(0, 2, 3, 1).numpy()
sample = sample.permute(0, 2, 3, 1).contiguous().numpy()
if output_type == "np":
return sample

# Output_type must be 'pil'
sample = numpy_to_pil(sample)
return sample
return numpy_to_pil(sample)
9 changes: 4 additions & 5 deletions src/diffusers/utils/pil_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,15 @@ def numpy_to_pil(images):
"""
Convert a numpy image or a batch of images to a PIL image.
"""
# If single HWC image, expand dims to NHWC
if images.ndim == 3:
images = images[None, ...]
images = (images * 255).round().astype("uint8")
images = (images * 255).round().astype("uint8", copy=False)
if images.shape[-1] == 1:
# special case for grayscale (single channel) images
pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images]
return [Image.fromarray(image[..., 0], mode="L") for image in images]
else:
pil_images = [Image.fromarray(image) for image in images]

return pil_images
return [Image.fromarray(image) for image in images]


def make_image_grid(images: List[PIL.Image.Image], rows: int, cols: int, resize: int = None) -> PIL.Image.Image:
Expand Down