DeepDanbooru Documentation

repository·master·Indexed 25 days ago

https://github.com/kichangkim/deepdanbooru

An anime-style girl image tag estimation system using deep learning to predict tags for images. It includes tools for creating and training custom models, downloading tag lists from Danbooru, evaluating model performance, generating Grad-CAM visualizations, and converting saved Keras models to TensorFlow Lite (.tflite) format.

Tokens
3.4K
Snippets
4
Records
22
Agent score
84%

What's inside DeepDanbooru

  1. Train a DeepDanbooru project

    master

    Follow these steps to create and train a custom tag estimation model:

    1. Prepare dataset: Ensure your images and SQLite database follow the required structure.
    2. Create project folder: Initialize a new project directory.
    3. Prepare tags: Download the latest tags from Danbooru or create a custom tags.txt.
    4. Configure project: Update project.json with the correct database_path.
    5. Train: Execute the training process.

    Commands:

    • deepdanbooru create-project [your_project_folder]
    • deepdanbooru download-tags [your_project_folder] --username [your_danbooru_account] --api-key [your_danbooru_api_key]
    • deepdanbooru train-project [your_project_folder]
  2. Install DeepDanbooru

    master

    You can install DeepDanbooru using pip. Note that tensorflow is not included by default. To include the tensorflow extra package, use the [tensorflow] syntax.

    Requirements include Python 3.11 and packages such as Click, numpy, requests, scikit-image, six, tensorflow, and tensorflow-io.

    # default installation
    pip install .
    
    # with tensorflow package
    pip install .[tensorflow]
    
    # or using requirements.txt
    pip install -r requirements.txt
  3. Configure training via project.json

    master

    The training behavior is controlled by the project.json file located within the project_path. The following keys are used to configure the training process:

    KeyDescription
    image_widthTarget width for input images
    image_heightTarget height for input images
    database_pathPath to the image database
    minimum_tag_countMinimum tags required for an image to be included
    modelModel architecture (resnet_152, resnet_custom_v1, resnet_custom_v2, resnet_custom_v3, or resnet_custom_v4)
    optimizerOptimizer type (adam, sgd, or rmsprop)
    learning_rateInitial learning rate (default: 0.001)
    learning_ratesA list of objects specifying learning rate schedules: [{"used_epoch": N, "learning_rate": R}, ...]
    minibatch_sizeNumber of samples per minibatch
    epoch_countTotal number of epochs to train
    export_model_per_epochFrequency of model exports (default: 10)
    checkpoint_frequency_mbFrequency of checkpointing (measured in minibatches)
    console_logging_frequency_mbFrequency of console log updates (measured in minibatches)
    rotation_rangeData augmentation: rotation range
    scale_rangeData augmentation: scale range
    shift_rangeData augmentation: shift range
    mixed_precisionBoolean to enable mixed precision training (default: false)
    lossLoss function type (binary_crossentropy or focal_loss)
  4. Configure the dataset structure and SQLite schema

    master

    DeepDanbooru requires a specific directory structure and a SQLite database for training.

    Directory Structure:

    • An images/ folder containing sub-folders named after the first two characters of the image filename (e.g., 00/, 01/, ff/).
    • A .sqlite file located in the same directory as the images/ folder.
    • Image filenames must follow the pattern [md5].[file_ext].

    SQLite posts table schema:

    • id (INTEGER)
    • md5 (TEXT)
    • file_ext (TEXT)
    • tag_string (TEXT): A space-separated list of tags (e.g., 1girl ahoge long_hair).
    • tag_count_general (INTEGER): Used for the minimum_tag_count setting. Images with a value $\ge$ minimum_tag_count are used for training.
  5. Filter dataset using make-training-database

    master

    If you need to train using specific optional tags (such as rating and score), convert your dataset into a filtered system tag database using the make-training-database command.

    deepdanbooru make-training-database [your_dataset_sqlite_path] [your_filtered_sqlite_path]
  6. Train a model within a project

    master

    The train_project function initiates the training process for a DeepDanbooru model using configurations defined in a project's project.json file. It supports resuming training from existing checkpoints, handles mixed-precision training, and manages model exports and checkpoints automatically.

    To use this, you must provide a project_path (containing the project.json and dataset) and an optional source_model path to resume training from a specific model file.

  7. Evaluate a training project performance

    master

    Use the evaluate_project function to assess a trained model's performance on a target dataset. The function loads a project context, model, and tags from a specified project_path, then iterates through images in a target_path to predict tags. For each image, it prints tags that meet or exceed a specified threshold score.

    Parameters:

    • project_path: Path to the directory containing the trained project (context, model, and tags).
    • target_path: Path to a specific image file or a directory containing images (supports .png, .jpg, .jpeg, .gif).
    • threshold: A float value used to filter predicted tags. Only tags with a prediction score greater than or equal to this value will be printed.
  8. Evaluate images with the DeepDanbooru evaluate command

    master

    The evaluate function allows you to run inference on one or more images using a trained DeepDanbooru model. It predicts tags for the provided images and can optionally save the results as .txt files.

    If a project_path is provided, the function will automatically attempt to load both the model and the tags from that project. Otherwise, you must explicitly provide model_path and tags_path.

  9. Download specific tag categories

    master

    The download_category_tags function allows for granular downloading of specific tag categories from the Danbooru API.

    Supported Categories

    • general (index 0)
    • artist (index 1)
    • copyright (index 3)
    • character (index 4)

    Parameters

    • category: The category string (general, artist, copyright, or character).
    • minimum_post_count: Minimum number of posts required for a tag to be included.
    • limit: Maximum number of tags to return.
    • username: Danbooru username.
    • api_key: Danbooru API key.
    • page_size: Number of tags to request per API page (default: 1000).
    • order: The order to sort the search results (default: "count").