模型概述
模型特點
模型能力
使用案例
🚀 iGPT-fr 圖像生成模型
iGPT-fr 是一款專為法語設計的GPT模型,由 形式語言學實驗室 (LLF) 開發的預訓練增量語言模型。本模型將 GPT-fr 🇫🇷 模型進行了適配,能夠根據文本輸入生成圖像。
🚀 快速開始
安裝依賴
模型可以通過 🤗 Transformers
庫使用。同時,為了進行高分辨率圖像合成,你還需要安裝 Taming Transformers
庫:
pip install git+https://github.com/CompVis/taming-transformers.git
基礎用法
from transformers import GPT2Tokenizer, GPT2LMHeadModel
from huggingface_hub import hf_hub_download
from omegaconf import OmegaConf
from taming.models import vqgan
import torch
from PIL import Image
import numpy as np
# Load VQGAN model
vqgan_ckpt = hf_hub_download(repo_id="boris/vqgan_f16_16384", filename="model.ckpt", force_download=False)
vqgan_config = hf_hub_download(repo_id="boris/vqgan_f16_16384", filename="config.yaml", force_download=False)
config = OmegaConf.load(vqgan_config)
vqgan_model = vqgan.VQModel(**config.model.params)
vqgan_model.eval().requires_grad_(False)
vqgan_model.init_from_ckpt(vqgan_ckpt)
# Load pretrained model
model = GPT2LMHeadModel.from_pretrained("asi/igpt-fr-cased-base")
model.eval()
tokenizer = GPT2Tokenizer.from_pretrained("asi/igpt-fr-cased-base")
# Generate a sample of text
input_sentence = "Une carte de l'europe"
input_ids = tokenizer.encode(input_sentence, return_tensors='pt')
input_ids = torch.cat((input_ids, torch.tensor([[50000]])), 1) # Add image generation token
greedy_output = model.generate(
input_ids.to(device),
max_length=256+input_ids.shape[1],
do_sample=True,
top_p=0.92,
top_k=0)
def custom_to_pil(x):
x = x.detach().cpu()
x = torch.clamp(x, -1., 1.)
x = (x + 1.)/2.
x = x.permute(1,2,0).numpy()
x = (255*x).astype(np.uint8)
x = Image.fromarray(x)
if not x.mode == "RGB":
x = x.convert("RGB")
return x
z_idx = greedy_output[0, input_ids.shape[1]:] - 50001
z_quant = vqgan_model.quantize.get_codebook_entry(z_idx, shape=(1, 16, 16, 256))
x_rec = vqgan_model.decode(z_quant).to('cpu')[0]
display(custom_to_pil(x_rec))
高級用法
你還可以基於CLIP對結果進行過濾:
from tqdm import tqdm
def hallucinate(prompt, num_images=64):
input_ids = tokenizer.encode(prompt, return_tensors='pt')
input_ids = torch.cat((input_ids, torch.tensor([[50000]])), 1).to(device) # Add image generation token
all_images = []
for i in tqdm(range(num_images)):
greedy_output = model.generate(
input_ids.to(device),
max_length=256+input_ids.shape[1],
do_sample=True,
top_p=0.92,
top_k=0)
z_idx = greedy_output[0, input_ids.shape[1]:] - 50001
z_quant = vqgan_model.quantize.get_codebook_entry(z_idx, shape=(1, 16, 16, 256))
x_rec = vqgan_model.decode(z_quant).to('cpu')[0]
all_images.append(custom_to_pil(x_rec))
return all_images
input_sentence = "Une carte de l'europe"
all_images = hallucinate(input_sentence)
from transformers import pipeline
opus_model = "Helsinki-NLP/opus-mt-fr-en"
opus_translator = pipeline("translation", model=opus_model)
opus_translator(input_sentence)
from transformers import CLIPProcessor, CLIPModel
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
def clip_top_k(prompt, images, k=8):
prompt_fr = opus_translator(input_sentence)[0]['translation_text']
inputs = clip_processor(text=prompt_fr, images=images, return_tensors="pt", padding=True)
outputs = clip_model(**inputs)
logits = outputs.logits_per_text # this is the image-text similarity score
scores = np.array(logits[0].detach()).argsort()[-k:][::-1]
return [images[score] for score in scores]
filtered_images = clip_top_k(input_sentence, all_images)
for fi in filtered_images:
display(fi)
✨ 主要特性
- 基於法語預訓練的GPT模型,能夠根據文本輸入生成圖像。
- 可以通過
Transformers
庫方便地使用。 - 支持使用
Taming Transformers
庫進行高分辨率圖像合成。 - 可以基於CLIP對生成的圖像結果進行過濾。
📦 安裝指南
安裝 Taming Transformers
庫:
pip install git+https://github.com/CompVis/taming-transformers.git
📚 詳細文檔
模型描述
iGPT-fr 🇫🇷 是一款法語的GPT模型,由 形式語言學實驗室 (LLF) 開發的預訓練增量語言模型。我們對 GPT-fr 🇫🇷 模型進行了適配,使其能夠根據文本輸入生成圖像。
預期用途與限制
該模型可用於圖像生成任務。目前該模型仍處於開發階段。
訓練數據
我們創建了一個專門的語料庫來訓練生成模型。訓練語料庫由文本 - 圖像對組成。我們從現有語料庫 Laion - 5B 和 WIT 中聚合了部分數據。最終數據集包含 10,807,534 個樣本。
訓練過程
我們在法國國家科學研究中心(CNRS)的 Jean Zay 超級計算機上對模型進行了預訓練。在Tesla V - 100硬件(TDP為300W)上進行了總共140小時的計算訓練。訓練分佈在8個計算節點(每個節點8個GPU)上進行。我們使用數據並行化將每個微批次分配到計算單元上。使用 Lacoste等人(2019) 提出的 機器學習影響計算器 估計,總排放量為1161.22 kgCO2eq。
🔧 技術細節
本模型基於GPT架構進行開發,通過對 GPT-fr 🇫🇷 模型進行適配,使其能夠處理文本輸入並生成對應的圖像。在訓練過程中,使用了專門構建的文本 - 圖像對語料庫,結合Tesla V - 100硬件和數據並行化技術進行訓練。同時,為了實現高分辨率圖像合成,引入了 Taming Transformers
庫,並可以使用CLIP對生成的圖像結果進行過濾。
📄 許可證
本項目採用 apache-2.0
許可證。









