見出し画像

Google Colab で CogView4 を試す

「Google Colab」で「CogView4」を試したのでまとめました。


1. CogView4

CogView4」は、清華大学のTHUDMによって開発された、テキストから画像を生成するオープンソースAIモデルです。

特徴は次のとおりです。

・高品質
複雑かつ詳細なテキスト指示に忠実に応答し、写実的かつ芸術性の高い画像を生成します。2048×2048ピクセルなどの高解像度にも対応しています。
・バイリンガル
中国語と英語の両方の入力に対応しており、特に中国語プロンプトでの生成性能が高いことが評価されています。
・先進技術の統合
GLM-4系のテキストエンコーダやTransformerベースのDiffusionアーキテクチャ、2D RoPEやFlow-matchingなどの最新技術を統合し、従来の手法を大きく上回る性能を実現しています。
・オープンソース
Apache 2.0ライセンスのもとで公開されています。

2. モデル

「CogView4」のモデルは、次の1つが提供されています。

THUDM/CogView4-6B

3. 画像生成

「Google Colab」での画像生成の実行手順は、次のとおりです。

(1) パッケージのインストール。

# パッケージのインストール
!pip install git+https://github.com/huggingface/diffusers.git

(2) モデルの準備。
今回は、「THUDM/CogView4-6B」を使います。

from diffusers import CogView4Pipeline
import torch

# モデルの準備
pipe = CogView4Pipeline.from_pretrained(
    "THUDM/CogView4-6B",
    torch_dtype=torch.bfloat16
)
pipe.enable_model_cpu_offload()
pipe.vae.enable_slicing()
pipe.vae.enable_tiling()

(2) 画像生成。
プロンプトは「日本のアニメスタイルの猫耳メイドが手をふっている」をプロンプト拡張したものになります。1分で「cogview4.png」に出力されます。

# 画像生成
prompt = "This image is a vibrant, full-color illustration in the Japanese anime style, capturing the essence of kawaii (cuteness). At the center of the composition stands a charming maid character with distinctive cat ears perched atop her head, adding an adorable feline touch. Her ears are soft and fluffy, with subtle shading that gives them a lifelike appearance. The maid is dressed in a traditional French maid outfit, but with playful, anime-inspired twists. Her dress is a crisp white, adorned with delicate black lace trim and a cute, frilly apron. The outfit is accented with a small, black bow tie and a matching headband that holds her cat ears in place. Her hair is a cascade of gentle curls, falling in a soft, pastel shade that complements her overall look. Her face is animated with a warm, welcoming smile, and her large, expressive eyes sparkle with friendliness. One hand is raised in a cheerful wave, the fingers gracefully extended, while the other hand rests lightly on her hip, adding a touch of casual elegance to her pose. The background is a softly blurred, pastel-colored room, suggesting a cozy, inviting atmosphere. Gentle lighting bathes the scene, highlighting the maid's features and the intricate details of her outfit. The overall effect is one of endearing charm and whimsy, perfectly capturing the spirit of a cute cat-eared maid in a Japanese anime setting."
image = pipe(
    prompt=prompt,
    guidance_scale=3.5,
    num_images_per_prompt=1,
    num_inference_steps=50,
    width=1024,
    height=1024,
).images[0]
image.save("cogview4.png")

消費メモリは次のとおりです。

関連



いいなと思ったら応援しよう!