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