omini-kontext

Saquib764长期未更新

支持 Flux Kontext dev 多图输入的推理与训练框架,可配合 LoRA 做参数高效微调,适合需要多图条件生成的图像模型实验。

An inference and training framework for multiple image input in Flux Kontext dev

暂无版本444star33fork同步于 2026-11-07 07:29 UTC

怎么上手

由 README 提炼,执行前请确认
conda create -n omini-kontext python=3.10
conda activate omini-kontext
pip install -r requirements.txt
pip install git+https://github.com/huggingface/diffusers
硬件要求按 README 说明

需要 CUDA 兼容 GPU,推荐 24GB 以上显存;README 未说明 CPU 运行与具体 GPU 型号支持。

需要 CUDA多卡 / 张量并行
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

基于 Flux Kontext 的多图参考训练与推理框架,用 3D RoPE 实现参考图编辑

支持 Flux Kontext dev 多图输入的推理与训练框架,可配合 LoRA 做参数高效微调,适合需要多图条件生成的图像模型实验。

核心能力

  • 不改模型架构,仅调整 3D RoPE 嵌入实现参考图编辑
  • 支持多图输入推理,可加载角色插入 LoRA 权重
  • 提供 LoRA 训练脚本,支持多卡与断点续训
  • 附带 ComfyUI 节点,兼容 Nunchaku 加速版模型
  • 提供 optimise_image_condition 预处理参考图减少 token

容易踩的坑

