🚀 iGPT-fr
iGPT-fr 🇫🇷は、Laboratoire de Linguistique Formelle (LLF)によって開発されたフランス語用のGPTモデルで、事前学習された漸進的言語モデルです。このモデルは、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
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)
model = GPT2LMHeadModel.from_pretrained("asi/igpt-fr-cased-base")
model.eval()
tokenizer = GPT2Tokenizer.from_pretrained("asi/igpt-fr-cased-base")
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)
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)
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
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)
📚 ドキュメント
学習データ
生成モデルを学習するために専用のコーパスを作成しました。学習コーパスはテキストと画像のペアで構成されています。既存のコーパス Laion-5B と WIT の一部を集約しています。最終的なデータセットには10,807,534個のサンプルが含まれています。
学習手順
このモデルは、新しいCNRS(フランス国立科学研究センター)の Jean Zay スーパーコンピュータで事前学習されました。Tesla V-100ハードウェア(TDP 300W)で合計140時間の計算を行いました。学習は8つのGPUを搭載した8つのコンピュートノードで分散されて行われました。マイクロバッチをコンピューティングユニットに分割するためにデータ並列化を使用しました。Lacoste et al., (2019) で提示された Machine Learning Impact calculator を使用して、総排出量は1161.22 kgCO2eqと推定されました。
📄 ライセンス
このモデルは apache-2.0
ライセンスの下で提供されています。