Skip to content

ClasCat® AI Research

クラスキャット – 生成 AI, AI エージェント, MCP

Menu
  • ホーム
    • ClassCat® AI Research ホーム
    • クラスキャット・ホーム
  • OpenAI API
    • OpenAI Python ライブラリ 1.x : 概要
    • OpenAI ブログ
      • GPT の紹介
      • GPT ストアの紹介
      • ChatGPT Team の紹介
    • OpenAI platform 1.x
      • Get Started : イントロダクション
      • Get Started : クイックスタート (Python)
      • Get Started : クイックスタート (Node.js)
      • Get Started : モデル
      • 機能 : 埋め込み
      • 機能 : 埋め込み (ユースケース)
      • ChatGPT : アクション – イントロダクション
      • ChatGPT : アクション – Getting started
      • ChatGPT : アクション – アクション認証
    • OpenAI ヘルプ : ChatGPT
      • ChatGPTとは何ですか?
      • ChatGPT は真実を語っていますか?
      • GPT の作成
      • GPT FAQ
      • GPT vs アシスタント
      • GPT ビルダー
    • OpenAI ヘルプ : ChatGPT > メモリ
      • FAQ
    • OpenAI ヘルプ : GPT ストア
      • 貴方の GPT をフィーチャーする
    • OpenAI Python ライブラリ 0.27 : 概要
    • OpenAI platform
      • Get Started : イントロダクション
      • Get Started : クイックスタート
      • Get Started : モデル
      • ガイド : GPT モデル
      • ガイド : 画像生成 (DALL·E)
      • ガイド : GPT-3.5 Turbo 対応 微調整
      • ガイド : 微調整 1.イントロダクション
      • ガイド : 微調整 2. データセットの準備 / ケーススタディ
      • ガイド : 埋め込み
      • ガイド : 音声テキスト変換
      • ガイド : モデレーション
      • ChatGPT プラグイン : イントロダクション
    • OpenAI Cookbook
      • 概要
      • API 使用方法 : レート制限の操作
      • API 使用方法 : tiktoken でトークンを数える方法
      • GPT : ChatGPT モデルへの入力をフォーマットする方法
      • GPT : 補完をストリームする方法
      • GPT : 大規模言語モデルを扱う方法
      • 埋め込み : 埋め込みの取得
      • GPT-3 の微調整 : 分類サンプルの微調整
      • DALL-E : DALL·E で 画像を生成して編集する方法
      • DALL·E と Segment Anything で動的マスクを作成する方法
      • Whisper プロンプティング・ガイド
  • Gemini API
    • Tutorials : クイックスタート with Python (1) テキスト-to-テキスト生成
    • (2) マルチモーダル入力 / 日本語チャット
    • (3) 埋め込みの使用
    • (4) 高度なユースケース
    • クイックスタート with Node.js
    • クイックスタート with Dart or Flutter (1) 日本語動作確認
    • Gemma
      • 概要 (README)
      • Tutorials : サンプリング
      • Tutorials : KerasNLP による Getting Started
  • Keras 3
    • 新しいマルチバックエンド Keras
    • Keras 3 について
    • Getting Started : エンジニアのための Keras 入門
    • Google Colab 上のインストールと Stable Diffusion デモ
    • コンピュータビジョン – ゼロからの画像分類
    • コンピュータビジョン – 単純な MNIST convnet
    • コンピュータビジョン – EfficientNet を使用した微調整による画像分類
    • コンピュータビジョン – Vision Transformer による画像分類
    • コンピュータビジョン – 最新の MLPモデルによる画像分類
    • コンピュータビジョン – コンパクトな畳込み Transformer
    • Keras Core
      • Keras Core 0.1
        • 新しいマルチバックエンド Keras (README)
        • Keras for TensorFlow, JAX, & PyTorch
        • 開発者ガイド : Getting started with Keras Core
        • 開発者ガイド : 関数型 API
        • 開発者ガイド : シーケンシャル・モデル
        • 開発者ガイド : サブクラス化で新しい層とモデルを作成する
        • 開発者ガイド : 独自のコールバックを書く
      • Keras Core 0.1.1 & 0.1.2 : リリースノート
      • 開発者ガイド
      • Code examples
      • Keras Stable Diffusion
        • 概要
        • 基本的な使い方 (テキスト-to-画像 / 画像-to-画像変換)
        • 混合精度のパフォーマンス
        • インペインティングの簡易アプリケーション
        • (参考) KerasCV – Stable Diffusion を使用した高性能画像生成
  • TensorFlow
    • TF 2 : 初級チュートリアル
    • TF 2 : 上級チュートリアル
    • TF 2 : ガイド
    • TF 1 : チュートリアル
    • TF 1 : ガイド
  • その他
    • 🦜️🔗 LangChain ドキュメント / ユースケース
    • Stable Diffusion WebUI
      • Google Colab で Stable Diffusion WebUI 入門
      • HuggingFace モデル / VAE の導入
      • LoRA の利用
    • Diffusion Models / 拡散モデル
  • クラスキャット
    • 会社案内
    • お問合せ
    • Facebook
    • ClassCat® Blog
