Preprocess images

Last-modified: 2023-10-25 (水) 21:15:00

概要

Stable Diffusion web UIの標準機能の一つ。
キャプション・タグの一括追加ができる。
WD1.4 Taggerほど高機能ではないが、導入の手間が掛からない。

手順

加工設定

項目を開く

「Train」タブを開きます。 ※Settingsの横です。
「Preprocess images」タブを開きます。

設定内容

  • 「Source directory」
    • ここでは加工前のフォルダを指定します。
      例)H:\stable-diffusion-webui\toreninguONLY\あひる加工前
  • 「Destination directory」
    • ここでは加工後の画像を保存するフォルダを指定します。
      例)H:\stable-diffusion-webui\toreninguONLY\あひる加工後
      ※注意※
      加工後の画像生成先を加工前のフォルダ内にするとエラーが起きる可能性があります。
  • Existing Caption txt Action
    • ignore⇒無視
    • copy⇒コピー
    • prepend⇒先頭に追加
    • append⇒追加
      試していないため不明だが、既にフォルダ内にプロンプトのtxtがある場合の挙動だと思われる。
      自分で調整したtxtと自動captionを同時に使いたい場合prependを選ぶ?
  • 「Create flipped copies」
    • 左右反転したものを作成します。
      ※髪の毛の分け目とかを滅茶苦茶な覚え方したりしてしまうので、やめた方がいいです。
  • 「Split oversized images into two」
    • 「Split image threshold」を「1」、「Split image overlap ratio」を「0」にすれば、ほぼ全て2分割されます。(比率1:1除く)
      問題がなければこちらに指定してください。
    • サイズを超えた場合2つに分割する機能です。
      縦長の腰まで立ち絵などの画像を読み込ませた場合
      頭~肩、胴体~太もも みたいな感じで分割してくれます。
      一度適当なファイルで実際に使って確かめてみてください。
  • 「Auto focal point crop」
    • 自動的に輪郭や顔などを認識して、そこに焦点を合わせて切り抜きする
  • 「Use deepbooru for caption
    • deepdanbooruの機能を使い、タグを作成します。。
      ※二次元美少女関連だとこちらを使った方がいい結果を得られるかもしれません。
    • ※設定画面からInterrogate: deepbooru sort alphabeticallyのチェックを外した方が良い結果になると思われます。
      チェック有りだと、caption出力(girl,boy等)がアルファベット順に並び、正確な学習を阻害する要因になります。
    • しきい値の調整は「setting」-「interrogate Options」-「deepbooru score thres hold」で行います。
      約0.7がおすすめ。
      低すぎると関係ない情報が混入し、大きすぎると基本的な構図すら省かれてしまいます。

加工開始

「Preprocess」ボタンを押すと画像の加工が始まります。
加工終了後、「Destination directory」で指定したフォルダを見ましょう。
フォルダ内には加工後の画像とその画像の説明を書いたtxtファイルが生成されるはずです。 (※ONにしていれば)
※初めて使用する場合は、ちょっと時間がかかるかも。
 関連ソフトの自動ダウンロードによりフリーズっぽくなります。

Tips

deepbooru利用時に、アルファベット順のタグをやめたい

Settings→interrogate項にある

deepbooru: sort tags alphabetically (if not: sort by score)

をオフにすることで、スコア順に変更できる。

BLIP利用時に、キャプションの精度を変えたい。

Settings→interrogate項にある

BLIP: num_beams

の値を大きくすることで、BLIPとしての精度が良くなります。
(ユーザーにとって精度が高くなるかは、やってみないとわからない)

エラーが発生する場合

学習用画像の加工でアップスケールを行った場合、
「TypeError: a bytes-like object is required, not 'str'」
とのエラーにより、動作が止まる場合があります。
その際は適当な画像編集ソフトを開いて保存し直せば解消されます。(画質劣化に注意)

加工後のファイルについて

過去画像ファイルを加工&caption付けした場合ファイル名で出力されていましたが、
今はtxtファイルが出力されるようになっているはずです。
それで問題ありません。
https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Textual-Inversion#filewords

filewords
[filewords] is a tag for prompt template file that allows you to insert text from filename into the prompt. By default, file's extension is removed, as well as all numbers and dashes (-) at the start of filename. So this filename: 000001-1-a man in suit.png will become this text for prompt: a man in suit. Formatting of the text in the filename is left as it is.

It's possible to use options Filename word regex and Filename join string to alter the text from filename: for example, with word regex = \w+ and join string = , , the file from above will produce this text: a, man, in, suit. regex is used to extract words from text (and they are ['a', 'man', 'in', 'suit', ]), and join string (', ') is placed between those words to create one text: a, man, in, suit.

It's also possible to make a text file with same filename as image (000001-1-a man in suit.txt) and just put the prompt text there. The filename and regex options will not be used.
ファイルワード
[filewords] はプロンプトテンプレートファイルのタグで、ファイル名からプロンプトにテキストを挿入することができます。デフォルトでは、ファイルの拡張子は削除され、ファイル名の先頭にある数字とダッシュ (-) もすべて削除される。したがって、このファイル名: 000001-1-a man in suit.png は、プロンプトのテキスト: a man in suit になります。ファイル名中のテキストの書式はそのままです。

例えば、word regex = \w+ と join string = , , とすると、上記のファイルは a, man, in, suit というテキストになります。regexを使ってテキストから単語を抽出し(それらは ['a', 'man', 'in', 'suit', ] )、それらの単語の間に join string (', ') を入れて a, man, in, suit という一つのテキストになります。

また、画像と同じファイル名のテキストファイル(000001-1-a man in suit.txt)を作り、そこにプロンプトのテキストを入れるだけでも可能です。filenameオプションとregexオプションは使用されません。

要約:
学習時のテンプレ内に[filewords]とある場合
画像ファイルのファイル名先頭にある数字とダッシュ(-)は削除され、拡張子も削除される。
画像ファイルと同じ名称のテキストファイルがある場合にはその中身が参照される。

word regexについての編集を求めています。ご協力いただけると幸いです。

やってはいけないこと

タグに作者タグ・作品タグを加えると、学習が進むにつれて歪んでいきます。
特に思うところがなければ、そのままでも大丈夫です。