Skip to content

COSMOS2IMAGE.hi

Cosmos2 Predict (Image) क्विकस्टार्ट

इस उदाहरण में, हम NVIDIA के flow‑matching मॉडल Cosmos2 Predict (Image) के लिए एक Lycoris LoKr प्रशिक्षित करेंगे।

हार्डवेयर आवश्यकताएँ

Cosmos2 Predict (Image) एक vision transformer‑आधारित मॉडल है जो flow matching का उपयोग करता है।

नोट: इसकी आर्किटेक्चर के कारण, इसे प्रशिक्षण के दौरान quantize नहीं करना चाहिए, जिसका अर्थ है कि full bf16 precision को संभालने के लिए पर्याप्त VRAM की आवश्यकता होगी।

बिना अत्यधिक optimizations के आरामदायक प्रशिक्षण के लिए 24GB GPU न्यूनतम के रूप में अनुशंसित है।

पूर्वापेक्षाएँ

सुनिश्चित करें कि Python इंस्टॉल है; SimpleTuner 3.10 से 3.12 के साथ अच्छा काम करता है।

आप इसे चलाकर जांच सकते हैं:

python --version

यदि आपके Ubuntu पर Python 3.12 इंस्टॉल नहीं है, तो आप यह प्रयास कर सकते हैं:

apt -y install python3.13 python3.13-venv

Container image dependencies

Vast, RunPod, और TensorDock (आदि) के लिए, CUDA 12.2‑12.8 इमेज पर CUDA extensions कम्पाइल करने हेतु यह काम करेगा:

apt -y install nvidia-cuda-toolkit

इंस्टॉलेशन

pip के जरिए SimpleTuner इंस्टॉल करें:

pip install 'simpletuner[cuda]'

# CUDA 13 / Blackwell users (NVIDIA B-series GPUs)
pip install 'simpletuner[cuda13]' --extra-index-url https://download.pytorch.org/whl/cu130

मैनुअल इंस्टॉलेशन या डेवलपमेंट सेटअप के लिए, installation documentation देखें।

वातावरण सेटअप

SimpleTuner चलाने के लिए, आपको एक configuration फ़ाइल, dataset और model directories, तथा एक dataloader configuration फ़ाइल सेट करनी होगी।

Configuration file

एक प्रयोगात्मक स्क्रिप्ट, configure.py, इंटरैक्टिव step‑by‑step कॉन्फ़िगरेशन के जरिए इस सेक्शन को पूरी तरह स्किप करने में मदद कर सकती है। इसमें कुछ सुरक्षा फीचर्स हैं जो सामान्य pitfalls से बचाते हैं।

नोट: यह आपके dataloader को कॉन्फ़िगर नहीं करता। आपको उसे बाद में मैन्युअली करना होगा।

इसे चलाने के लिए:

simpletuner configure

⚠️ जिन देशों में Hugging Face Hub आसानी से उपलब्ध नहीं है, वहाँ HF_ENDPOINT=https://hf-mirror.com को अपने ~/.bashrc या ~/.zshrc में जोड़ें, यह आपके सिस्टम के $SHELL पर निर्भर करता है।

यदि आप मैनुअल कॉन्फ़िगर करना पसंद करते हैं:

config/config.json.example को config/config.json में कॉपी करें:

cp config/config.json.example config/config.json

वहाँ आपको संभवतः निम्न वेरिएबल्स बदलने होंगे:

  • model_type - इसे lora पर सेट करें।
  • lora_type - इसे lycoris पर सेट करें।
  • model_family - इसे cosmos2image पर सेट करें।
  • base_model_precision - महत्वपूर्ण: इसे no_change पर सेट करें — Cosmos2 को quantize नहीं करना चाहिए।
  • output_dir - इसे उस डायरेक्टरी पर सेट करें जहाँ आप अपने checkpoints और validation images रखना चाहते हैं। यहाँ full path उपयोग करने की सलाह है।
  • train_batch_size - 1 से शुरू करें और पर्याप्त VRAM होने पर बढ़ाएँ।
  • validation_resolution - Cosmos2 के लिए डिफ़ॉल्ट 1024x1024 है।
  • अन्य resolutions को कॉमा से अलग कर सकते हैं: 1024x1024,768x768
  • validation_guidance - Cosmos2 के लिए लगभग 4.0 रखें।
  • validation_num_inference_steps - लगभग 20 स्टेप्स रखें।
  • use_ema - इसे true सेट करने से मुख्य trained checkpoint के साथ अधिक स्मूद परिणाम मिलते हैं।
  • optimizer - उदाहरण में adamw_bf16 उपयोग किया गया है।
  • mixed_precision - सबसे कुशल training के लिए bf16 सेट करना अनुशंसित है।
  • gradient_checkpointing - VRAM उपयोग घटाने के लिए इसे सक्षम करें, लेकिन training धीमी होगी।

