キャプションファイルとタグファイルをJsonファイルにまとめる - Stable Diffusion

学習用にキャプションファイルとタグファイルをJsonファイルにまとめる手順を紹介します。

概要

こちらの記事で画像からキャプション、キャプションファイルを作成する手順を紹介しました。 また、こちらの記事では画像からタグ付けを実行し、タグのファイルを作成する手順を紹介しました。 sd-scriptsで学習するにあたり、キャプションファイルとタグファイルをまとめてjsonファイルを作成する必要があります。

手順の概要

始めに、merge_captions_to_metadata.py スクリプトを実行し、キャプションファイルからjson形式のメタデータファイルを作成します。 作成したファイルをmeta_caption.json とします。
次に、merge_dd_tags_to_metadata.py スクリプトを実行し、meta_caption.json にタグファイルの情報を追記します。 追記したファイルを meta_caption_tag.json ファイルとして保存します。
複数のフォルダにキャプションファイルとタグファイルがある場合はこの操作を繰り返します。
すべてのキャプションとタグファイルを追記したファイルに対して、clean_captions_and_tags.py スクリプトを実行しクリーニングを実行します。 クリーニング後のファイル(meta_caption_tag_clean.json)を学習用のメタデータファイルとして利用します。

事前準備

キャプションファイルの作成

キャプションファイルを作成します。手順はこちらの記事を参照してください。

タグファイルの作成

タグファイルを作成します。手順はこちらの記事を参照してください。

手順

コマンドプロンプトを起動し、sd-scriptsの仮想環境に切り替えます。

キャプションファイルからjson形式のメタデータファイルの作成

キャプションファイルのあるフォルダの一つ上のフォルダにカレントディレクトリを変更します。
変更後、次のコマンドを実行します。
python (merge_captions_to_metadata.py のフルパス) (キャプションファイルが保存されているディレクトリ名) (出力するjsonファイル名)

今回の実行環境では以下のコマンドとなります。
python c:\Storage\Image-Gen\sd-scripts\finetune\merge_captions_to_metadata.py toricchi meta_caption.json

コマンドを実行します。エラーが出ずに実行できれば成功です。処理が完了すると "done:" が表示されます。


カレントディレクトリに meta_caption.json ファイルが作成されています。


ファイル内容は以下です。
{
  "01": {
    "caption": "an animated picture of a little bird standing"
  },
  "02": {
    "caption": "a white bird with yellow feet standing with stars"
  },
  "03": {
    "caption": "a cartoon white bird with a shocked look on its face"
  },

  (中略...)

  "35": {
    "caption": "a cartoon bird sitting at a table with a cup of coffee"
  },
  "36": {
    "caption": "a coffee cup on a wooden table"
  }
}

タグファイルの情報をjson形式のメタデータファイルに追記する

続いて、タグファイルの情報を先ほど作成したjson形式のメタデータファイルに追記します。
次のコマンドを実行します。
python (merge_dd_tags_to_metadata.py のフルパス) (タグファイルが保存されているディレクトリ名) (出力するjsonファイル名) --in_json (追記元のjsonファイル名)

今回の実行環境では以下のコマンドとなります。
python c:\Storage\Image-Gen\sd-scripts\finetune\merge_dd_tags_to_metadata.py toricchi meta_caption_tag.json --in_json meta_caption.json

コマンドを実行します。エラーが出ずに実行できれば成功です。処理が完了すると "done:" が表示されます。


カレントディレクトリに meta_caption_tag.json ファイルが作成されています。


ファイル内容は以下です。meta_caption.jsonの内容に、タグファイルの内容が追記されている状態が確認できます。
{
  "01": {
    "caption": "an animated picture of a little bird standing",
    "tags": "toricchi, standing, no humans, bird, animal focus, simple background, pokemon (creature), solo, duck, animal, ..., white background, comic, ._."
  },
  "02": {
    "caption": "a white bird with yellow feet standing with stars",
    "tags": "toricchi, standing, smile, no humans, bird, closed eyes, pokemon (creature), animal focus, duck, solo, simple background, comic, animal, white background"
  },
  "03": {
    "caption": "a cartoon white bird with a shocked look on its face",
    "tags": "toricchi, standing, angry, no humans, bird, solo, pokemon (creature), simple background, animal focus, white background, comic, open mouth, up arms, up hands"
  },

  (中略...)

  "35": {
    "caption": "a cartoon bird sitting at a table with a cup of coffee",
    "tags": "toricchi, sitting, no humans, bird, cup, animal focus, comic, mug, coffee, table, simple background, animal, white background, coffee mug, beak, coffee cup"
  },
  "36": {
    "caption": "a coffee cup on a wooden table",
    "tags": "coffee, no humans, cup, mug, coffee mug, simple background, comic, border"
  }
}

メタデータのクリーニング

メタデータのクリーニングを実行します。以下のコマンドを実行します。
python (clean_captions_and_tags.py のフルパス) (クリーニング元のjsonファイル) (クリーニング処理されたjsonファイルの出力ファイル名)

今回の実行環境では以下のコマンドとなります。
python c:\Storage\Image-Gen\sd-scripts\finetune\clean_captions_and_tags.py meta_caption_tag.json meta_caption_tag_clean.json

コマンドを実行します。エラーが出ずに実行できれば成功です。処理が完了すると "done:" が表示されます。


カレントディレクトリに meta_caption_tag_clean.json ファイルが作成されています。


以上でメタデータファイルの作成は完了です。

補足: --full_path を追加した場合

--full_path オプションを追加した場合は、以下の出力になります。
画像のあるフォルダ名とファイル名がJsonファイル内に記述されています。
{
  "toricchi\\01.png": {
    "caption": "an animated picture of a little bird standing"
  },
  "toricchi\\02.png": {
    "caption": "a white bird with yellow feet standing with stars"
  },
  "toricchi\\03.png": {
    "caption": "a cartoon white bird with a shocked look on its face"
  },

  (中略...)

  "toricchi\\35.png": {
    "caption": "a cartoon bird sitting at a table with a cup of coffee"
  },
  "toricchi\\36.png": {
    "caption": "a coffee cup on a wooden table"
  }
}
著者
iPentecのメインデザイナー
Webページ、Webクリエイティブのデザインを担当。PhotoshopやIllustratorの作業もする。
掲載日: 2023-07-21
iPentec all rights reserverd.