当前位置: 首页 > news >正文

【AutoencoderKL】基于stable-diffusion-v1.4的vae对图像重构

模型地址:https://huggingface.co/CompVis/stable-diffusion-v1-4/tree/main/vae
主要参考:Using-Stable-Diffusion-VAE-to-encode-satellite-images
在这里插入图片描述

sd1.4 vae

下载到本地

from diffusers import AutoencoderKL
from PIL import Image
import  torch
import torchvision.transforms as T#  ./huggingface/stable-diffusion-v1-4/vae 切换为任意本地路径
vae = AutoencoderKL.from_pretrained("./huggingface/stable-diffusion-v1-4/vae",variant='fp16')
# c:\Users\zeng\Downloads\vae_config.jsondef encode_img(input_img):# Single image -> single latent in a batch (so size 1, 4, 64, 64)# Transform the image to a tensor and normalize ittransform = T.Compose([# T.Resize((256, 256)),T.ToTensor()])input_img = transform(input_img)if len(input_img.shape)<4:input_img = input_img.unsqueeze(0)with torch.no_grad():latent = vae.encode(input_img*2 - 1) # Note scalingreturn 0.18215 * latent.latent_dist.sample()def decode_img(latents):# bath of latents -> list of imageslatents = (1 / 0.18215) * latentswith torch.no_grad():image = vae.decode(latents).sampleimage = (image / 2 + 0.5).clamp(0, 1)image = image.detach().cpu()# image = T.Resize(original_size)(image.squeeze())return T.ToPILImage()(image.squeeze())if __name__ == '__main__':# Load an example imageinput_img = Image.open("huge.jpg")original_size = input_img.sizeprint('original_size',original_size)# Encode and decode the imagelatents = encode_img(input_img)reconstructed_img = decode_img(latents)# Save the reconstructed imagereconstructed_img.save("reconstructed_example2.jpg")# Concatenate the original and reconstructed imagesconcatenated_img = Image.new('RGB', (original_size[0] * 2, original_size[1]))concatenated_img.paste(input_img, (0, 0))concatenated_img.paste(reconstructed_img, (original_size[0], 0))# Save the concatenated imageconcatenated_img.save("concatenated_example2.jpg")
http://www.lryc.cn/news/395167.html

相关文章:

  • 《警世贤文》摘抄:守法篇、惜时篇、修性篇、修身篇、待人篇、防人篇(建议多读书、多看报、少吃零食多睡觉)
  • vue2+element-ui新增编辑表格+删除行
  • Day05-组织架构-角色管理
  • 【LLM】二、python调用本地的ollama部署的大模型
  • 20240708 每日AI必读资讯
  • 为什么KV Cache只需缓存K矩阵和V矩阵,无需缓存Q矩阵?
  • VS code修改底部的行号的状态栏颜色
  • 【鸿蒙学习笔记】MVVM模式
  • 端、边、云三级算力网络
  • java —— JSP 技术
  • 【Python学习笔记】菜鸟教程Scrapy案例 + B站amazon案例视频
  • Pycharm的终端(Terminal)中切换到当前项目所在的虚拟环境
  • Nginx 高效加速策略:动静分离与缓存详解
  • Unity3D 游戏摇杆的制作与实现详解
  • 从nginx返回404来看http1.0和http1.1的区别
  • MySQL 代理层:ProxySQL
  • 异步主从复制
  • 论文解析——Full Stack Optimization of Transformer Inference: a Survey
  • selenium处理cookie问题实战
  • (十五)GLM库对矩阵操作
  • android中activity与fragment之间的各种跳转
  • 动态规划算法-以中学排课管理系统为例
  • 本安防爆手机:危险环境下的安全通信解决方案
  • 算法学习笔记(8)-动态规划基础篇
  • 数据库常见问题(持续更新)
  • 定个小目标之刷LeetCode热题(40)
  • Linux--线程(概念篇)
  • Mojo: 轻量级Perl框架的魔力
  • Python 游戏服务器架构优化
  • 13 学习总结:指针 · 其一