模型简介
模型特点
模型能力
使用案例
🚀 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
许可证。









