How caraj7/t2i-r1 works
Project Type and Purpose
• This is a research‐oriented, full‐stack machine‐learning pipeline for text‐to‐image generation (T2I) with reinforcement learning (RL) and Chain‐of‐Thought (CoT) reasoning.
• The core novelty is the BiCoT‐GRPO trainer that optimizes both semantic‐level and token‐level CoT during T2I model training.
High‐Level Architecture Components
a. Data Layer
• data/geneval_and_t2i_data_final.json: Prompt examples and ground truths
• data/prompt/reasoning_prompt.txt: Template for semantic‐level CoT
b. Configuration & Orchestration
• src/t2i-r1/configs/*.json: Zero3 and offload settings
• src/scripts/run_grpo.sh: Entry point for launching the BiCoT‐GRPO training
c. Semantic‐level CoT Module (“janus”)
• src/t2i-r1/src/janus/janusflow/models: CLIP encoder, VLM modeling, image processing
• Responsible for planning global image structure (object layout, attributes)
d. Token‐level CoT Generator (“open_r1”)
• src/t2i-r1/src/open_r1/trainer/grpo_trainer.py: GRPO algorithm implementation
• src/t2i-r1/src/open_r1/grpo.py: Core RL loop coordinating token‐by‐token generation
e. Reward Model Suite
• src/t2i-r1/src/utils/GroundingDINO: Object detection reward
• src/t2i-r1/src/utils/LLaVA-NeXT: ORM reward via LLaVA
• src/t2i-r1/src/utils/reward_*.py: wrappers for HPS, GIT, ORM, custom rewards
• These modules produce scalar rewards used by the GRPO trainer
f. Inference Module
• src/t2i-r1/src/infer/reason_inference.py: Runs semantic CoT + generator for testing or demo
g. External Dependencies & Environments
• Python, PyTorch, TorchVision, Transformers, TRL, Conda environment
• Dockerfile/ environment.yaml under GroundingDINO (for reproducibility)
Relationships and Data Flow
• User/data prompts → Semantic‐level CoT “janusflow” → text reasoning output
• Text reasoning + prompt → Token‐level CoT generator (open_r1) → image tokens
• Generated images → Reward Model Suite (GroundingDINO, LLaVA, HPS, GIT) → scalar rewards
• GRPO trainer ingests rewards + model gradients → updates both CoT policy and image generator
• Config files parameterize the flow (Zero3 offloading, checkpoint paths)
• Inference script uses the trained checkpoint for one‐shot generation
Architectural Patterns & Design Principles
• Modular separation of concerns: reasoning vs generation vs reward computation
• Pluggable reward functions: external repos are vendored under src/utils
• Configuration‐driven: high reuse of scripts and JSON configs for scaling and ablation
• Ensemble reward composition within a single RL training loop (BiCoT‐GRPO)
Capturing Essential Elements
• BiCoT‐GRPO feedback loop: CoT policies and generator policies updated by composite rewards
• Interaction between semantic and token CoT levels within the trainer
• External repos as sub‐components of the Reward Suite, noting they are pip‐installed from vendored code