diffusers 必须从 GitHub 源码安装,PyPI 版本不含 FluxKontext 管线;需 HuggingFace 账号并下载 black-forest-labs/FLUX.1-Kontext-dev 基座模型;角色插入需额外加载 saquiboye/omini-kontext-character 的 LoRA 权重。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Flux Omini Kontext framework for multi-image reference: training and inference

    Python PyTorch Lightning License

    Updates

    • Qwen-Image-Edit support added. Spatial character insertion model available on HF(link below). this is experimental model, and not yet trained on large dataset. It sometimes insert duplicate characters.
    • This repository includes nodes and patches for ComfyUI that are compatible with the Nunchaku extension. So you can use nunchaku version of Flux Kontext model with Omini Kontext at lightning speed.

    🚀 Live Demo

    https://github.com/user-attachments/assets/e3397377-3599-499b-b9c8-77d14b60dd3f

    Replicate version: https://replicate.com/thefluxtrain/omini-kontext

    Installation guides, workflows, turorials, and demos

    If you have trained your own model, you can use it on replicate. You will have to upload the model on HF, and enter the details on replicate.

    OminiKontext is a framework built around Flux.1-Kontext-dev model. We do not alter the model architecture, but rather we play around with 3D RoPE embeddings to enable reference based edits on a given image.

    The approach is heavily inspired from OminiControl project, that uses the same RoPE embeddings trick to achieve reference based image generation using Flux.1 dev model. However, Flux.1 dev uses 2D RoPE embeddings, where as Kontext uses 3D RoPE embeddings.

    More details on delta values-

    Read this issue - https://github.com/Saquib764/omini-kontext/issues/12

    🎨 Generated Samples

    Spatial Character Insertion

    The following examples demonstrate how the trained model can insert cartoon characters into existing scenes. It takes a reference character with the desired position of character in an white image, and a scene image. The model will insert the character into the scene image at the desired position. The model takes a some freedom to place the character, around the desired position (not exactly at the desired position), based on the feasibility (common sense) of image.

    I used 30 image pair to train the model for intuitive blending task. Sometimes the results are not good, but overall the model is able to blend the character into the scene. This more like a POC. I plan to train another model(obviously opensource) with much larger dataset to make it more robust.

    SceneReference CharacterGenerated Result
    Scene 1Boy ReferenceOutput 1
    Scene 2Boy ReferenceOutput 2
    Scene 3Boy ReferenceOutput 3
    SceneReference CharacterGenerated Result
    Scene 4Boy ReferenceOutput 4
    Scene 5Boy ReferenceOutput 5
    Scene 6Boy ReferenceOutput 6
    Scene 7Boy ReferenceOutput 7
    Scene 8Boy ReferenceOutput 8
    Scene 9Boy ReferenceOutput 9
    Scene 10Boy ReferenceOutput 10
    Scene 11Boy ReferenceOutput 11
    Scene 12Boy ReferenceOutput 12
    Scene 13Boy ReferenceOutput 13
    Scene 14Boy ReferenceOutput 14

    Non-spatial Character Insertion

    The following examples demonstrate how the trained model can insert cartoon characters into existing scenes. There is not spatial control over the character in this case.

    SceneReference CharacterGenerated Result
    Scene 1Boy ReferenceOutput 1
    Scene 2Boy ReferenceOutput 2

    More comming soon!

    Model Comparison

    The following table compares the performance of Omini Kontext model with a character insertion LoRA, against the vanilla FLUX.1-Kontext-dev model. For the comparision, we used Add character to the image. The character is scared. as the prompt for all the images.

    SceneReferenceVanillaOmini
    Living RoomBoyLiving Room Boy VanillaLiving Room Boy Omini
    Living RoomDogLiving Room Dog VanillaLiving Room Dog Omini
    SceneReferenceVanillaOmini
    Living RoomGirlLiving Room Girl VanillaLiving Room Girl Omini
    ForestBoyForest Boy VanillaForest Boy Omini
    ForestGirlForest Girl VanillaForest Girl Omini
    ForestDogForest Dog VanillaForest Dog Omini

    Pretrained LoRA models

    Model NameDeltaDescription
    character_3000.safetensors[0,0,96]On comfyUI, use cfg=1.5 , LoRA strength = 0.5-0.7. Character in white background.
    spatial-character-test.safetensors[1,0,0]On comfyUI, use cfg=1.5 , LoRA strength = 0.5-0.7 . Upload the reference image of same size as the base image, with character placed corresponding to where you want it to be.
    product_2000.safetensors[0,0,96]On comfyUI, use cfg=1.5 , LoRA strength = 0.5-0.7. Product in white background.

    Notes:

    1. Sometimes, the product or characters are too big compared to the rest of the scene, simply use a smaller resolution image.
    2. Trained on 512x512 reference image, but works fine with 1024x1024

    📋 To-do

    • Add Qwen-Image-Edit support
    • Extend to input multiple references.
    • Create more demos for various usecases. Community support needed!
    • Add Nunchaku integration in ComfyUI
    • Use dataset from HF for training
    • Scrip to push dataset to huggingface
    • Create an easy to use ComfyUI integration, one that uses native comfyui nodes. Scroll to end.
    • Make a data processing script, available in helpers/dataset_creator.ipynb
    • Add ways to control location and scale of the reference character
    • Speed up by removing irrelevant pixels
    • Deploy a public demo
    • Deploy a replicate version
    • Add comfyUI integration - Scroll to bottom
    • Basic training script
    • Basic inference script

    Model training Plans

    • Qwen-Image-Edit character model: Train character models for Qwen-Image-Edit.
    • Person Models: Develop models for realistic human subjects
    • Clothes Models: Create models for clothing and fashion items
    • Subject Models: Train models for specific objects and items
    • Character Models: Train specialized models for anime/cartoon characters

    🚀 Quick Start

    Setup Environment

    # Create conda environment
    conda create -n omini-kontext python=3.10
    conda activate omini-kontext
    
    # Install dependencies
    pip install -r requirements.txt
    

    Basic Training

    📦 Installation

    Prerequisites

    • Python 3.8+
    • CUDA-compatible GPU (recommended: 24GB+ VRAM)
    • PyTorch 2.0+
    • HuggingFace account for model access

    Install Dependencies

    # Core requirements
    pip install torch>=2.0.0 lightning>=2.0.0
    
    # Install diffusers from GitHub (required for FluxKontext pipeline)
    pip install git+https://github.com/huggingface/diffusers
    
    # Training-specific requirements
    pip install -r requirements.txt
    

    Verify Installation

    import torch
    from src.pipeline_flux_omini_kontext import FluxOminiKontextPipeline
    
    # Test pipeline loading
    pipe = FluxOminiKontextPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-Kontext-dev"
    )
    print("✅ Installation successful!")
    

    🎯 Usage

    Basic Inference

    from diffusers.utils import load_image
    from src.pipeline_flux_omini_kontext import FluxOminiKontextPipeline
    import torch
    
    # Load pipeline
    pipe = FluxOminiKontextPipeline.from_pretrained(
        "black-forest-labs/FLUX.1-Kontext-dev",
        torch_dtype=torch.bfloat16
    )
    pipe.to("cuda")
    
    # Load images
    input_image = load_image("path/to/input.jpg")
    reference_image = load_image("path/to/reference.jpg")
    
    # Load Character OminiKontext LoRA
    pipe.load_lora_weights(
        "saquiboye/omini-kontext-character",
        weight_name="character_3000.safetensors",
        adapter_name="lora_weights"
    )
    
    # Generate
    result = pipe(
        image=input_image,
        reference=reference_image,
        reference_delta=[0, 0, 96],  # Position delta for reference
        prompt="A beautiful landscape with mountains",
        guidance_scale=3.5,
        num_inference_steps=28
    )
    
    # Save result
    result.images[0].save("output.png")
    

    Optimizing Reference Images

    The optimise_image_condition function helps improve inference and training performance by preprocessing reference images to optimize token usage. This optimization removes irrelevant pixels while preserving the essential features needed for conditioning.

    from src.utils.image_utils import optimise_image_condition
    from PIL import Image
    
    # Load your reference image
    reference = Image.open("path/to/reference.jpg")
    
    # Optimize the reference image
    reference_delta = [0, 0, 96]
    optimised_reference, new_reference_delta = optimise_image_condition(reference, reference_delta)
    
    # Use in inference
    result = pipe(
        image=input_image,
        reference=optimized_reference,  # Pass the optimized reference
        reference_delta=new_reference_delta,
        prompt="A beautiful landscape with mountains",
        guidance_scale=3.5,
        num_inference_steps=28
    )
    

    🛠️ Training

    Data Preparation

    Your training data should be organized as follows:

    data/
    ├── start/          # Input images (960x512)
    ├── reference/      # Reference images (512x512)
    └── end/           # Target images (896x512)
    

    Training Configuration

    # Training config
    config = {
        "flux_pipe_id": "black-forest-labs/FLUX.1-Kontext-dev",
        "lora_config": {
            "r": 16,
            "lora_alpha": 32,
            "target_modules": ["to_q", "to_k", "to_v", "to_out.0"],
            "lora_dropout": 0.1,
            "bias": "none",
            "task_type": "CAUSAL_LM"
        },
        "optimizer_config": {
            "type": "AdamW",
            "params": {
                "lr": 1e-4,
                "weight_decay": 0.01,
                "betas": (0.9, 0.999)
            }
        },
        "gradient_checkpointing": True
    }
    

    Start Training

    # Basic training
    python train/script/train.py --config train/config/basic.yaml
    
    # Multi-GPU training
    python train/script/train.py --config train/config/multi_gpu.yaml
    
    # Resume training
    python train/script/train.py --config train/config/resume.yaml --resume_from_checkpoint path/to/checkpoint.ckpt
    

    Training Monitoring

    # Monitor with TensorBoard
    tensorboard --logdir runs/
    
    # Monitor with Weights & Biases
    wandb login
    python train/script/train.py --config train/config/wandb.yaml
    

    📚 Examples

    Character Insertion

    See examples/character_insert.ipynb for a complete example of inserting characters into scenes.

    Trained Model: Check out the omini-kontext-character model on Hugging Face, which is specifically trained to insert cartoon characters into existing scenes.

    🏗️ Model Architecture

    The Flux Omini Kontext pipeline consists of several key components:

    Base model

    Flux Kontext dev model

    LoRA Integration

    # LoRA layers are applied to attention modules
    target_modules = ["to_q", "to_k", "to_v", "to_out.0"]
    
    # LoRA configuration
    lora_config = {
        "r": 16,                    # Rank
        "lora_alpha": 32,           # Alpha scaling
        "lora_dropout": 0.1,        # Dropout rate
        "bias": "none",             # Bias handling
        "task_type": "CAUSAL_LM"    # Task type
    }
    

    Training Process

    1. Input Processing: Encode input and reference images
    2. Text Encoding: Process prompts with CLIP and T5
    3. LoRA Forward: Apply LoRA layers during forward pass
    4. Noise Prediction: Train to predict noise
    5. Loss Computation: MSE loss on noise prediction

    ⚙️ Configuration

    Pipeline Parameters

    ParameterTypeDefaultDescription
    imagePIL.ImageNoneInput image
    referencePIL.ImageNoneReference image
    reference_deltaList[int][0, 0, 0]Position offset for reference (specific to trained LoRA, recommended: [0, 0, (1024+512)//16])
    promptstrNoneText prompt
    prompt_2strNoneSecondary text prompt
    guidance_scalefloat3.5Classifier-free guidance scale
    num_inference_stepsint28Number of denoising steps
    heightint1024Output height
    widthint1024Output width

    Training Parameters

    ParameterTypeDefaultDescription
    learning_ratefloat1e-4Learning rate
    batch_sizeint1Training batch size
    max_epochsint10Maximum training epochs
    gradient_accumulation_stepsint1Gradient accumulation steps
    warmup_stepsint100Learning rate warmup steps

    ComfyUI Integration

    Simply clone this repo in your ComfyUI/custom_nodes folder. Using this integration will let you use the native ComfyUI nodes together with the OminiKontext nodes. There are two nodes in the repo -

    1. OminiKontextConditioning - To condition the model on a reference image, along with a delta value.
    2. OminiKontextModelPatch - Patch for the Kontext model.

    Drop this image in ComfyUI interface to load the workflow -


    Alternative ComfyUI integration -

    Repo link - https://github.com/tercumantanumut/ComfyUI-Omini-Kontext

    Thanks to tercumantanumut for the ComfyUI integration!

    Star History

    Star History Chart

    🤝 Contributing

    We welcome contributions! Please see our contributing guidelines:

    1. Fork the repository
    2. Create a feature branch
    3. Make your changes
    4. Add tests if applicable
    5. Submit a pull request

    📄 License

    This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

    🙏 Acknowledgments

    • Black Forest Labs for the FLUX.1-Kontext-dev model
    • HuggingFace for the diffusers library
    • PyTorch Lightning for the training framework
    • PEFT for LoRA implementation
    • OminiControl for the universal control framework for Diffusion Transformers
    • ComfyUI-Omini-Kontext for the ComfyUI integration

    📚 References

    @article{omini-kontext,
      title={OminiKontext: Multi-image references for image to image instruction models},
      author={Saquib Alam},
      year={2025}
    }
    

    📞 Support


    Made with ❤️ for the AI community