This commit is contained in:
Patrick von Platen 2022-06-06 16:22:00 +00:00
commit 6f75ef12d4
1 changed files with 15 additions and 16 deletions

View File

@ -67,10 +67,13 @@ for t in reversed(range(len(scheduler))):
sampled_prev_image = prev_image + prev_variance
image = sampled_prev_image
# process image to PIL
image_processed = image.cpu().permute(0, 2, 3, 1)
image_processed = (image_processed + 1.0) * 127.5
image_processed = image_processed.numpy().astype(np.uint8)
image_pil = PIL.Image.fromarray(image_processed[0])
# save image
image_pil.save("test.png")
```
@ -80,28 +83,24 @@ image_pil.save("test.png")
Example:
```python
from diffusers import UNetModel, GaussianDDPMScheduler
from modeling_ddpm import DDPM
import tempfile
import PIL.Image
import numpy as np
unet = UNetModel.from_pretrained("fusing/ddpm_dummy")
sampler = GaussianDDPMScheduler.from_config("fusing/ddpm_dummy")
# load model and scheduler
ddpm = DDPM.from_pretrained("fusing/ddpm-lsun-bedroom-pipe")
# compose Diffusion Pipeline
ddpm = DDPM(unet, sampler)
# generate / sample
# run pipeline in inference (sample random noise and denoise)
image = ddpm()
print(image)
# process image to PIL
image_processed = image.cpu().permute(0, 2, 3, 1)
image_processed = (image_processed + 1.0) * 127.5
image_processed = image_processed.numpy().astype(np.uint8)
image_pil = PIL.Image.fromarray(image_processed[0])
# save and load with 0 extra code (handled by general `DiffusionPipeline` class)
# will also be possible to do so from the Hub
with tempfile.TemporaryDirectory() as tmpdirname:
ddpm.save_pretrained(tmpdirname)
print("Model saved")
ddpm_new = DDPM.from_pretrained(tmpdirname)
print("Model loaded")
print(ddpm_new)
# save image
image_pil.save("test.png")
```
## Library structure: