Skip to content

Commit ef4365c

Browse files
up
1 parent addc43a commit ef4365c

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

models/vision/ddpm/example.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
#!/usr/bin/env python3
22
import tempfile
33
import sys
4-
4+
import os
5+
import pathlib
56
from modeling_ddpm import DDPM
7+
import PIL.Image
8+
import numpy as np
69

7-
model_id = sys.argv[1]
10+
model_ids = ["ddpm-lsun-cat", "ddpm-lsun-cat-ema", "ddpm-lsun-church-ema", "ddpm-lsun-church", "ddpm-lsun-bedroom", "ddpm-lsun-bedroom-ema", "ddpm-cifar10-ema", "ddpm-lsun-cifar10", "ddpm-lsun-celeba-hq", "ddpm-lsun-celeba-hq-ema"]
811

9-
ddpm = DDPM.from_pretrained(model_id)
10-
image = ddpm()
12+
for model_id in model_ids:
1113

12-
import PIL.Image
13-
import numpy as np
14-
image_processed = image.cpu().permute(0, 2, 3, 1)
15-
image_processed = (image_processed + 1.0) * 127.5
16-
image_processed = image_processed.numpy().astype(np.uint8)
17-
image_pil = PIL.Image.fromarray(image_processed[0])
18-
image_pil.save("test.png")
14+
path = os.path.join("/home/patrick/images/hf", model_id)
15+
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
16+
17+
ddpm = DDPM.from_pretrained("fusing/" + model_id)
18+
image = ddpm(batch_size=4)
19+
20+
image_processed = image.cpu().permute(0, 2, 3, 1)
21+
image_processed = (image_processed + 1.0) * 127.5
22+
image_processed = image_processed.numpy().astype(np.uint8)
1923

20-
import ipdb; ipdb.set_trace()
24+
for i in range(image_processed.shape[0]):
25+
image_pil = PIL.Image.fromarray(image_processed[i])
26+
image_pil.save(os.path.join(path, f"image_{i}.png"))

models/vision/ddpm/modeling_ddpm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __call__(self, batch_size=1, generator=None, torch_device=None):
3333

3434
self.unet.to(torch_device)
3535
# 1. Sample gaussian noise
36-
image = self.noise_scheduler.sample_noise((1, self.unet.in_channels, self.unet.resolution, self.unet.resolution), device=torch_device, generator=generator)
36+
image = self.noise_scheduler.sample_noise((batch_size, self.unet.in_channels, self.unet.resolution, self.unet.resolution), device=torch_device, generator=generator)
3737
for t in tqdm.tqdm(reversed(range(len(self.noise_scheduler))), total=len(self.noise_scheduler)):
3838
# i) define coefficients for time step t
3939
clip_image_coeff = 1 / torch.sqrt(self.noise_scheduler.get_alpha_prod(t))

src/diffusers/schedulers/gaussian_ddpm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def get_alpha_prod(self, time_step):
108108

109109
def sample_variance(self, time_step, shape, device, generator=None):
110110
variance = self.log_variance[time_step]
111-
nonzero_mask = torch.tensor([1 - (time_step == 0)], device=device).float()[None, :].repeat(shape[0], 1)
111+
nonzero_mask = torch.tensor([1 - (time_step == 0)], device=device).float()[None, :]
112112

113113
noise = self.sample_noise(shape, device=device, generator=generator)
114114

tests/test_modeling_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def floats_tensor(shape, scale=1.0, rng=None, name=None):
7676
class ModelTesterMixin(unittest.TestCase):
7777
@property
7878
def dummy_input(self):
79-
batch_size = 1
79+
batch_size = 4
8080
num_channels = 3
8181
sizes = (32, 32)
8282

0 commit comments

Comments
 (0)