Menu

FLUX.1 : AI Toolkit による FLUX.1 LoRA トレーニング (2) RunPod で実践

Posted on 08/29/202409/01/2024 by Masashi Okumura

AI Toolkit by Ostris の基本を抑えたので、今回は RunPod で LoRA トレーニングを実践してみます。
Google Colab でもまったく同様の手順でトレーニングできますが、A100 GPU でないと動作しないのが難点です。

FLUX.1 : AI Toolkit による FLUX.1 LoRA トレーニング (2) RunPod で実践

作成 : Masashi Okumura (@ClassCat)
作成日時 : 08/29/2024

* 本記事は github ostris/ai-toolkit の以下のページを参考にしています :

  • ostris/ai-toolkit/README.md

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

 

FLUX.1 : AI Toolkit による FLUX.1 LoRA トレーニング (2) RunPod で実践

前回 AI Toolkit by Ostris の基本 を抑えたので、今回は RunPod で LoRA トレーニングを実践してみます。

Google Colab でもまったく同様の手順でトレーニングできますが、A100 GPU でないと動作しないのが難点です。

 

RunPod

AI Toolkit by Ostris の README で推奨されている、ポッド仕様の例は以下です :

  • テンプレート: runpod/pytorch:2.2.0-py3.10-cuda12.1.1-devel-ubuntu22.04

  • 1x A40 (48 GB VRAM)
  • 19 vCPU 100 GB RAM
  • ~120 GB Disk
  • ~120 GB Pod Volume

  • Start Jupyter Notebook

GPU は最低 24GB VRAM あれば何でも良いです。
ディスク・ボリュームは単にトレーニングして推論するだけなら 120GB も必要ないです

 

トレーニングデータ

トレーニングデータは、定番ですが、東北ずんこ・ずんだもんプロジェクト の AI画像モデル用学習データ を使用させていただきました。

記載されている使用規約は :

  • 公式が配布する 東北3姉妹のイラストは非商用、もしくは東北企業であれば自由に使える そうです。
  • また、AI画像モデル用学習データについては :

    ずんずんPJ公式イラストを学習したデータのLoRAの配布はなどは大丈夫です(2次創作のデータが入る場合は2次創作者への配慮[2次創作者のガイドライン確認など]が別途必要になります)

 

実践

上記の仕様でポッドを起動し、Jupyter ノートブックでアクセスします。

 

ポッドの確認

最初にポッドの仕様を簡単に確認しましょう :

!nvidia-smi
Tue Aug 27 09:44:14 2024       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.161.08             Driver Version: 535.161.08   CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  NVIDIA A40                     On  | 00000000:53:00.0 Off |                    0 |
|  0%   29C    P8              21W / 300W |      0MiB / 46068MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|  No running processes found                                                           |
+---------------------------------------------------------------------------------------+
!free -h
               total        used        free      shared  buff/cache   available
Mem:           503Gi        73Gi        28Gi       5.2Gi       401Gi       421Gi
Swap:             0B          0B          0B

 

トレーニングデータの準備

次にトレーニングデータを準備します。前述の「東北ずんこ・ずんだもんプロジェクト」の AI 画像モデル用学習データから 東北イタコ のデータを使用しましたが、もちろんお好みでどうぞ。

zip ファイルをアップロードして解凍します :

!unzip -q /workspace/itako.zip

 

環境構築

AI Toolkit by Ostris のレポジトリを複製して、インストールします :

!git clone https://github.com/ostris/ai-toolkit.git
%cd /workspace/ai-toolkit
!git submodule update --init --recursive
!pip install -U pip
!pip install -r requirements.txt

config ファイルはサンプルの train_lora_flux_24gb.yaml をコピーして使用します :

!cp -p /workspace/ai-toolkit/config/examples/train_lora_flux_24gb.yaml \
  /workspace/ai-toolkit/config/itako.yaml

