Skip to content

Commit

Permalink
fix image resizing for diffusers < v0.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
echarlaix committed Sep 4, 2023
1 parent 99f4018 commit c2d966b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions optimum/pipelines/diffusers/pipeline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import torch
from diffusers import ConfigMixin
from diffusers.image_processor import VaeImageProcessor as DiffusersVaeImageProcessor
from diffusers.utils.pil_utils import PIL_INTERPOLATION
from PIL import Image
from tqdm.auto import tqdm

Expand Down Expand Up @@ -259,3 +260,23 @@ def reshape(images: np.ndarray) -> np.ndarray:
images = images[..., None]

return images.transpose(0, 3, 1, 2)

# TODO : remove after diffusers v0.21.0 release
def resize(
self,
image: [PIL.Image.Image, np.ndarray, torch.Tensor],
height: Optional[int] = None,
width: Optional[int] = None,
) -> [PIL.Image.Image, np.ndarray, torch.Tensor]:
"""
Resize image.
"""
if isinstance(image, PIL.Image.Image):
image = image.resize((width, height), resample=PIL_INTERPOLATION[self.config.resample])
elif isinstance(image, torch.Tensor):
image = torch.nn.functional.interpolate(image, size=(height, width))
elif isinstance(image, np.ndarray):
image = self.numpy_to_pt(image)
image = torch.nn.functional.interpolate(image, size=(height, width))
image = self.pt_to_numpy(image)
return image

0 comments on commit c2d966b

Please sign in to comment.