starik222 / BooruDatasetTagManager

MIT License
1.45k stars 124 forks source link

How to load downloaded models not from cache? #133

Closed RandomErrorkun closed 6 months ago

RandomErrorkun commented 6 months ago

Hi, when I use the AutoTagger function, the script always wants to download models from https://huggingface.co/SmilingWolf, but it is hard for my network to connect to hugging face when using CLI, so I download the model and csv files from the website directly.

But I'm not sure how BDTM or script will load these models. It appears to have to choose between load cache and download models.

Is there any way to use the downloaded autotagger models in this situation?

(By the way, I downloaded Z3D-E621-Convnext from https://huggingface.co/silveroxides/Z3D-E621-Convnext/tree/main, this is not included in SmilingWolf's tagger list, I modify the interrogator.py file and make BDTM show this option in AutoTagger setting, but when I use it, it said that can not find the URL or something, I think this can be solved by loading model which not from the cache?)

(Not good at English, hope these words can explain what I mean)

RandomErrorkun commented 6 months ago

I find the way to use these downloaded models by reading https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache

I create a folder in the cache folder like a real cache folder

for example:

<CACHE_DIR>
├─ models--SmilingWolf--wd-vit-tagger-v3
│  ├─blobs
│  ├─refs
│  └─snapshots
│      └─f02b7b0e63824995e32adb78c84c41728bba13e3
...

the folder in snapshots is named by a 40-char-long string which fulfill by characters randomly choose in '0123456789abcdef', and then put model.onnx and selected_tags.csv in this folder

create a text file in refs folder and rename it to main (no suffix), then edit it by inputting the random string (dir name in snapshots, without \n)

the cache-like folder now should look like this:

<CACHE_DIR>
├─ models--SmilingWolf--wd-vit-tagger-v3
│  ├─blobs
│  ├─refs
│  │  └─main
│  └─snapshots
│      └─f02b7b0e63824995e32adb78c84c41728bba13e3
│          ├─model.onnx
│          └─selected_tags.csv
...

and in BDTM, turn on Skip internet requests in Auto tagger settings

now you should use the autotagger function normally(of course you need to run the "interrogator_rpc" service)

for the Z3D-E621-Convnext model, create a folder named models--SmilingWolf--Z3D-E621-Convnext in cache folder and do the same things as mentioned above, additionally, modify interrogator.py file in interrogator_rpc folder, add corresponding item to WD_TAGGER_NAMES and WD_TAGGER_THRESHOLDS

for example:

WD_TAGGER_NAMES = [
    ... # original item
    "Z3D-E621-Convnext", # added item
]

WD_TAGGER_THRESHOLDS = [
    ... # original item
    0.25, # added item
]

this may work for other models

if there is a more convenient way to do this, I will be very happy to know~