OmniGen2 Documentation

repository·main·Indexed 26 days ago

https://github.com/vectorspacelab/omnigen2

OmniGen2 is a multimodal generative model supporting visual understanding, text-to-image generation, instruction-guided image editing, and in-context generation using decoupled decoding pathways. The repository includes tools for the OmniContext benchmark, RL fine-tuning via the FlowGRPO algorithm, and the EditScore Reward Server for image editing optimization. It provides support for distributed multi-machine inference and Gradio-based local demos.

Tokens
6.2K
Snippets
17
Records
30
Agent score
86%

What's inside OmniGen2

  1. Evaluate generated images with GPT-4.1

    main

    Use the omnicontext.test_omnicontext_score module to evaluate image quality via GPT-4.1. You must provide a valid OpenAI API key.

    cd OmniGen2
    
    openai_key="<Your-API-Key>"
    
    python -m omnicontext.test_omnicontext_score \
    --test_data "OmniGen2/OmniContext" \
    --result_dir "omnicontext/results" \
    --model_name "OmniGen2" \
    --openai_key ${openai_key} \
    --max_workers 100
  2. Convert FSDP checkpoints to Hugging Face format

    main

    Before inference, you must convert the .bin checkpoint saved by FSDP into the standard Hugging Face format using convert_ckpt_to_hf_format.py.

    For full fine-tuned models:

    python convert_ckpt_to_hf_format.py \
      --config_path experiments/ft/ft.yml \
      --model_path experiments/ft/checkpoint-10/pytorch_model_fsdp.bin \
      --save_path experiments/ft/checkpoint-10/transformer

    For LoRA fine-tuned models:

    python convert_ckpt_to_hf_format.py \
      --config_path experiments/ft_lora/ft_lora.yml \
      --model_path experiments/ft_lora/checkpoint-10/pytorch_model_fsdp.bin \
      --save_path experiments/ft_lora/checkpoint-10/transformer_lora
  3. Improve subject consistency in generations

    main

    If the generated image does not align well with the input image, use these strategies to improve subject consistency:

    1. Image Quality: Use larger images where the subject occupies a larger proportion of the frame.
    2. Increase Guidance: Increase image_guidance_scale (e.g., to 3.0), though this may cause slight overexposure.
    3. Prompt Templates: When using a single input image, use a template like: "she/he ..., maintaining her/his facial features, hairstyle, and other attributes."
    4. Batch Generation: Increase the number of images per prompt to find a result with better consistency.
    5. Prompt Detail: Use longer, more detailed descriptions of the scene and character interactions.
  4. Install OmniGen2 (Recommended Setup)

    main

    Follow these steps to set up a local environment for OmniGen2. It is recommended to use a clean Python environment (e.g., via Conda) and install flash-attn for optimal performance, although the model can run without it.

    Prerequisites:

    • Python 3.11
    • CUDA 12.4 (for the specified PyTorch and flash-attn versions)

    Steps:

    1. Clone the repository.
    2. Create and activate a Conda environment.
    3. Install PyTorch with CUDA 12.4 support.
    4. Install required dependencies from requirements.txt.
    5. Install flash-attn (optional but recommended).
    # 1. Clone the repo
    git clone git@github.com:VectorSpaceLab/OmniGen2.git
    cd OmniGen2
    
    # 2. (Optional) Create a clean Python environment
    conda create -n omnigen2 python=3.11
    conda activate omnigen2
    
    # 3. Install dependencies
    # 3.1 Install PyTorch (choose correct CUDA version)
    pip install torch==2.6.0 torchvision --extra-index-url https://download.pytorch.org/whl/cu124
    
    # 3.2 Install other required packages
    pip install -r requirements.txt
    
    # Note: Version 2.7.4.post1 is specified for compatibility with CUDA 12.4.
    # OmniGen2 runs even without flash-attn, though we recommend install it for best performance.
    pip install flash-attn==2.7.4.post1 --no-build-isolation
  5. Evaluate RL Fine-Tuned models on GEdit-Bench

    main

    Once the checkpoint is converted to Hugging Face format, use the GEdit-Bench evaluation scripts. You must modify the internal paths in the scripts to point to your converted model checkpoint.

    # Run evaluation for the converted model from step 500
    bash evaluation/GEdit-Bench/omnigen2.sh --experiment_name=omnigen2_edit_rl_4machine_editscore7b_avg4 --step=500
    bash evaluation/GEdit-Bench/omnigen2_eval.sh --experiment_name=omnigen2_edit_rl_4machine_editscore7b_avg4 --step=500
  6. Perform in-context image editing

    main

    For in-context editing (editing based on multiple images), use the following prompt format:

    "Edit the first image: add/replace (the [object] with) the [object] from the second image. [description for your target image]."

    Example: "Edit the first image: add the man from the second image. The man is talking with a woman in the kitchen"

    Ensure the description for your target image is as detailed as possible.

  7. Launch OmniGen2 training

    main

    Training is handled via shell scripts that support PyTorch FSDP for multi-GPU and multi-node distributed training.

    Scripts available:

    • Full-Parameter: scripts/train/ft.sh
    • LoRA: scripts/train/ft_lora.sh

    Single-Node Training: Simply run the script without additional arguments:

    bash scripts/train/ft.sh

    Multi-Node / Multi-GPU Training: Provide environment variables to coordinate the distributed processes:

    bash scripts/train/ft.sh --rank=$RANK --master_addr=$MASTER_ADDR --master_port=$MASTER_PORT --world_size=$WORLD_SIZE

    Note: LoRA checkpoints currently save the entire model's parameters due to FSDP limitations; a conversion step is required before inference.

    # Example for full-parameter fine-tuning
    bash scripts/train/ft.sh --rank=$RANK --master_addr=$MASTER_ADDR --master_port=$MASTER_PORT --world_size=$WORLD_SIZE
  8. Configure and launch RL Fine-Tuning

    main

    RL fine-tuning is performed using the FlowGRPO algorithm.

    Configuration

    Adjust parameters in options/omnigen2_edit_rl_4machine_editscore7b_avg4.yml. Key settings include:

    • train.global_batch_size: Total images generated across all GPUs in a sampling phase.
    • train.batch_size: Batch size per GPU.
    • train.rl.num_images_per_prompt: Candidate images per prompt.
    • train.rl.num_unique_prompts_per_sampling: Unique prompts in a global batch.
    • train.rl.num_update_steps_per_sampling: Gradient updates per sampling phase (set $> 1$ for off-policy RL).
    • train.rl.batch_size_per_forward: Batch size for each forward pass.

    Execution

    Launch training using FSDP-based scripts:

    • Single-machine (8 GPUs): scripts/train/omnigen2_edit_rl_single_machine_editscore7b.sh
    • Multi-machine (e.g., 4 machines): scripts/train/omnigen2_edit_rl_4machine_editscore7b_avg4.sh
    # Single-machine training (8 GPUs)
    bash scripts/train/omnigen2_edit_rl_single_machine_editscore7b.sh
    
    # Multi-machine training
    bash scripts/train/omnigen2_edit_rl_4machine_editscore7b_avg4.sh