Skip to main content

A package to use the MusubiTraining

Project description

TechTrash_MusubiTraining

Petite librairie Python pour automatiser un training “photo” avec Musubi Tuner:

  • Prépare un dataset (téléchargement d’un zip d’images + captions).
  • Lance le pre-cache (VAE latents + text encoder outputs).
  • Lance le training via accelerate launch.
  • Convertit optionnellement le LoRA au format ComfyUI.

Pour les utilisateurs (simple)

Prérequis

  • Python 3.11+
  • Un clone local de Musubi Tuner (le repo qui contient zimage_train_network.py)
  • Les modèles .safetensors (DiT / VAE / text encoder / base weights)

Notes:

  • Cette lib lance des scripts externes. Donc ton environnement Python doit avoir accelerate installé.
  • Le code utilise aussi requests et pynvml.

Installation (dev / local)

Depuis Libs-MusubiTraining/:

pip install -e .

Exemple minimal (init + train)

Tu dois fournir des chemins absolus:

  • absolute_path_models: dossier qui contient tes .safetensors
  • absolute_path_training_folder: dossier de travail (dataset.toml, images/, cache/, etc.)
  • absolute_path_output: dossier où le training écrit le modèle final
  • absolute_path_musubi_tuner: dossier du repo Musubi Tuner

Ensuite:

  • dataset_toml_content: le contenu TOML en string (il est validé avant écriture)
  • images_zip_url: une URL vers un .zip contenant des images
  • trigger_word: un texte qui sera écrit dans chaque .txt (caption)

Exemple:

from musubitraining.main import MusubiTraining, Models

models = Models(
    ae="ae.safetensors",
    text_encoder="qwen_3_4b.safetensors",
    DiT="z_image_de_turbo_v1_bf16.safetensors",
    base_weights="zimage_turbo_training_adapter_v2.safetensors",
)

trainer = MusubiTraining(
    absolute_path_models="/workspace/models",
    models_for_training=models,
    absolute_path_training_folder="/workspace/my-training-run",
    absolute_path_output="/workspace/outputs",
    absolute_path_musubi_tuner="/workspace/musubi-tuner",
)

# IMPORTANT:
# `image_directory` and `cache_directory` MUST match what the code creates:
# - {absolute_path_training_folder}/images
# - {absolute_path_training_folder}/cache
dataset_toml_content = f"""# resolution, caption_extension, batch_size, num_repeats, enable_bucket, bucket_no_upscale should be set in either general or datasets
# otherwise, the default values will be used for each item

# general configurations
[general]
resolution = [1024, 1024]
caption_extension = ".txt"
batch_size = 1
enable_bucket = true
bucket_no_upscale = false

[[datasets]]
image_directory = "{trainer.absolute_path_training_folder}/images"
cache_directory = "{trainer.absolute_path_training_folder}/cache"
"""

model_path = trainer.train(
    dataset_toml_content=dataset_toml_content,
    images_zip_url="https://example.com/my_images.zip",
    trigger_word="my_trigger_word",
    # Par défaut: pre-cache ON, conversion ComfyUI ON
    # use_pre_cache=True,
    # convert_for_comfyui=True,
    output_name="output_lora_model",
    max_train_steps=2000,
    seed=42,
)

print("Model created:", model_path)

Exemple de dataset.toml (valide)

Ce fichier est écrit automatiquement dans:

  • {absolute_path_training_folder}/dataset.toml

La partie importante: les chemins doivent correspondre à ce que la lib crée:

  • image_directory = "{absolute_path_training_folder}/images"
  • cache_directory = "{absolute_path_training_folder}/cache"

Modèle de base (celui-ci est valide TOML):

# resolution, caption_extension, batch_size, num_repeats, enable_bucket, bucket_no_upscale should be set in either general or datasets
# otherwise, the default values will be used for each item

# general configurations
[general]
resolution = [1024, 1024]
caption_extension = ".txt"
batch_size = 1
enable_bucket = true
bucket_no_upscale = false

[[datasets]]
image_directory = "/ABS/PATH/TO/YOUR/TRAINING_FOLDER/images"
cache_directory = "/ABS/PATH/TO/YOUR/TRAINING_FOLDER/cache"

Output (ce que retourne train())

  • Si convert_for_comfyui=True (par défaut): retourne le chemin du fichier:
    • .../{output_name}_comfyui.safetensors
  • Sinon: retourne le chemin du fichier:
    • .../{output_name}.safetensors

Pour les devs (compréhension du projet)

Où est le code

  • Code principal: src/musubitraining/main.py

API principale

  • Models (dataclass):
    • Stocke les noms de fichiers .safetensors attendus dans absolute_path_models.
  • MusubiTraining:
    • __init__(...): stocke les chemins absolus et fait quelques checks.
    • _prepare_dataset(...): crée dataset.toml, télécharge et extrait le zip d’images, crée les captions.
    • _pre_cache(...): lance 2 scripts Musubi Tuner pour pré-calculer les caches.
    • _launch_training(...): lance le training via python -m accelerate launch ... et retourne le chemin du modèle final .safetensors.
    • _convert_for_comfyui(...): convertit un LoRA “z-image” vers un LoRA compatible ComfyUI et retourne le chemin du fichier converti.
    • train(...): orchestre tout et retourne le chemin final.

Choix techniques importants

  • Validation TOML “fail fast”:
    • _validate_toml_str() parse le TOML avant écriture.
    • Ça évite de découvrir des erreurs plus tard dans le training.
  • Pas de os.system:
    • On utilise subprocess.run([...], check=True) pour:
      • gérer les chemins avec espaces,
      • remonter les erreurs correctement,
      • éviter les commandes multi-lignes fragiles.
  • Exécution dans le bon env:
    • On utilise sys.executable pour exécuter accelerate et les scripts.
    • Ça aide à éviter les bugs “pas le bon python”.

Limites / points à améliorer (connus)

  • _prepare_dataset() écrit la même caption (trigger_word) pour toutes les images.
    • C’est volontairement simple, mais tu peux l’étendre.
  • Si tu veux un vrai “runner” propre, on peut:
    • ajouter des logs,
    • capturer stdout/stderr,
    • ajouter une config plus riche pour les params d’entraînement.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

techtrash_musubitraining-0.0.9.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

techtrash_musubitraining-0.0.9-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file techtrash_musubitraining-0.0.9.tar.gz.

File metadata

File hashes

Hashes for techtrash_musubitraining-0.0.9.tar.gz
Algorithm Hash digest
SHA256 3f85f8267f97b7c7c06c4e4379eabe188fb06d44cb324ff4e1afa72c667cb7c4
MD5 78f9237a261d091ed0f226cd0f3fbfee
BLAKE2b-256 4dacb43e2fb658a35f525a74da92879d171b79c038f6a74ab1dd4c9d8ed15268

See more details on using hashes here.

File details

Details for the file techtrash_musubitraining-0.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for techtrash_musubitraining-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 62a62e9a40e566bfe40ca2c6b2a6f64c733cd38e86728eca4cc1766cb01fdfa6
MD5 f2d4d7e8bd3add591513e12d859a33c5
BLAKE2b-256 101f7d4fdd8af6496b4a961c0ad28b84257ff8b1baa5e47c3f2acdcc1bf45520

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page