आपका config.json कुछ ऐसा दिखेगा:

उदाहरण कॉन्फ़िग देखें
{
    "base_model_precision": "no_change",
    "checkpoint_step_interval": 500,
    "data_backend_config": "config/cosmos2image/multidatabackend.json",
    "disable_bucket_pruning": true,
    "flow_schedule_shift": 0.0,
    "flow_schedule_auto_shift": true,
    "gradient_checkpointing": true,
    "hub_model_id": "cosmos2image-lora",
    "learning_rate": 6e-5,
    "lora_type": "lycoris",
    "lycoris_config": "config/cosmos2image/lycoris_config.json",
    "lr_scheduler": "constant",
    "lr_warmup_steps": 100,
    "max_train_steps": 10000,
    "model_family": "cosmos2image",
    "model_type": "lora",
    "num_train_epochs": 0,
    "optimizer": "adamw_bf16",
    "output_dir": "output/cosmos2image",
    "push_checkpoints_to_hub": false,
    "push_to_hub": false,
    "quantize_via": "cpu",
    "report_to": "tensorboard",
    "seed": 42,
    "tracker_project_name": "cosmos2image-training",
    "tracker_run_name": "cosmos2image-lora",
    "train_batch_size": 1,
    "use_ema": true,
    "vae_batch_size": 1,
    "validation_disable_unconditional": true,
    "validation_guidance": 4.0,
    "validation_guidance_rescale": 0.0,
    "validation_negative_prompt": "ugly, cropped, blurry, low-quality, mediocre average",
    "validation_num_inference_steps": 20,
    "validation_prompt": "A photo-realistic image of a cat",
    "validation_prompt_library": false,
    "validation_resolution": "512x512",
    "validation_seed": 42,
    "validation_step_interval": 500
}

ℹ️ Multi‑GPU उपयोगकर्ता उपयोग किए जाने वाले GPU की संख्या कॉन्फ़िगर करने के लिए इस दस्तावेज़ को देखें।

और एक config/cosmos2image/lycoris_config.json फ़ाइल:

उदाहरण कॉन्फ़िग देखें
{
    "bypass_mode": true,
    "algo": "lokr",
    "multiplier": 1.0,
    "full_matrix": true,
    "linear_dim": 10000,
    "linear_alpha": 1,
    "factor": 4,
    "apply_preset": {
        "target_module": [
            "Attention"
        ],
        "module_algo_map": {
            "Attention": {
                "factor": 4
            }
        }
    }
}

उन्नत प्रयोगात्मक विशेषताएँ