書き換えが必須なのは以下の 2 箇所だけです :

  • トレーニングデータのパス
          datasets:
            # datasets are a folder of images. captions need to be txt files with the same name as the image
            # for instance image2.jpg and image2.txt. Only jpg, jpeg, and png are supported currently
            # images will automatically be resized and bucketed into the resolution specified
            # on windows, escape back slashes with another backslash so
            # "C:\\path\\to\\images\\folder"
            - folder_path: "/path/to/images/folder"
    

     

  • トレーニング中にサンプル画像を生成するためのプロンプト
          sample:
            sampler: "flowmatch" # must match train.noise_scheduler
            sample_every: 250 # sample every this many steps
            width: 1024
            height: 1024
            prompts:
              # you can add [trigger] to the prompts here and it will be replaced with the trigger word
    #          - "[trigger] holding a sign that says 'I LOVE PROMPTS!'"\
              - "woman with red hair, playing chess at the park, bomb going off in the background"
              - "a woman holding a coffee cup, in a beanie, sitting at a cafe"
              - "a horse is a DJ at a night club, fish eye lens, smoke machine, lazer lights, holding a martini"
              - "a man showing off his cool new t shirt at the beach, a shark is jumping out of the water in the background"
              - "a bear building a log cabin in the snow covered mountains"
              - "woman playing the guitar, on stage, singing a song, laser lights, punk rocker"
              - "hipster man with a beard, building a chair, in a wood shop"
              - "photo of a man, white background, medium shot, modeling clothing, studio lighting, white backdrop"
              - "a man holding a sign that says, 'this is a sign'"
              - "a bulldog, in a post apocalyptic world, with a shotgun, in a leather jacket, in a desert, with a motorcycle"
    

それから、Hugging Face のアクセストークンを含む .env ファイルをルートに配置しておくと、”black-forest-labs/FLUX.1-dev” を自動的にダウンロードしてくれます :

import os
hf_token = os.getenv('HF_TOKEN')

with open('/workspace/ai-toolkit/.env', 'w') as f:
    f.write(f"HF_TOKEN={hf_token}")

※ HF_TOKEN はポッド起動時に設定しておいた環境変数です。

 

トレーニング

それではトレーニングしてみましょう。デフォルトの 2,000 ステップ実行しましたが、1,000 〜 1,500 ステップくらいでも十分です :

%%time

%cd /workspace/ai-toolkit

!python run.py /workspace/ai-toolkit/config/itako.yaml

 
0 ステップ

 
500 ステップ

 
1,000 ステップ

 
2,000 ステップ

 

推論

トレーニングが完了したら、サンプリングしてみます :

import torch
from diffusers import  FluxPipeline

pipe = FluxPipeline.from_pretrained(
    "black-forest-labs/FLUX.1-dev",
    torch_dtype=torch.bfloat16,
    token=hf_token,
)
pipe.enable_model_cpu_offload()
pipe.load_lora_weights("/workspace/ai-toolkit/output/my_first_flux_lora_v1/my_first_flux_lora_v1.safetensors")
%%time

prompt = "itako, 1girl, solo, kimono, hagoromo, tabi, animal ear fluff, hair ornament, dancing"

out = pipe(
    prompt=prompt,
    guidance_scale=3.5,
    width=1024,
    height=1024,
    num_inference_steps=40,
).images[0]

out.save("/workspace/image.png")

 

以上



クラスキャット

最近の投稿

  • LangGraph Platform : Get started : クイックスタート
  • LangGraph Platform : 概要
  • LangGraph : Prebuilt エージェント : ユーザインターフェイス
  • LangGraph : Prebuilt エージェント : 配備
  • LangGraph : Prebuilt エージェント : マルチエージェント

タグ

AutoGen (13) ClassCat Press Release (20) ClassCat TF/ONNX Hub (11) DGL 0.5 (14) Eager Execution (7) Edward (17) FLUX.1 (16) Gemini (20) HuggingFace Transformers 4.5 (10) HuggingFace Transformers 4.6 (7) HuggingFace Transformers 4.29 (9) Keras 2 Examples (98) Keras 2 Guide (16) Keras 3 (10) Keras Release Note (17) Kubeflow 1.0 (10) LangChain (45) LangGraph (20) MediaPipe 0.8 (11) Model Context Protocol (16) NNI 1.5 (16) OpenAI Agents SDK (8) OpenAI Cookbook (13) OpenAI platform (10) OpenAI platform 1.x (10) OpenAI ヘルプ (8) TensorFlow 2.0 Advanced Tutorials (33) TensorFlow 2.0 Advanced Tutorials (Alpha) (15) TensorFlow 2.0 Advanced Tutorials (Beta) (16) TensorFlow 2.0 Guide (10) TensorFlow 2.0 Guide (Alpha) (16) TensorFlow 2.0 Guide (Beta) (9) TensorFlow 2.0 Release Note (12) TensorFlow 2.0 Tutorials (20) TensorFlow 2.0 Tutorials (Alpha) (14) TensorFlow 2.0 Tutorials (Beta) (12) TensorFlow 2.4 Guide (24) TensorFlow Deploy (8) TensorFlow Get Started (7) TensorFlow Graphics (7) TensorFlow Probability (9) TensorFlow Programmer's Guide (22) TensorFlow Release Note (18) TensorFlow Tutorials (33) TF-Agents 0.4 (11)
2024年8月
月 火 水 木 金 土 日
 1234
567891011
12131415161718
19202122232425
262728293031  
« 7月   9月 »
© 2025 ClasCat® AI Research | Powered by Minimalist Blog WordPress Theme