mrbgom2000 commited on
Commit
c10274f
1 Parent(s): 6858b77

Update NAhaha

Browse files
Files changed (1) hide show
  1. NAhaha +18 -22
NAhaha CHANGED
@@ -1,27 +1,23 @@
1
- import jax
 
 
2
  import numpy as np
3
- from flax.jax_utils import replicate
4
- from flax.training.common_utils import shard
5
 
6
- from diffusers import FlaxStableDiffusionPipeline
 
 
7
 
8
- pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
9
- "CompVis/stable-diffusion-v1-4", revision="flax", dtype=jax.numpy.bfloat16
10
- )
11
 
12
- prompt = "a photo of an astronaut riding a horse on mars"
 
 
 
 
13
 
14
- prng_seed = jax.random.PRNGKey(0)
15
- num_inference_steps = 50
16
-
17
- num_samples = jax.device_count()
18
- prompt = num_samples * [prompt]
19
- prompt_ids = pipeline.prepare_inputs(prompt)
20
-
21
- # shard inputs and rng
22
- params = replicate(params)
23
- prng_seed = jax.random.split(prng_seed, num_samples)
24
- prompt_ids = shard(prompt_ids)
25
-
26
- images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=True).images
27
- images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
 
1
+ from diffusers import DDPMScheduler, UNet2DModel
2
+ from PIL import Image
3
+ import torch
4
  import numpy as np
 
 
5
 
6
+ scheduler = DDPMScheduler.from_pretrained("google/ddpm-cat-256")
7
+ model = UNet2DModel.from_pretrained("google/ddpm-cat-256").to("cuda")
8
+ scheduler.set_timesteps(50)
9
 
10
+ sample_size = model.config.sample_size
11
+ noise = torch.randn((1, 3, sample_size, sample_size)).to("cuda")
12
+ input = noise
13
 
14
+ for t in scheduler.timesteps:
15
+ with torch.no_grad():
16
+ noisy_residual = model(input, t).sample
17
+ prev_noisy_sample = scheduler.step(noisy_residual, t, input).prev_sample
18
+ input = prev_noisy_sample
19
 
20
+ image = (input / 2 + 0.5).clamp(0, 1)
21
+ image = image.cpu().permute(0, 2, 3, 1).numpy()[0]
22
+ image = Image.fromarray((image * 255).round().astype("uint8"))
23
+ image