उन्नत प्रयोगात्मक विवरण दिखाएँ SimpleTuner में प्रयोगात्मक फीचर्स शामिल हैं जो प्रशिक्षण की स्थिरता और प्रदर्शन को काफी बेहतर कर सकते हैं। * **[Scheduled Sampling (Rollout)](../experimental/SCHEDULED_SAMPLING.md):** exposure bias कम करता है और आउटपुट गुणवत्ता बढ़ाता है, क्योंकि यह प्रशिक्षण के दौरान मॉडल को अपने इनपुट्स खुद जनरेट करने देता है। > ⚠️ ये फीचर्स प्रशिक्षण के कंप्यूटेशनल ओवरहेड को बढ़ाते हैं। #### वैलिडेशन प्रॉम्प्ट्स `config/config.json` के अंदर "primary validation prompt" होता है, जो आमतौर पर आपके single subject या style के लिए मुख्य instance_prompt होता है। इसके अतिरिक्त, एक JSON फ़ाइल बनाई जा सकती है जिसमें वैलिडेशन के दौरान चलाने के लिए अतिरिक्त प्रॉम्प्ट्स हों। उदाहरण config फ़ाइल `config/user_prompt_library.json.example` का फ़ॉर्मैट:
{
  "nickname": "the prompt goes here",
  "another_nickname": "another prompt goes here"
}
nicknames validation के लिए फ़ाइलनाम होते हैं, इसलिए इन्हें छोटा और फ़ाइलसिस्टम‑अनुकूल रखें। ट्रेनर को इस prompt library की ओर इंगित करने के लिए, अपने कॉन्फ़िग में यह सेट करें:
"validation_prompt_library": "config/user_prompt_library.json"
विविध प्रॉम्प्ट्स का सेट यह निर्धारित करने में मदद करेगा कि मॉडल प्रशिक्षण के दौरान collapse तो नहीं हो रहा। इस उदाहरण में `` शब्द को अपने subject नाम (instance_prompt) से बदलें।
{
    "anime_<token>": "a breathtaking anime-style portrait of <token>, capturing essence with vibrant colors and expressive features",
    "chef_<token>": "a high-quality, detailed photograph of <token> as a sous-chef, immersed in the art of culinary creation",
    "just_<token>": "a lifelike and intimate portrait of <token>, showcasing unique personality and charm",
    "cinematic_<token>": "a cinematic, visually stunning photo of <token>, emphasizing dramatic and captivating presence",
    "elegant_<token>": "an elegant and timeless portrait of <token>, exuding grace and sophistication",
    "adventurous_<token>": "a dynamic and adventurous photo of <token>, captured in an exciting, action-filled moment",
    "mysterious_<token>": "a mysterious and enigmatic portrait of <token>, shrouded in shadows and intrigue",
    "vintage_<token>": "a vintage-style portrait of <token>, evoking the charm and nostalgia of a bygone era",
    "artistic_<token>": "an artistic and abstract representation of <token>, blending creativity with visual storytelling",
    "futuristic_<token>": "a futuristic and cutting-edge portrayal of <token>, set against a backdrop of advanced technology",
    "woman": "a beautifully crafted portrait of a woman, highlighting natural beauty and unique features",
    "man": "a powerful and striking portrait of a man, capturing strength and character",
    "boy": "a playful and spirited portrait of a boy, capturing youthful energy and innocence",
    "girl": "a charming and vibrant portrait of a girl, emphasizing bright personality and joy",
    "family": "a heartwarming and cohesive family portrait, showcasing the bonds and connections between loved ones"
}
#### CLIP score ट्रैकिंग यदि आप मॉडल प्रदर्शन स्कोर करने के लिए evaluations सक्षम करना चाहते हैं, तो CLIP scores को कॉन्फ़िगर और इंटरप्रेट करने के लिए [यह दस्तावेज़](../evaluation/CLIP_SCORES.md) देखें। #### Validation previews SimpleTuner Tiny AutoEncoder मॉडलों का उपयोग करके generation के दौरान intermediate validation previews स्ट्रीम करने का समर्थन करता है। इससे आप webhook callbacks के जरिए real‑time में step‑by‑step validation images देख सकते हैं। सक्रिय करने के लिए:
{
  "validation_preview": true,
  "validation_preview_steps": 1
}
**आवश्यकताएँ:** - Webhook configuration - Validation सक्षम होना `validation_preview_steps` को ऊँचा मान (जैसे 3 या 5) रखें ताकि Tiny AutoEncoder का ओवरहेड कम हो। `validation_num_inference_steps=20` और `validation_preview_steps=5` के साथ, आपको steps 5, 10, 15, और 20 पर preview images मिलेंगी। #### Flow schedule shifting एक flow‑matching मॉडल होने के कारण, Cosmos2 में "shift" नाम का गुण होता है जो हमें एक सरल decimal value से timestep schedule के प्रशिक्षित हिस्से को शिफ्ट करने देता है। कॉन्फ़िग में `flow_schedule_auto_shift` डिफ़ॉल्ट रूप से सक्षम है, जो resolution‑dependent timestep shift उपयोग करता है — बड़े images के लिए उच्च shift मान और छोटे images के लिए कम shift मान। #### डेटासेट विचार अपने मॉडल को प्रशिक्षित करने के लिए पर्याप्त बड़ा डेटासेट होना महत्वपूर्ण है। डेटासेट आकार पर सीमाएँ हैं, और आपको सुनिश्चित करना होगा कि आपका डेटासेट पर्याप्त बड़ा हो। ध्यान दें कि न्यूनतम डेटासेट आकार `train_batch_size * gradient_accumulation_steps` के साथ-साथ `vae_batch_size` से भी अधिक होना चाहिए। यदि डेटासेट बहुत छोटा है, तो वह उपयोग योग्य नहीं होगा। > ℹ️ बहुत कम images होने पर आपको **no images detected in dataset** संदेश दिख सकता है — `repeats` मान बढ़ाना इस सीमा को पार करेगा। आपके डेटासेट के अनुसार, आपको डेटासेट डायरेक्टरी और dataloader configuration फ़ाइल अलग तरीके से सेट करनी होगी। इस उदाहरण में हम [pseudo-camera-10k](https://huggingface.co/datasets/bghira/pseudo-camera-10k) डेटासेट उपयोग करेंगे। एक `--data_backend_config` (`config/cosmos2image/multidatabackend.json`) दस्तावेज़ बनाएँ जिसमें यह हो:
[
  {
    "id": "pseudo-camera-10k-cosmos2",
    "type": "local",
    "crop": true,
    "crop_aspect": "square",
    "crop_style": "center",
    "resolution": 1024,
    "minimum_image_size": 1024,
    "maximum_image_size": 1024,
    "target_downsample_size": 1024,
    "resolution_type": "pixel_area",
    "cache_dir_vae": "cache/vae/cosmos2/pseudo-camera-10k",
    "instance_data_dir": "datasets/pseudo-camera-10k",
    "disabled": false,
    "skip_file_discovery": "",
    "caption_strategy": "filename",
    "metadata_backend": "discovery",
    "repeats": 0,
    "is_regularisation_data": true
  },
  {
    "id": "dreambooth-subject",
    "type": "local",
    "crop": false,
    "resolution": 1024,
    "minimum_image_size": 1024,
    "maximum_image_size": 1024,
    "target_downsample_size": 1024,
    "resolution_type": "pixel_area",
    "cache_dir_vae": "cache/vae/cosmos2/dreambooth-subject",
    "instance_data_dir": "datasets/dreambooth-subject",
    "caption_strategy": "instanceprompt",
    "instance_prompt": "the name of your subject goes here",
    "metadata_backend": "discovery",
    "repeats": 1000
  },
  {
    "id": "text-embeds",
    "type": "local",
    "dataset_type": "text_embeds",
    "default": true,
    "cache_dir": "cache/text/cosmos2",
    "disabled": false,
    "write_batch_size": 128
  }
]
> ℹ️ यदि आपके पास captions वाली `.txt` फ़ाइलें हैं तो `caption_strategy=textfile` उपयोग करें। > `caption_strategy` विकल्प और आवश्यकताओं के लिए [DATALOADER.md](../DATALOADER.md#caption_strategy) देखें। फिर, `datasets` डायरेक्टरी बनाएँ:
mkdir -p datasets
pushd datasets
    huggingface-cli download --repo-type=dataset bghira/pseudo-camera-10k --local-dir=pseudo-camera-10k
    mkdir dreambooth-subject
    # place your images into dreambooth-subject/ now
popd
यह लगभग 10k फोटोग्राफ सैंपल्स को आपकी `datasets/pseudo-camera-10k` डायरेक्टरी में डाउनलोड करेगा, जो अपने‑आप बन जाएगी। आपकी Dreambooth images को `datasets/dreambooth-subject` डायरेक्टरी में जाना चाहिए। #### WandB और Huggingface Hub में लॉग‑इन प्रशिक्षण शुरू करने से पहले WandB और HF Hub में लॉग‑इन करना बेहतर है, खासकर यदि आप `--push_to_hub` और `--report_to=wandb` उपयोग कर रहे हैं। यदि आप Git LFS रिपॉज़िटरी में मैन्युअली आइटम्स push करने वाले हैं, तो `git config --global credential.helper store` भी चलाएँ। निम्न कमांड चलाएँ:
wandb login
और
huggingface-cli login
निर्देशों का पालन करके दोनों सेवाओं में लॉग‑इन करें।

प्रशिक्षण रन निष्पादित करना

SimpleTuner डायरेक्टरी से, प्रशिक्षण शुरू करने के लिए आपके पास कई विकल्प हैं:

विकल्प 1 (अनुशंसित - pip install):

pip install 'simpletuner[cuda]'

# CUDA 13 / Blackwell users (NVIDIA B-series GPUs)
pip install 'simpletuner[cuda13]' --extra-index-url https://download.pytorch.org/whl/cu130
simpletuner train

विकल्प 2 (Git clone विधि):

simpletuner train

विकल्प 3 (Legacy विधि - अभी भी काम करता है):

./train.sh

इससे text embed और VAE आउटपुट कैशिंग डिस्क पर शुरू होगी।

अधिक जानकारी के लिए dataloader और tutorial दस्तावेज़ देखें।

बाद में LoKr पर inference चलाना

क्योंकि Cosmos2 एक नया मॉडल है और इसकी डाक्यूमेंटेशन सीमित है, inference उदाहरणों में समायोजन की आवश्यकता हो सकती है। एक बेसिक उदाहरण संरचना:

Show Python inference example
import torch
from lycoris import create_lycoris_from_weights

# Model and adapter paths
model_id = 'nvidia/Cosmos-1.0-Predict-Image-Text2World-12B'
adapter_repo_id = 'your-username/your-cosmos2-lora'
adapter_filename = 'pytorch_lora_weights.safetensors'

def download_adapter(repo_id: str):
    import os
    from huggingface_hub import hf_hub_download
    adapter_filename = "pytorch_lora_weights.safetensors"
    cache_dir = os.environ.get('HF_PATH', os.path.expanduser('~/.cache/huggingface/hub/models'))
    cleaned_adapter_path = repo_id.replace("/", "_").replace("\\", "_").replace(":", "_")
    path_to_adapter = os.path.join(cache_dir, cleaned_adapter_path)
    path_to_adapter_file = os.path.join(path_to_adapter, adapter_filename)
    os.makedirs(path_to_adapter, exist_ok=True)
    hf_hub_download(
        repo_id=repo_id, filename=adapter_filename, local_dir=path_to_adapter
    )

    return path_to_adapter_file

# Load the model and adapter

import torch
from diffusers import Cosmos2TextToImagePipeline

# Available checkpoints: nvidia/Cosmos-Predict2-2B-Text2Image, nvidia/Cosmos-Predict2-14B-Text2Image
model_id = "nvidia/Cosmos-Predict2-2B-Text2Image"
adapter_repo_id = "youruser/your-repo-name"

adapter_file_path = download_adapter(repo_id=adapter_repo_id)
pipe = Cosmos2TextToImagePipeline.from_pretrained(model_id, torch_dtype=torch.bfloat16)

lora_scale = 1.0
wrapper, _ = create_lycoris_from_weights(lora_scale, adapter_file_path, pipeline.transformer)
wrapper.merge_to()

pipe.to("cuda")

prompt = "A close-up shot captures a vibrant yellow scrubber vigorously working on a grimy plate, its bristles moving in circular motions to lift stubborn grease and food residue. The dish, once covered in remnants of a hearty meal, gradually reveals its original glossy surface. Suds form and bubble around the scrubber, creating a satisfying visual of cleanliness in progress. The sound of scrubbing fills the air, accompanied by the gentle clinking of the dish against the sink. As the scrubber continues its task, the dish transforms, gleaming under the bright kitchen lights, symbolizing the triumph of cleanliness over mess."
negative_prompt = "The video captures a series of frames showing ugly scenes, static with no motion, motion blur, over-saturation, shaky footage, low resolution, grainy texture, pixelated images, poorly lit areas, underexposed and overexposed scenes, poor color balance, washed out colors, choppy sequences, jerky movements, low frame rate, artifacting, color banding, unnatural transitions, outdated special effects, fake elements, unconvincing visuals, poorly edited content, jump cuts, visual noise, and flickering. Overall, the video is of poor quality."

output = pipe(
    prompt=prompt, negative_prompt=negative_prompt, generator=torch.Generator().manual_seed(1)
).images[0]
output.save("output.png")

नोट्स और समस्या‑समाधान टिप्स

मेमोरी विचार

चूंकि Cosmos2 को training के दौरान quantize नहीं किया जा सकता, इसलिए मेमोरी उपयोग quantized मॉडलों से अधिक होगा। कम VRAM उपयोग के लिए key सेटिंग्स:

  • gradient_checkpointing सक्षम करें
  • batch size 1 रखें
  • यदि मेमोरी तंग है तो adamw_8bit optimizer पर विचार करें
  • environment variable PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True सेट करना multi‑aspect ratio प्रशिक्षण में VRAM उपयोग को कम करने में मदद करता है
  • डिस्क पर कैशिंग के बिना online VAE encoding करने के लिए --vae_cache_disable उपयोग करें; यह डिस्क स्पेस बचाता है लेकिन training समय/मेमोरी दबाव बढ़ाता है।

प्रशिक्षण विचार

Cosmos2 एक नया मॉडल है, इसलिए optimal training parameters अभी खोजे जा रहे हैं:

  • उदाहरण में AdamW के साथ 6e-5 learning rate उपयोग किया गया है
  • अलग‑अलग resolutions संभालने के लिए flow schedule auto‑shift सक्षम है
  • प्रशिक्षण प्रगति मॉनिटर करने के लिए CLIP evaluation उपयोग होता है

Aspect bucketing

कॉन्फ़िग में disable_bucket_pruning true सेट है, जिसे आपके डेटासेट लक्षणों के अनुसार बदला जा सकता है।

Multiple‑resolution training

मॉडल को शुरू में 512px पर प्रशिक्षित किया जा सकता है, और बाद में उच्च resolutions पर प्रशिक्षण की संभावना होती है। flow_schedule_auto_shift सेटिंग multi‑resolution training में मदद करती है।