@@ -235,6 +235,55 @@ images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=True).
235235images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[- 3 :])))
236236```
237237
238+ Diffusers also has a Image-to-Image generation pipeline with Flax/Jax
239+ ``` python
240+ import jax
241+ import numpy as np
242+ import jax.numpy as jnp
243+ from flax.jax_utils import replicate
244+ from flax.training.common_utils import shard
245+ import requests
246+ from io import BytesIO
247+ from PIL import Image
248+ from diffusers import FlaxStableDiffusionImg2ImgPipeline
249+
250+ def create_key (seed = 0 ):
251+ return jax.random.PRNGKey(seed)
252+ rng = create_key(0 )
253+
254+ url = " https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
255+ response = requests.get(url)
256+ init_img = Image.open(BytesIO(response.content)).convert(" RGB" )
257+ init_img = init_img.resize((768 , 512 ))
258+
259+ prompts = " A fantasy landscape, trending on artstation"
260+
261+ pipeline, params = FlaxStableDiffusionImg2ImgPipeline.from_pretrained(
262+ " CompVis/stable-diffusion-v1-4" , revision = " flax" ,
263+ dtype = jnp.bfloat16,
264+ )
265+
266+ num_samples = jax.device_count()
267+ rng = jax.random.split(rng, jax.device_count())
268+ prompt_ids, processed_image = pipeline.prepare_inputs(prompt = [prompts]* num_samples, image = [init_img]* num_samples)
269+ p_params = replicate(params)
270+ prompt_ids = shard(prompt_ids)
271+ processed_image = shard(processed_image)
272+
273+ output = pipeline(
274+ prompt_ids = prompt_ids,
275+ image = processed_image,
276+ params = p_params,
277+ prng_seed = rng,
278+ strength = 0.75 ,
279+ num_inference_steps = 50 ,
280+ jit = True ,
281+ height = 512 ,
282+ width = 768 ).images
283+
284+ output_images = pipeline.numpy_to_pil(np.asarray(output.reshape((num_samples,) + output.shape[- 3 :])))
285+ ```
286+
238287### Image-to-Image text-guided generation with Stable Diffusion
239288
240289The ` StableDiffusionImg2ImgPipeline ` lets you pass a text prompt and an initial image to condition the generation of new images.
0 commit comments