Skip to content

Commit 65d136e

Browse files
Add improved handling of pil (huggingface#1309)
* Better error message for transformers dummy * [PIL] Better deprecation functionality * up
1 parent 46893ad commit 65d136e

20 files changed

+64
-39
lines changed

docs/source/api/pipelines/latent_diffusion.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ The original codebase can be found [here](https://github.com/CompVis/latent-diff
3939

4040

4141
## LDMTextToImagePipeline
42-
[[autodoc]] pipelines.latent_diffusion.pipeline_latent_diffusion.LDMTextToImagePipeline
42+
[[autodoc]] LDMTextToImagePipeline
4343
- __call__
4444

4545
## LDMSuperResolutionPipeline
46-
[[autodoc]] pipelines.latent_diffusion.pipeline_latent_diffusion_superresolution.LDMSuperResolutionPipeline
46+
[[autodoc]] LDMSuperResolutionPipeline
4747
- __call__

docs/source/api/pipelines/overview.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ available a colab notebook to directly try them out.
5050
| [ddpm](./api/pipelines/ddpm) | [**Denoising Diffusion Probabilistic Models**](https://arxiv.org/abs/2006.11239) | Unconditional Image Generation |
5151
| [ddim](./api/pipelines/ddim) | [**Denoising Diffusion Implicit Models**](https://arxiv.org/abs/2010.02502) | Unconditional Image Generation |
5252
| [latent_diffusion](./api/pipelines/latent_diffusion) | [**High-Resolution Image Synthesis with Latent Diffusion Models**](https://arxiv.org/abs/2112.10752)| Text-to-Image Generation |
53+
| [latent_diffusion](./api/pipelines/latent_diffusion) | [**High-Resolution Image Synthesis with Latent Diffusion Models**](https://arxiv.org/abs/2112.10752)| Super Resolution Image-to-Image |
5354
| [latent_diffusion_uncond](./api/pipelines/latent_diffusion_uncond) | [**High-Resolution Image Synthesis with Latent Diffusion Models**](https://arxiv.org/abs/2112.10752) | Unconditional Image Generation |
5455
| [pndm](./api/pipelines/pndm) | [**Pseudo Numerical Methods for Diffusion Models on Manifolds**](https://arxiv.org/abs/2202.09778) | Unconditional Image Generation |
5556
| [score_sde_ve](./api/pipelines/score_sde_ve) | [**Score-Based Generative Modeling through Stochastic Differential Equations**](https://openreview.net/forum?id=PxTIG12RRHS) | Unconditional Image Generation |

docs/source/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ available a colab notebook to directly try them out.
4040
| [ddpm](./api/pipelines/ddpm) | [**Denoising Diffusion Probabilistic Models**](https://arxiv.org/abs/2006.11239) | Unconditional Image Generation |
4141
| [ddim](./api/pipelines/ddim) | [**Denoising Diffusion Implicit Models**](https://arxiv.org/abs/2010.02502) | Unconditional Image Generation |
4242
| [latent_diffusion](./api/pipelines/latent_diffusion) | [**High-Resolution Image Synthesis with Latent Diffusion Models**](https://arxiv.org/abs/2112.10752)| Text-to-Image Generation |
43+
| [latent_diffusion](./api/pipelines/latent_diffusion) | [**High-Resolution Image Synthesis with Latent Diffusion Models**](https://arxiv.org/abs/2112.10752)| Super Resolution Image-to-Image |
4344
| [latent_diffusion_uncond](./api/pipelines/latent_diffusion_uncond) | [**High-Resolution Image Synthesis with Latent Diffusion Models**](https://arxiv.org/abs/2112.10752) | Unconditional Image Generation |
4445
| [pndm](./api/pipelines/pndm) | [**Pseudo Numerical Methods for Diffusion Models on Manifolds**](https://arxiv.org/abs/2202.09778) | Unconditional Image Generation |
4546
| [score_sde_ve](./api/pipelines/score_sde_ve) | [**Score-Based Generative Modeling through Stochastic Differential Equations**](https://openreview.net/forum?id=PxTIG12RRHS) | Unconditional Image Generation |

examples/community/imagic_stable_diffusion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
1818
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
1919
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
20-
from diffusers.utils import logging
20+
from diffusers.utils import PIL_INTERPOLATION, logging
2121
from tqdm.auto import tqdm
2222
from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
2323

@@ -28,7 +28,7 @@
2828
def preprocess(image):
2929
w, h = image.size
3030
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
31-
image = image.resize((w, h), resample=PIL.Image.LANCZOS)
31+
image = image.resize((w, h), resample=PIL_INTERPOLATION["lanczos"])
3232
image = np.array(image).astype(np.float32) / 255.0
3333
image = image[None].transpose(0, 3, 1, 2)
3434
image = torch.from_numpy(image)

examples/community/lpw_stable_diffusion.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
1313
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
1414
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
15-
from diffusers.utils import deprecate, is_accelerate_available, logging
15+
from diffusers.utils import PIL_INTERPOLATION, deprecate, is_accelerate_available, logging
1616
from transformers import CLIPFeatureExtractor, CLIPTextModel, CLIPTokenizer
1717

1818

@@ -358,7 +358,7 @@ def get_weighted_text_embeddings(
358358
def preprocess_image(image):
359359
w, h = image.size
360360
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
361-
image = image.resize((w, h), resample=PIL.Image.LANCZOS)
361+
image = image.resize((w, h), resample=PIL_INTERPOLATION["lanczos"])
362362
image = np.array(image).astype(np.float32) / 255.0
363363
image = image[None].transpose(0, 3, 1, 2)
364364
image = torch.from_numpy(image)
@@ -369,7 +369,7 @@ def preprocess_mask(mask):
369369
mask = mask.convert("L")
370370
w, h = mask.size
371371
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
372-
mask = mask.resize((w // 8, h // 8), resample=PIL.Image.NEAREST)
372+
mask = mask.resize((w // 8, h // 8), resample=PIL_INTERPOLATION["nearest"])
373373
mask = np.array(mask).astype(np.float32) / 255.0
374374
mask = np.tile(mask, (4, 1, 1))
375375
mask = mask[None].transpose(0, 1, 2, 3) # what does this step do?

examples/community/lpw_stable_diffusion_onnx.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from diffusers.pipeline_utils import DiffusionPipeline
1111
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
1212
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
13-
from diffusers.utils import logging
13+
from diffusers.utils import PIL_INTERPOLATION, logging
1414
from transformers import CLIPFeatureExtractor, CLIPTokenizer
1515

1616

@@ -365,7 +365,7 @@ def get_weighted_text_embeddings(
365365
def preprocess_image(image):
366366
w, h = image.size
367367
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
368-
image = image.resize((w, h), resample=PIL.Image.LANCZOS)
368+
image = image.resize((w, h), resample=PIL_INTERPOLATION["lanczos"])
369369
image = np.array(image).astype(np.float32) / 255.0
370370
image = image[None].transpose(0, 3, 1, 2)
371371
return 2.0 * image - 1.0
@@ -375,7 +375,7 @@ def preprocess_mask(mask):
375375
mask = mask.convert("L")
376376
w, h = mask.size
377377
w, h = map(lambda x: x - x % 32, (w, h)) # resize to integer multiple of 32
378-
mask = mask.resize((w // 8, h // 8), resample=PIL.Image.NEAREST)
378+
mask = mask.resize((w // 8, h // 8), resample=PIL_INTERPOLATION["nearest"])
379379
mask = np.array(mask).astype(np.float32) / 255.0
380380
mask = np.tile(mask, (4, 1, 1))
381381
mask = mask[None].transpose(0, 1, 2, 3) # what does this step do?

examples/textual_inversion/textual_inversion.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import torch.utils.checkpoint
1313
from torch.utils.data import Dataset
1414

15-
import PIL
1615
from accelerate import Accelerator
1716
from accelerate.logging import get_logger
1817
from accelerate.utils import set_seed
1918
from diffusers import AutoencoderKL, DDPMScheduler, PNDMScheduler, StableDiffusionPipeline, UNet2DConditionModel
2019
from diffusers.optimization import get_scheduler
2120
from diffusers.pipelines.stable_diffusion import StableDiffusionSafetyChecker
21+
from diffusers.utils import PIL_INTERPOLATION
2222
from huggingface_hub import HfFolder, Repository, whoami
2323
from PIL import Image
2424
from torchvision import transforms
@@ -260,10 +260,10 @@ def __init__(
260260
self._length = self.num_images * repeats
261261

262262
self.interpolation = {
263-
"linear": PIL.Image.LINEAR,
264-
"bilinear": PIL.Image.BILINEAR,
265-
"bicubic": PIL.Image.BICUBIC,
266-
"lanczos": PIL.Image.LANCZOS,
263+
"linear": PIL_INTERPOLATION["linear"],
264+
"bilinear": PIL_INTERPOLATION["bilinear"],
265+
"bicubic": PIL_INTERPOLATION["bicubic"],
266+
"lanczos": PIL_INTERPOLATION["lanczos"],
267267
}[interpolation]
268268

269269
self.templates = imagenet_style_templates_small if learnable_property == "style" else imagenet_templates_small

examples/textual_inversion/textual_inversion_flax.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import jax
1515
import jax.numpy as jnp
1616
import optax
17-
import PIL
1817
import transformers
1918
from diffusers import (
2019
FlaxAutoencoderKL,
@@ -24,6 +23,7 @@
2423
FlaxUNet2DConditionModel,
2524
)
2625
from diffusers.pipelines.stable_diffusion import FlaxStableDiffusionSafetyChecker
26+
from diffusers.utils import PIL_INTERPOLATION
2727
from flax import jax_utils
2828
from flax.training import train_state
2929
from flax.training.common_utils import shard
@@ -246,10 +246,10 @@ def __init__(
246246
self._length = self.num_images * repeats
247247

248248
self.interpolation = {
249-
"linear": PIL.Image.LINEAR,
250-
"bilinear": PIL.Image.BILINEAR,
251-
"bicubic": PIL.Image.BICUBIC,
252-
"lanczos": PIL.Image.LANCZOS,
249+
"linear": PIL_INTERPOLATION["linear"],
250+
"bilinear": PIL_INTERPOLATION["bilinear"],
251+
"bicubic": PIL_INTERPOLATION["bicubic"],
252+
"lanczos": PIL_INTERPOLATION["lanczos"],
253253
}[interpolation]
254254

255255
self.templates = imagenet_style_templates_small if learnable_property == "style" else imagenet_templates_small

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
# 1. all dependencies should be listed here with their version requirements if any
7979
# 2. once modified, run: `make deps_table_update` to update src/diffusers/dependency_versions_table.py
8080
_deps = [
81-
"Pillow<10.0", # keep the PIL.Image.Resampling deprecation away
81+
"Pillow", # keep the PIL.Image.Resampling deprecation away
8282
"accelerate>=0.11.0",
8383
"black==22.8",
8484
"datasets",

src/diffusers/dependency_versions_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# 1. modify the `_deps` dict in setup.py
33
# 2. run `make deps_table_update``
44
deps = {
5-
"Pillow": "Pillow<10.0",
5+
"Pillow": "Pillow",
66
"accelerate": "accelerate>=0.11.0",
77
"black": "black==22.8",
88
"datasets": "datasets",

0 commit comments

Comments
 (0)