扩散模型DDPM代码实践
安装diffusers
pip install diffusers
按照diffusers官方代码
from diffusers import DDPMPipeline
pipe = DDPMPipeline.from_pretrained("google/ddpm-cat-256")
image = pipe().images[0]
image.save("/data/zhz/projects/diffusion/output/ddpm_generated_image.png")
准备 google/ddpm-cat-256/
因为服务器连不了外网所以只能手动下载:
https://huggingface.co/google/ddpm-cat-256/tree/main
文件结构:
同时代码也要修改为:
from diffusers import DDPMPipeline
# pipe = DDPMPipeline.from_pretrained("google/ddpm-cat-256")
pipe = DDPMPipeline.from_pretrained("/data/zhz/projects/diffusion/ddpm-cat-256")
image = pipe().images[0]
image.save("/data/zhz/projects/diffusion/output/ddpm_generated_image.png")
运行
python /data/zhz/projects/diffusion/test_20240929.py
等待进度条
如果是换成gpu的话:
from diffusers import DDPMPipeline
# pipe = DDPMPipeline.from_pretrained("google/ddpm-cat-256")
pipe = DDPMPipeline.from_pretrained("/data/zhz/projects/diffusion/ddpm-cat-256").to("cuda")
image = pipe().images[0]
image.save("/data/zhz/projects/diffusion/output/ddpm_generated_image.png")
显存开销3G左右:
最后生成图片: