{"id":25004347,"url":"https://github.com/hearmeneigh/dataset-rising","last_synced_at":"2025-04-12T13:51:19.433Z","repository":{"id":192298626,"uuid":"669895611","full_name":"hearmeneigh/dataset-rising","owner":"hearmeneigh","description":"Toolchain for creating custom datasets and training Stable Diffusion (1.x, 2.x, XL) models and LoRAs","archived":false,"fork":false,"pushed_at":"2023-12-12T01:13:53.000Z","size":240,"stargazers_count":15,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-06T21:06:26.412Z","etag":null,"topics":["booru","crawler","danbooru","dataset","dataset-generation","diffusers","e621","finetuning","gelbooru","huggingface","huggingface-diffusers","imageboard","imagebooru","lora","machine-learning","ml","mlops","sdxl","stable-diffusion","training"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hearmeneigh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-07-23T19:24:39.000Z","updated_at":"2024-06-26T10:54:16.000Z","dependencies_parsed_at":"2023-09-04T02:59:57.942Z","dependency_job_id":"fc9c3e02-7c62-4057-bc45-6d1e24db2593","html_url":"https://github.com/hearmeneigh/dataset-rising","commit_stats":null,"previous_names":["hearmeneigh/dataset-rising"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hearmeneigh%2Fdataset-rising","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hearmeneigh%2Fdataset-rising/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hearmeneigh%2Fdataset-rising/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hearmeneigh%2Fdataset-rising/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hearmeneigh","download_url":"https://codeload.github.com/hearmeneigh/dataset-rising/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248575693,"owners_count":21127247,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["booru","crawler","danbooru","dataset","dataset-generation","diffusers","e621","finetuning","gelbooru","huggingface","huggingface-diffusers","imageboard","imagebooru","lora","machine-learning","ml","mlops","sdxl","stable-diffusion","training"],"created_at":"2025-02-04T23:52:16.827Z","updated_at":"2025-04-12T13:51:19.411Z","avatar_url":"https://github.com/hearmeneigh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Dataset Rising\n\n\u003e A toolchain for creating and training Stable Diffusion 1.x, Stable Diffusion 2.x, and Stable Diffusion XL models\n\u003e with custom datasets.\n\nWith this toolchain, you can:\n* **Crawl and download** metadata and images from 'booru' style image boards\n* Combine **multiple sources of images** (including your own custom sources)\n* **Build datasets** based on your personal preferences and filters\n* **Train Stable Diffusion models** with your datasets\n* Convert models into [Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui/tree/master) compatible models\n* Use only the parts you need – the toolchain uses modular design, YAML configuration files, and JSONL data exchange formats\n* Work with confidence that the end-to-end tooling has been tested with Nvidia's RTX30x0, RTX40x0, A100, and H100 GPUs\n\n## Requirements\n* Python `\u003e=3.8`\n* Docker `\u003e=22.0.0`\n\n## Tested With\n* MacOS 13 (M1)\n* Ubuntu 22 (x86_64)\n\n\n## Full Example\nBelow is a summary of each step in dataset generation process. For a full production-quality example, see [e621-rising-configs](https://github.com/hearmeneigh/e621-rising-configs) (NSFW).\n\n### 0. Installation\n```bash\n# install DatasetRising\npip3 install DatasetRising\n\n# start MongoDB database; use `dr-db-down` to stop\ndr-db-up\n```\n\n### 1. Download Metadata (Posts, Tags, ...)\nDataset Rising has a crawler (`dr-crawl`) to download metadata (=posts and tags) from booru-style image boards.\n\nYou must select a unique user agent string for your crawler (`--agent AGENT_STRING`). This string will\nbe passed to the image board with every  HTTP request. If you don't pick a user agent that uniquely identifies you,\nthe image boards will likely block your requests. For example:\n\n\u003e `--agent 'my-imageboard-crawler/1.0 (user @my-username-on-the-imageboard)'`\n\nThe crawler will automatically manage rate limits and retries. If you want to automatically resume a previous (failed)\ncrawl, use `--recover`.\n\n```bash\n## download tag metadata to /tmp/tags.jsonl\ndr-crawl --output /tmp/e962-tags.jsonl --type tags --source e926 --recover --agent '\u003cAGENT_STRING\u003e'\n\n## download posts metadata to /tmp/e926.net-posts.jsonl\ndr-crawl --output /tmp/e926.net-posts.jsonl --type posts --source e926 --recover --agent '\u003cAGENT_STRING\u003e'\n```\n\n### 2. Import Metadata\n\n\u003e This section requires a running MongoDB database, which you can start with `dr-db-up` command.\n\nOnce you have enough post and tag metadata, it's time to import the data into a database.\n\nDataset Rising uses MongoDB as a store for the post and tag metadata. Use `dr-import` to\nimport the metadata downloaded in the previous step into MongoDB.\n\nIf you want to adjust how the tag metadata is treated during the import,\nreview files in [`\u003cdataset-rising\u003e/examples/tag_normalizer`](./examples/tag_normalizer) and set the optional\nparameters `--prefilter FILE`, `--rewrites FILE`, `--aspect-ratios FILE`, `--category-weights FILE`, and\n`--symbols FILE` accordingly.\n\n```bash\ndr-import --tags /tmp/e926.net-tags.jsonl --posts /tmp/e926.net-posts.jsonl --source e926\n```\n\n### 3. Preview Selectors\n\u003e This section requires a running MongoDB database, which you can start with `dr-db-up` command.\n\nAfter the metadata has been imported into a database, you can use selector files to select\na subset of the posts in a dataset.\n\nYour goal is **not** to include **all** images, but to produce\na set of **high quality** samples. The selectors are the mechanism for that.\n\nEach selector contains a **positive** and **negative** list of tags. A post will be included\nby the selector, if it contains at least one tag from the **positive** list and none of the\ntags in the **negative** list.\n\nNote that a great dataset will contain positive **and** negative examples. If you only\ntrain your dataset with positive samples, your model will not be able to use negative\nprompts well. That's why the examples below include four different types of selectors.\n\nDataset Rising has example selectors available in [`\u003cdataset-rising\u003e/examples/select`](examples/select).\n\nTo make sure your selectors are producing the kind of samples you want, use the `dr-preview`\nscript:\n\n```bash\n# generate a HTML preview of how the selector will perform (note: --aggregate is required):\ndr-preview --selector ./examples/select/tier-1/tier-1.yaml --output /tmp/preview/tier-1 --limit 1000 --output --aggregate\n\n# generate a HTML preview of how each sub-selector will perform:\ndr-preview --selector ./examples/select/tier-1/helpers/artists.yaml --output /tmp/preview/tier-1-artists\n```\n\n### 4. Select Images For a Dataset\n\u003e This section requires a running MongoDB database, which you can start with `dr-db-up` command.\n\nWhen you're confident that the selectors are producing the right kind of samples, it's time to select the posts for\nbuilding a dataset. Use `dr-select` to select posts from the database and store them in a JSONL file. \n\n```bash\ncd \u003cdataset-rising\u003e/database\n\ndr-select --selector ./examples/select/tier-1/tier-1.yaml --output /tmp/tier-1.jsonl\ndr-select --selector ./examples/select/tier-2/tier-2.yaml --output /tmp/tier-2.jsonl\n```\n\n### 5. Build a Dataset\nAfter selecting the posts for the dataset, use `dr-join` to combine the selections and \n`dr-build` to download the images and build the actual dataset.\n\nBy default, the build script prunes all tags that have fewer than 100 samples. To adjust this limit, use `--min-posts-per-tag LIMIT`.\n\nThe build script will also prune all images that have fewer than 10 tags. To adjust this limit, use `--min-tags-per-post LIMIT`.\n\nAdding a percentage at the end of a `--source` tells the build script to pick that many samples of the total dataset from the given source, e.g. `--source ./my.jsonl:50%`.\n\n```bash\ndr-join \\\n  --samples '/tmp/tier-1.jsonl:80%' \\\n  --samples '/tmp/tier-2.jsonl:20%' \\\n  --output '/tmp/joined.jsonl'\n\ndr-build \\\n  --source '/tmp/joined.jsonl' \\\n  --output '/tmp/my-dataset'\n```\n\n\n## 6. Upload the Dataset to Huggingface\nAfter building your dataset, [upload it to Huggingface](https://huggingface.co/docs/datasets/upload_dataset).\n\n\n### 7. Train a Model\nThe dataset built by the `dr-build` script is now ready to be used for training.  Dataset Rising uses\n[Huggingface Accelerate](https://huggingface.co/docs/accelerate/index) to train Stable Diffusion models.\n\nTo train a model, you will need to pick a base model to start from. The `--base-model` can be any\n[Diffusers](https://huggingface.co/docs/diffusers/index) compatible model, such as:\n\n* [hearmeneigh/e621-rising-v3](https://huggingface.co/hearmeneigh/e621-rising-v3) (NSFW)\n* [stabilityai/stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0)\n* [stabilityai/stable-diffusion-2-1-base](https://huggingface.co/stabilityai/stable-diffusion-2-1-base)\n* [runwayml/stable-diffusion-v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5)\n\nNote that your training results will be improved significantly if you set `--image-width` and `--image-height`\nto match the resolution the base model was trained with.\n\nNote that the training stage assumes that you have already uploaded your dataset to Huggingface.\n\n\u003e This example does not scale to multiple GPUs. See the [Advanced Topics](#advanced-topics) section for multi-GPU training.\n\n\u003e This example uses `dr-train-xl`, which is designed for training Stable Diffusion XL models. If you want to train\n\u003e Stable Diffusion 1.x or Stable Diffusion 2.x models, use `dr-train` instead.\n\n```bash\ndr-train-xl \\\n  --pretrained-model-name-or-path 'stabilityai/stable-diffusion-xl-base-1.0' \\\n  --dataset-name 'username/dataset-name' \\\n  --output '/tmp/dataset-rising-v3-model' \\\n  --resolution 1024 \\\n  --maintain-aspect-ratio \\\n  --reshuffle-tags \\\n  --tag-separator ' ' \\\n  --random-flip \\\n  --train-batch-size 32 \\\n  --learning-rate 4e-6 \\\n  --use-ema \\\n  --max-grad-norm 1 \\\n  --checkpointing-steps 1000 \\\n  --lr-scheduler constant \\\n  --lr-warmup-steps 0\n```\n\n### 8. Generate Samples\n\u003e This step has not yet been implemented. It is safe to skip this step.\n\nAfter training, you can use the `dr-generate` script to verify that the model is working as expected.\n\n```bash\ndr-generate \\\n  --model '/tmp/dataset-rising-v3-model' \\\n  --output '/tmp/samples' \\\n  --prompt 'cat playing chess with a horse' \\\n  --samples 100 \\\n```\n\n### 9. Use the Model with Stable Diffusion WebUI\nIn order to use the model with [Stable Diffusion WebUI](https://github.com/AUTOMATIC1111/stable-diffusion-webui), it has to be converted to the `safetensors` format.\n\n```bash\n# Stable Diffusion XL models:\ndr-convert-sdxl \\\n  --model_path '/tmp/dataset-rising-v3-model' \\\n  --checkpoint_path '/tmp/dataset-rising-v3-model.safetensors' \\\n  --use_safetensors\n\n# Other Stable Diffusion models:\ndr-convert-sd \\\n  --model_path '/tmp/dataset-rising-v3-model' \\\n  --checkpoint_path '/tmp/dataset-rising-v3-model.safetensors' \\\n  --use_safetensors\n  \n# Copy the model to the WebUI models directory:\ncp '/tmp/dataset-rising-v3-model.safetensors' '\u003cwebui-root\u003e/models/Stable-diffusion'\n```\n\n## Uninstall\nThe only part of Dataset Rising that requires uninstallation is the MongoDB database. You can uninstall the database\nwith the following commands:\n\n```bash\n# Shut down MongoDB instance\ndr-db-down\n\n# Remove MongoDB container and its data -- warning! data loss will occur\ndr-db-uninstall\n```\n\n## Advanced Topics\n\n### Resetting the Database\nTo reset the database, run the following commands.\n\n\u003e **Warning: You will lose all data in the database.**\n\n```bash\ndr-db-uninstall \u0026\u0026 dr-db-up \u0026\u0026 dr-db-create\n```\n\n### Importing Posts from Multiple Sources\nThe `append` script allows you to import posts from additional sources.\n\nUse `import` to import the first source and define the tag namespace, then use `append` to import additional sources.\n\n```bash\n# main sources and tags\ndr-import ...\n\n# additional sources\ndr-append --input /tmp/gelbooru-posts.jsonl --source gelbooru\n```\n\n### Multi-GPU Training\nMulti-GPU training can be carried out with [Huggingface Accelerate](https://huggingface.co/docs/accelerate/package_reference/cli) library.\n\nBefore training, run `accelerate config` to set up your Multi-GPU environment.\n\n```bash\ncd \u003cdataset-rising\u003e/train\n\n# set up environment\naccelerate config\n\n# run training\naccelerate launch \\\n  --multi_gpu \\\n  --mixed_precision=${PRECISION} \\\n  dr_train.py \\\n    --pretrained-model-name-or-path 'stabilityai/stable-diffusion-xl-base-1.0' \\\n    --dataset-name 'username/dataset-name' \\\n    --resolution 1024 \\\n    --maintain-aspect-ratio \\\n    --reshuffle-tags \\\n    --tag-separator ' ' \\\n    --random-flip \\\n    --train-batch-size 32 \\\n    --learning-rate 4e-6 \\\n    --use-ema \\\n    --max-grad-norm 1 \\\n    --checkpointing-steps 1000 \\\n    --lr-scheduler constant \\\n    --lr-warmup-steps 0\n```\n\n## Setting Up a Training Machine\n* Install `dataset-rising`\n* Install [Huggingface CLI](https://huggingface.co/docs/huggingface_hub/installation)\n* Install [Accelerate CLI](https://huggingface.co/docs/accelerate/basic_tutorials/install)\n* Configure Huggingface CLI (`huggingface-cli login`)\n* Configure Accelerate CLI (`accelerate config`)\n\n### Optional\n* Install [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)\n* Install [xFormers](https://github.com/facebookresearch/xformers)\n* Configure AWS CLI (`aws configure`)\n\n### Troubleshooting\n\n#### NCCL Errors\nSome configurations will require `NCCL_P2P_DISABLE=1` and/or `NCCL_IB_DISABLE=1` environment variables to be set.\n\n```bash\nexport NCCL_P2P_DISABLE=1\nexport NCCL_IB_DISABLE=1\n\ndr-train-xl ...\n```\n\n### Cache Directories\nUse `HF_DATASETS_CACHE` and `HF_MODULES_CACHE` to control where Huggingface stores its cache files\n\n```bash\nexport HF_DATASETS_CACHE=/workspace/cache/huggingface/datasets\nexport HF_MODULES_CACHE=/workspace/cache/huggingface/modules\n\ndr-train-xl ...\n```\n\n\n## Developers\n### Setting Up\nCreates a virtual environment, installs packages, and sets up a MongoDB database on Docker. \n\n```bash\ncd \u003cdataset-rising\u003e\n./up.sh\n```\n\n### Shutting Down\nStops the MongoDB database container. The database can be restarted by running `./up.sh` again.\n\n```bash\ncd \u003cdataset-rising\u003e\n./down.sh\n```\n\n\n### Uninstall\nWarning: This step **removes** the MongoDB database container and all data stored on it.\n\n```bash\ncd \u003cdataset-rising\u003e\n./uninstall.sh\n```\n\n### Deployments\n```bash\npython3 -m pip install --upgrade build twine\npython3 -m build \npython3 -m twine upload dist/*\n```\n\n### Architecture\n```mermaid\nflowchart TD\n    CRAWL[Crawl/Download posts, tags, and tag aliases] -- JSONL --\u003e IMPORT\n    IMPORT[Import posts, tags, and tag aliases] --\u003e STORE\n    APPEND[Append additional posts] --\u003e STORE\n    STORE[Database] --\u003e PREVIEW\n    STORE --\u003e SELECT1\n    STORE --\u003e SELECT2\n    STORE --\u003e SELECT3\n    PREVIEW[Preview selectors] --\u003e HTML(HTML)\n    SELECT1[Select samples] -- JSONL --\u003e JOIN\n    SELECT2[Select samples] -- JSONL --\u003e JOIN\n    SELECT3[Select samples] -- JSONL --\u003e JOIN\n    JOIN[Join and prune samples] -- JSONL --\u003e BUILD \n    BUILD[Build dataset] -- HF Dataset/Parquet --\u003e TRAIN\n    TRAIN[Train model] --\u003e MODEL[Model]\n```\n\n## Links\n* [SDXL training notes](https://rentry.org/59xed3#sdxl)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhearmeneigh%2Fdataset-rising","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhearmeneigh%2Fdataset-rising","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhearmeneigh%2Fdataset-rising/lists"}