CSGO
CSGO是一个用于文本生成图像的PyTorch实现,支持图像驱动的风格迁移、文本驱动的风格化合成和文本编辑驱动的风格化合成。
下载量 500
发布时间 : 8/30/2024
模型简介
CSGO是一个先进的文本生成图像模型,能够实现内容与风格的灵活组合,支持多种风格迁移和合成任务。
模型特点
内容-风格组合
支持灵活的内容和风格标记组合,实现多样化的图像生成效果。
多模式生成
支持图像驱动的风格迁移、文本驱动的风格化合成和文本编辑驱动的风格化合成。
兼容性
与SDXL、VAE、ControlNet和图像编码器完全兼容。
模型能力
文本生成图像
图像风格迁移
文本驱动的风格合成
文本编辑驱动的风格合成
使用案例
创意设计
图像风格迁移
将一张图像的风格迁移到另一张图像上,保留内容特征。
生成具有目标风格的内容图像
文本驱动的风格合成
根据文本描述生成具有特定风格的图像。
生成符合文本描述的风格化图像
图像编辑
文本编辑驱动的风格合成
通过编辑文本描述来调整生成图像的风格。
生成根据文本编辑调整后的风格化图像
🚀 CSGO:文本到图像生成中的内容 - 风格组合
本项目 CSGO 是论文 CSGO: Content-Style Composition in Text-to-Image Generation 的官方 PyTorch 实现。我们持续更新和改进此仓库,若您发现任何问题或有建议,欢迎提出问题或提交拉取请求(PR)💖。
🚀 快速开始
1. 克隆代码并准备环境
git clone https://github.com/instantX-research/CSGO
cd CSGO
# 使用 conda 创建环境
conda create -n CSGO python=3.9
conda activate CSGO
# 使用 pip 安装依赖
# 适用于 Linux 和 Windows 用户
pip install -r requirements.txt
2. 下载预训练权重(即将推出)
从 HuggingFace 下载预训练权重是最简单的方法:
# 首先,确保安装了 git-lfs,详见:https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage
git lfs install
# 克隆并移动权重
git clone https://huggingface.co/InstantX/CSGO
我们的方法与 SDXL、VAE、ControlNet 和 图像编码器 完全兼容。请将它们下载并放置在 ./base_models
文件夹中。
3. 推理
import torch
from ip_adapter.utils import resize_content
import numpy as np
from ip_adapter.utils import BLOCKS as BLOCKS
from ip_adapter.utils import controlnet_BLOCKS as controlnet_BLOCKS
from PIL import Image
from diffusers import (
AutoencoderKL,
ControlNetModel,
StableDiffusionXLControlNetPipeline,
)
from ip_adapter import CSGO
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
base_model_path = "./base_models/stable-diffusion-xl-base-1.0"
image_encoder_path = "./base_models/IP-Adapter/sdxl_models/image_encoder"
csgo_ckpt = "./CSGO/csgo.bin"
pretrained_vae_name_or_path ='./base_models/sdxl-vae-fp16-fix'
controlnet_path = "./base_models/TTPLanet_SDXL_Controlnet_Tile_Realistic"
weight_dtype = torch.float16
vae = AutoencoderKL.from_pretrained(pretrained_vae_name_or_path,torch_dtype=torch.float16)
controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16,use_safetensors=True)
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
base_model_path,
controlnet=controlnet,
torch_dtype=torch.float16,
add_watermarker=False,
vae=vae
)
pipe.enable_vae_tiling()
target_content_blocks = BLOCKS['content']
target_style_blocks = BLOCKS['style']
controlnet_target_content_blocks = controlnet_BLOCKS['content']
controlnet_target_style_blocks = controlnet_BLOCKS['style']
csgo = CSGO(pipe, image_encoder_path, csgo_ckpt, device, num_content_tokens=4,num_style_tokens=32,
target_content_blocks=target_content_blocks, target_style_blocks=target_style_blocks,controlnet_adapter=True,
controlnet_target_content_blocks=controlnet_target_content_blocks,
controlnet_target_style_blocks=controlnet_target_style_blocks,
content_model_resampler=True,
style_model_resampler=True,
)
style_name = 'img_1.png'
content_name = 'img_0.png'
style_image = Image.open("../assets/{}".format(style_name)).convert('RGB')
content_image = Image.open('../assets/{}'.format(content_name)).convert('RGB')
caption ='a small house with a sheep statue on top of it'
num_sample=4
# 图像驱动的风格迁移
images = csgo.generate(pil_content_image= content_image, pil_style_image=style_image,
prompt=caption,
negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
content_scale=1.0,
style_scale=1.0,
guidance_scale=10,
num_images_per_prompt=num_sample,
num_samples=1,
num_inference_steps=50,
seed=42,
image=content_image.convert('RGB'),
controlnet_conditioning_scale=0.6,
)
# 文本编辑驱动的风格合成
caption='a small house'
images = csgo.generate(pil_content_image= content_image, pil_style_image=style_image,
prompt=caption,
negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
content_scale=1.0,
style_scale=1.0,
guidance_scale=10,
num_images_per_prompt=num_sample,
num_samples=1,
num_inference_steps=50,
seed=42,
image=content_image.convert('RGB'),
controlnet_conditioning_scale=0.4,
)
# 文本驱动的风格合成
caption='a cat'
# 如果内容图像仍对生成结果有干扰,可将内容图像设置为空图像。
# content_image =Image.fromarray(np.zeros((content_image.size[0],content_image.size[1], 3), dtype=np.uint8)).convert('RGB')
images = csgo.generate(pil_content_image= content_image, pil_style_image=style_image,
prompt=caption,
negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
content_scale=1.0,
style_scale=1.0,
guidance_scale=10,
num_images_per_prompt=num_sample,
num_samples=1,
num_inference_steps=50,
seed=42,
image=content_image.convert('RGB'),
controlnet_conditioning_scale=0.01,
)
✨ 主要特性
- 我们的 CSGO 实现了 图像驱动的风格迁移、文本驱动的风格化合成以及文本编辑驱动的风格化合成。
- 更多结果请访问我们的 主页 🔥
📦 安装指南
克隆代码并准备环境
git clone https://github.com/instantX-research/CSGO
cd CSGO
# 使用 conda 创建环境
conda create -n CSGO python=3.9
conda activate CSGO
# 使用 pip 安装依赖
# 适用于 Linux 和 Windows 用户
pip install -r requirements.txt
下载预训练权重(即将推出)
从 HuggingFace 下载预训练权重是最简单的方法:
# 首先,确保安装了 git-lfs,详见:https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage
git lfs install
# 克隆并移动权重
git clone https://huggingface.co/InstantX/CSGO
💻 使用示例
基础用法
# 克隆代码并准备环境
git clone https://github.com/instantX-research/CSGO
cd CSGO
# 使用 conda 创建环境
conda create -n CSGO python=3.9
conda activate CSGO
# 使用 pip 安装依赖
# 适用于 Linux 和 Windows 用户
pip install -r requirements.txt
# 下载预训练权重(即将推出)
# 首先,确保安装了 git-lfs,详见:https://docs.github.com/en/repositories/working-with-files/managing-large-files/installing-git-large-file-storage
git lfs install
# 克隆并移动权重
git clone https://huggingface.co/InstantX/CSGO
# 推理代码
import torch
from ip_adapter.utils import resize_content
import numpy as np
from ip_adapter.utils import BLOCKS as BLOCKS
from ip_adapter.utils import controlnet_BLOCKS as controlnet_BLOCKS
from PIL import Image
from diffusers import (
AutoencoderKL,
ControlNetModel,
StableDiffusionXLControlNetPipeline,
)
from ip_adapter import CSGO
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
base_model_path = "./base_models/stable-diffusion-xl-base-1.0"
image_encoder_path = "./base_models/IP-Adapter/sdxl_models/image_encoder"
csgo_ckpt = "./CSGO/csgo.bin"
pretrained_vae_name_or_path ='./base_models/sdxl-vae-fp16-fix'
controlnet_path = "./base_models/TTPLanet_SDXL_Controlnet_Tile_Realistic"
weight_dtype = torch.float16
vae = AutoencoderKL.from_pretrained(pretrained_vae_name_or_path,torch_dtype=torch.float16)
controlnet = ControlNetModel.from_pretrained(controlnet_path, torch_dtype=torch.float16,use_safetensors=True)
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
base_model_path,
controlnet=controlnet,
torch_dtype=torch.float16,
add_watermarker=False,
vae=vae
)
pipe.enable_vae_tiling()
target_content_blocks = BLOCKS['content']
target_style_blocks = BLOCKS['style']
controlnet_target_content_blocks = controlnet_BLOCKS['content']
controlnet_target_style_blocks = controlnet_BLOCKS['style']
csgo = CSGO(pipe, image_encoder_path, csgo_ckpt, device, num_content_tokens=4,num_style_tokens=32,
target_content_blocks=target_content_blocks, target_style_blocks=target_style_blocks,controlnet_adapter=True,
controlnet_target_content_blocks=controlnet_target_content_blocks,
controlnet_target_style_blocks=controlnet_target_style_blocks,
content_model_resampler=True,
style_model_resampler=True,
)
style_name = 'img_1.png'
content_name = 'img_0.png'
style_image = Image.open("../assets/{}".format(style_name)).convert('RGB')
content_image = Image.open('../assets/{}'.format(content_name)).convert('RGB')
caption ='a small house with a sheep statue on top of it'
num_sample=4
# 图像驱动的风格迁移
images = csgo.generate(pil_content_image= content_image, pil_style_image=style_image,
prompt=caption,
negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
content_scale=1.0,
style_scale=1.0,
guidance_scale=10,
num_images_per_prompt=num_sample,
num_samples=1,
num_inference_steps=50,
seed=42,
image=content_image.convert('RGB'),
controlnet_conditioning_scale=0.6,
)
高级用法
# 文本编辑驱动的风格合成
caption='a small house'
images = csgo.generate(pil_content_image= content_image, pil_style_image=style_image,
prompt=caption,
negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
content_scale=1.0,
style_scale=1.0,
guidance_scale=10,
num_images_per_prompt=num_sample,
num_samples=1,
num_inference_steps=50,
seed=42,
image=content_image.convert('RGB'),
controlnet_conditioning_scale=0.4,
)
# 文本驱动的风格合成
caption='a cat'
# 如果内容图像仍对生成结果有干扰,可将内容图像设置为空图像。
# content_image =Image.fromarray(np.zeros((content_image.size[0],content_image.size[1], 3), dtype=np.uint8)).convert('RGB')
images = csgo.generate(pil_content_image= content_image, pil_style_image=style_image,
prompt=caption,
negative_prompt= "text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry",
content_scale=1.0,
style_scale=1.0,
guidance_scale=10,
num_images_per_prompt=num_sample,
num_samples=1,
num_inference_steps=50,
seed=42,
image=content_image.convert('RGB'),
controlnet_conditioning_scale=0.01,
)
📚 详细文档
模型细节
我们目前发布了两个模型权重。
属性 | 详情 |
---|---|
模型类型 | 我们目前发布了两个模型权重,分别为 csgo.bin 、csgo_4_32.bin 和 csgo_4_32_v2.bin |
内容令牌 | csgo.bin 为 4;csgo_4_32.bin 和 csgo_4_32_v2.bin 为 4 |
风格令牌 | csgo.bin 为 16;csgo_4_32.bin 和 csgo_4_32_v2.bin 为 32 |
其他 | csgo.bin 无;csgo_4_32.bin 采用 Deepspeed zero2;csgo_4_32_v2.bin 采用 Deepspeed zero2 + 更多(即将推出) |
管道流程
功能展示
演示示例
内容 - 风格组合
循环翻译
文本驱动的风格合成
文本编辑驱动的风格合成
🔧 技术细节
本项目使用 PyTorch 实现,与 SDXL、VAE、ControlNet 和 图像编码器 完全兼容。
📄 许可证
本项目采用 Apache-2.0 许可证。
致谢
本项目由 InstantX 团队开发,保留所有版权。
引用 💖
如果您发现 CSGO 对您的研究有用,欢迎给此仓库加星 🌟 并使用以下 BibTeX 引用我们的工作:
@article{xing2024csgo,
title={CSGO: Content-Style Composition in Text-to-Image Generation},
author={Peng Xing and Haofan Wang and Yanpeng Sun and Qixun Wang and Xu Bai and Hao Ai and Renyuan Huang and Zechao Li},
year={2024},
journal = {arXiv 2408.16766},
}
仓库 Star 历史
Stable Diffusion V1 5
Openrail
稳定扩散是一种潜在的文本到图像扩散模型,能够根据任何文本输入生成逼真的图像。
图像生成
S
stable-diffusion-v1-5
3.7M
518
Stable Diffusion Inpainting
Openrail
基于稳定扩散的文本到图像生成模型,具备图像修复能力
图像生成
S
stable-diffusion-v1-5
3.3M
56
Stable Diffusion Xl Base 1.0
SDXL 1.0是基于扩散的文本生成图像模型,采用专家集成的潜在扩散流程,支持高分辨率图像生成
图像生成
S
stabilityai
2.4M
6,545
Stable Diffusion V1 4
Openrail
稳定扩散是一种潜在文本到图像扩散模型,能够根据任意文本输入生成逼真图像。
图像生成
S
CompVis
1.7M
6,778
Stable Diffusion Xl Refiner 1.0
SD-XL 1.0优化器模型是Stability AI开发的图像生成模型,专为提升SDXL基础模型生成的图像质量而设计,特别擅长最终去噪步骤处理。
图像生成
S
stabilityai
1.1M
1,882
Stable Diffusion 2 1
基于扩散的文本生成图像模型,支持通过文本提示生成和修改图像
图像生成
S
stabilityai
948.75k
3,966
Stable Diffusion Xl 1.0 Inpainting 0.1
基于Stable Diffusion XL的潜在文本到图像扩散模型,具备通过遮罩进行图像修复的功能
图像生成
S
diffusers
673.14k
334
Stable Diffusion 2 Base
基于扩散的文生图模型,可根据文本提示生成高质量图像
图像生成
S
stabilityai
613.60k
349
Playground V2.5 1024px Aesthetic
其他
开源文生图模型,能生成1024x1024分辨率及多种纵横比的美学图像,在美学质量上处于开源领域领先地位。
图像生成
P
playgroundai
554.94k
723
Sd Turbo
SD-Turbo是一款高速文本生成图像模型,仅需单次网络推理即可根据文本提示生成逼真图像。该模型作为研究原型发布,旨在探索小型蒸馏文本生成图像模型。
图像生成
S
stabilityai
502.82k
380
精选推荐AI模型
Llama 3 Typhoon V1.5x 8b Instruct
专为泰语设计的80亿参数指令模型,性能媲美GPT-3.5-turbo,优化了应用场景、检索增强生成、受限生成和推理任务
大型语言模型
Transformers 支持多种语言

L
scb10x
3,269
16
Cadet Tiny
Openrail
Cadet-Tiny是一个基于SODA数据集训练的超小型对话模型,专为边缘设备推理设计,体积仅为Cosmo-3B模型的2%左右。
对话系统
Transformers 英语

C
ToddGoldfarb
2,691
6
Roberta Base Chinese Extractive Qa
基于RoBERTa架构的中文抽取式问答模型,适用于从给定文本中提取答案的任务。
问答系统 中文
R
uer
2,694
98
智启未来,您的人工智能解决方案智库
简体中文