ホーム 暮らし Various thoughts on wd14-tagger

Various thoughts on wd14-tagger

東條 淳平 東條 淳平 • 更新: • 公開: • 読了約 15 分
目次
n0e5c338f27dc

Hello, I'm cella. I'm not an engineer. I'm an amateur.

Whether it's LoRA or FPFT, the tagging problem is unavoidable when fine-tuning Stable Diffusion. It is a well-known fact among trainers that the quality of these tags significantly impacts the performance of the model.

I believe SmilingWolf's wd-tagger-v2/v3 is the most commonly used. However, it has a major weakness,

If the dataset does not contain at least 600 images including that tag, the tag itself is not used for training (it is not mentioned whether the tag itself is removed or if the MLP is removed after training).
This is very problematic, because the concepts one wants to obtain through fine-tuning often do not have that many tags included, and there is a reality that simply tagging and training with the existing wd-tagger as a routine is completely meaningless. Basically, you end up tagging manually.

Also, the performance of SmilingWolf's tagger is actually not very good, and the F1 score remains at 0.48 (though it is better not to rely too much on the F1 score itself). I think those who use it have felt that there are quite a few mis-tags at the specified threshold.

Since FPFT is too heavy...

Naturally, the desire to improve it arises, and there is an official fine-tuning repository called JAX-CV in SmilingWolf's GitHub repository, but this assumes Full-parameter Finetune and is written in jax/flux, a repository that is likely unfamiliar to SD users, so I think it is difficult to get started with.

Therefore, the idea is to apply LoRA to the wd-tagger. As for existing efforts, plat has developed a model called siglip-tagger-test-3, but this adds a linear layer to google/siglip-so400m-patch14-384 as an Image encoder and uses it as a classifier; the siglip part itself is not fine-tuned.
I thought about whether I could obtain inference performance for a new data range by applying LoRA to each block of the existing wdtagger. In tagutl, I designed it to allow fine-tuning on new datasets at a low cost by applying LoRA to each layer of the attention block and making the head's linear layer trainable. I have confirmed that it works with Bs = 16 on 48GB VRAM and Bs = 4 on 12GB VRAM with a Rank 64 LoRA.

Results

These are the results of training with a dataset of about 300,000 images using this.

The results are generally good, with F1 consistently exceeding 0.8 (due to computational constraints, I performed it on 100 images randomly selected from the Validation Dataset).
However, as far as I can see from the data during training, most of the tags with high pred in False Positive were actually tags that were forgotten when creating the dataset (or were removed because they could be sufficiently explained by other tags). This is a result that could be expected to some extent.

Therefore, the basic idea is that it is not necessarily required to have a high F1 score for actual data. It is reasonable to allow for some False Positives in the data and widen the range to pick up True Positives (in this graph, lowering the threshold to about 0.5).

What exactly is Pred?

The horizontal axis value of the graph above is pred, but some people call this the "probability of being included in that category." Explanations such as "it is normalized to the 0-1 interval by passing the output of the Encoder through a Sigmoid or Softmax function, which is treated as a probability for convenience" often appear when reading Neural network books, but I wonder if it is really okay to call this a probability.

I have displayed blue error bars for False Positive in the graph above, which shows the distribution of pred values for images that were actually negative in the Validation Dataset (the 95% CI looks suspicious, but I think at least the average is reliable).
For example, tags that do not depend on the content of the image, such as "Commentary Request," should not be able to be inferred well, so the negative pred value remains high and does not easily drop. It is easy to imagine that saying "the Commentary Request relevance rate of this image is 70%" for such tags is a very reckless argument.
I haven't properly worked on the mathematical derivation of this point myself, so I'm thinking of considering it when I have some free time.

The same can be said for the results displayed in HIVE and similar tools. Frankly speaking, I believe that those who do not stop at the understanding that 'it is called probability for convenience' and instead seriously engage in discourse like 'It's AI at XX%! It's not AI!' have low literacy. I believe that those who do not stop at the understanding that 'it is called probability for convenience' and instead seriously engage in discourse like 'It's AI at XX%! It's not AI!' have low literacy regarding this.
Furthermore, even if one could truly consider it a probability, since multiple tags are being inferred simultaneously, one must consider multiplicity (the problem of multiple testing), and failing to understand this will also lead to incorrect conclusions.
I will omit the statistical discussion as I am sure you can find plenty of information on it by searching.

Future

Now, regarding the model, I am currently expanding the number of tags to around 50,000 and making fine adjustments. I don't know if I will release the completed tagger itself in the future, but I am continuing to develop it bit by bit within my own Discord server. If you are willing to be a tester while listening to my complaints, please send me a DM on X.

The following is a geeky memo for those interested in the development process. It doesn't contain anything significant, so I hope only those who wish to support the development will take a look (the model itself is not released as of 2025.03.19).

Efforts to improve accuracy

同じカテゴリの記事

東條 淳平

スポーツと芸能が交差する話題を担当。大会公式記録と所属先の発表を突き合わせ、数字は必ず出典付きで扱います。

おすすめの関連記事