Update README.md
Browse files
README.md
CHANGED
@@ -20,10 +20,28 @@ tags:
|
|
20 |
|
21 |
## Usage
|
22 |
|
23 |
-
###
|
24 |
|
25 |
```python
|
26 |
-
!pip install
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
from diffusers import UNet2DModel, DDIMScheduler, VQModel
|
28 |
import torch
|
29 |
import PIL.Image
|
@@ -33,9 +51,9 @@ import tqdm
|
|
33 |
seed = 3
|
34 |
|
35 |
# load all models
|
36 |
-
unet = UNet2DModel.from_pretrained("CompVis/
|
37 |
-
vqvae = VQModel.from_pretrained("CompVis/
|
38 |
-
scheduler = DDIMScheduler.from_config("CompVis/
|
39 |
|
40 |
# set to cuda
|
41 |
torch_device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -46,7 +64,7 @@ vqvae.to(torch_device)
|
|
46 |
# generate gaussian noise to be decoded
|
47 |
generator = torch.manual_seed(seed)
|
48 |
noise = torch.randn(
|
49 |
-
(1, unet.in_channels, unet.
|
50 |
generator=generator,
|
51 |
).to(torch_device)
|
52 |
|
@@ -78,36 +96,10 @@ image_pil = PIL.Image.fromarray(image_processed[0])
|
|
78 |
image_pil.save(f"generated_image_{seed}.png")
|
79 |
```
|
80 |
|
81 |
-
### pipeline
|
82 |
-
|
83 |
-
```python
|
84 |
-
!pip install git+https://github.com/huggingface/diffusers.git
|
85 |
-
from diffusers import LatentDiffusionUncondPipeline
|
86 |
-
import torch
|
87 |
-
import PIL.Image
|
88 |
-
import numpy as np
|
89 |
-
import tqdm
|
90 |
-
|
91 |
-
seed = 3
|
92 |
-
|
93 |
-
pipeline = LatentDiffusionUncondPipeline.from_pretrained("CompVis/latent-diffusion-celeba-256")
|
94 |
-
|
95 |
-
# generatae image by calling the pipeline
|
96 |
-
generator = torch.manual_seed(seed)
|
97 |
-
image = pipeline(generator=generator, num_inference_steps=200)["sample"]
|
98 |
-
|
99 |
-
# process image
|
100 |
-
image_processed = image.cpu().permute(0, 2, 3, 1)
|
101 |
-
image_processed = (image_processed + 1.0) * 127.5
|
102 |
-
image_processed = image_processed.clamp(0, 255).numpy().astype(np.uint8)
|
103 |
-
image_pil = PIL.Image.fromarray(image_processed[0])
|
104 |
-
|
105 |
-
image_pil.save(f"generated_image_{seed}.png")
|
106 |
-
```
|
107 |
|
108 |
## Samples
|
109 |
|
110 |
-
1. ![
|
111 |
-
2. ![sample_1](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/generated_image_1.png)
|
112 |
-
3. ![
|
113 |
-
4. ![
|
|
|
20 |
|
21 |
## Usage
|
22 |
|
23 |
+
### Inference with a pipeline
|
24 |
|
25 |
```python
|
26 |
+
!pip install diffusers
|
27 |
+
from diffusers import DiffusionPipeline
|
28 |
+
|
29 |
+
model_id = "CompVis/ldm-celebahq-256"
|
30 |
+
|
31 |
+
# load model and scheduler
|
32 |
+
pipeline = DiffusionPipeline.from_pretrained(model_id)
|
33 |
+
|
34 |
+
# run pipeline in inference (sample random noise and denoise)
|
35 |
+
image = pipeline(num_inference_steps=200)["sample"]
|
36 |
+
|
37 |
+
# save image
|
38 |
+
image[0].save("ldm_generated_image.png")
|
39 |
+
```
|
40 |
+
|
41 |
+
### Inference with an unrolled loop
|
42 |
+
|
43 |
+
```python
|
44 |
+
!pip install diffusers
|
45 |
from diffusers import UNet2DModel, DDIMScheduler, VQModel
|
46 |
import torch
|
47 |
import PIL.Image
|
|
|
51 |
seed = 3
|
52 |
|
53 |
# load all models
|
54 |
+
unet = UNet2DModel.from_pretrained("CompVis/ldm-celebahq-256", subfolder="unet")
|
55 |
+
vqvae = VQModel.from_pretrained("CompVis/ldm-celebahq-256", subfolder="vqvae")
|
56 |
+
scheduler = DDIMScheduler.from_config("CompVis/ldm-celebahq-256", subfolder="scheduler")
|
57 |
|
58 |
# set to cuda
|
59 |
torch_device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
64 |
# generate gaussian noise to be decoded
|
65 |
generator = torch.manual_seed(seed)
|
66 |
noise = torch.randn(
|
67 |
+
(1, unet.in_channels, unet.sample_size, unet.sample_size),
|
68 |
generator=generator,
|
69 |
).to(torch_device)
|
70 |
|
|
|
96 |
image_pil.save(f"generated_image_{seed}.png")
|
97 |
```
|
98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
## Samples
|
101 |
|
102 |
+
1. ![sample_0](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/images/generated_image_0.png)
|
103 |
+
2. ![sample_1](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/images/generated_image_1.png)
|
104 |
+
3. ![sample_2](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/images/generated_image_2.png)
|
105 |
+
4. ![sample_3](https://huggingface.co/CompVis/latent-diffusion-celeba-256/resolve/main/images/generated_image_3.png)
|