Add image_processor (#2617) · Aivean/diffusers@e52cd55 · GitHub
Skip to content

Commit e52cd55

Browse files
yiyixuxuyiyixuxupatrickvonplatenpcuenca
authored
Add image_processor (huggingface#2617)
* add image_processor --------- Co-authored-by: yiyixuxu <yixu310@gmail,com> Co-authored-by: Patrick von Platen <patrick.v.platen@gmail.com> Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
1 parent c0b4d72 commit e52cd55

6 files changed

Lines changed: 447 additions & 45 deletions

File tree

‎src/diffusers/image_processor.py‎

Lines changed: 177 additions & 0 deletions

‎src/diffusers/pipelines/alt_diffusion/pipeline_alt_diffusion_img2img.py‎

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from diffusers.utils import is_accelerate_available, is_accelerate_version
2525

2626
from ...configuration_utils import FrozenDict
27+
from ...image_processor import VaeImageProcessor
2728
from ...models import AutoencoderKL, UNet2DConditionModel
2829
from ...schedulers import KarrasDiffusionSchedulers
2930
from ...utils import PIL_INTERPOLATION, deprecate, logging, randn_tensor, replace_example_docstring
@@ -192,7 +193,6 @@ def __init__(
192193
new_config = dict(unet.config)
193194
new_config["sample_size"] = 64
194195
unet._internal_dict = FrozenDict(new_config)
195-
196196
self.register_modules(
197197
vae=vae,
198198
text_encoder=text_encoder,
@@ -203,7 +203,11 @@ def __init__(
203203
feature_extractor=feature_extractor,
204204
)
205205
self.vae_scale_factor = 2 ** (len(self.vae.config.block_out_channels) - 1)
206-
self.register_to_config(requires_safety_checker=requires_safety_checker)
206+
207+
self.image_processor = VaeImageProcessor(vae_scale_factor=self.vae_scale_factor)
208+
self.register_to_config(
209+
requires_safety_checker=requires_safety_checker,
210+
)
207211

208212
def enable_sequential_cpu_offload(self, gpu_id=0):
209213
r"""
@@ -415,21 +419,17 @@ def _encode_prompt(
415419
return prompt_embeds
416420

417421
def run_safety_checker(self, image, device, dtype):
418-
if self.safety_checker is not None:
419-
safety_checker_input = self.feature_extractor(self.numpy_to_pil(image), return_tensors="pt").to(device)
420-
image, has_nsfw_concept = self.safety_checker(
421-
images=image, clip_input=safety_checker_input.pixel_values.to(dtype)
422-
)
423-
else:
424-
has_nsfw_concept = None
422+
feature_extractor_input = self.image_processor.postprocess(image, output_type="pil")
423+
safety_checker_input = self.feature_extractor(feature_extractor_input, return_tensors="pt").to(device)
424+
image, has_nsfw_concept = self.safety_checker(
425+
images=image, clip_input=safety_checker_input.pixel_values.to(dtype)
426+
)
425427
return image, has_nsfw_concept
426428

427429
def decode_latents(self, latents):
428430
latents = 1 / self.vae.config.scaling_factor * latents
429431
image = self.vae.decode(latents).sample
430432
image = (image / 2 + 0.5).clamp(0, 1)
431-
# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
432-
image = image.cpu().permute(0, 2, 3, 1).float().numpy()
433433
return image
434434

435435
def prepare_extra_step_kwargs(self, generator, eta):
@@ -663,7 +663,7 @@ def __call__(
663663
)
664664

665665
# 4. Preprocess image
666-
image = preprocess(image)
666+
image = self.image_processor.preprocess(image)
667667

668668
# 5. set timesteps
669669
self.scheduler.set_timesteps(num_inference_steps, device=device)
@@ -703,15 +703,26 @@ def __call__(
703703
if callback is not None and i % callback_steps == 0:
704704
callback(i, t, latents)
705705

706-
# 9. Post-processing
706+
if output_type not in ["latent", "pt", "np", "pil"]:
707+
deprecation_message = (
708+
f"the output_type {output_type} is outdated. Please make sure to set it to one of these instead: "
709+
"`pil`, `np`, `pt`, `latent`"
710+
)
711+
deprecate("Unsupported output_type", "1.0.0", deprecation_message, standard_warn=False)
712+
output_type = "np"
713+
714+
if output_type == "latent":
715+
image = latents
716+
has_nsfw_concept = None
717+
707718
image = self.decode_latents(latents)
708719

709-
# 10. Run safety checker
710-
image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype)
720+
if self.safety_checker is not None:
721+
image, has_nsfw_concept = self.run_safety_checker(image, device, prompt_embeds.dtype)
722+
else:
723+
has_nsfw_concept = False
711724

712-
# 11. Convert to PIL
713-
if output_type == "pil":
714-
image = self.numpy_to_pil(image)
725+
image = self.image_processor.postprocess(image, output_type=output_type)
715726

716727
# Offload last model to CPU
717728
if hasattr(self, "final_offload_hook") and self.final_offload_hook is not None:

‎src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion_img2img.py‎

Lines changed: 29 additions & 21 deletions

0 commit comments

Comments
 (0)