A COM-304 project implementing the Multimodal Chain of Thought (MCoT) extension for the 4M vision model, inspired by the MINT paper. This project extends 4M's capabilities with a four-stage reasoning pipeline for enhanced complex image generation.
Our implementation replaces single-step text-to-image generation with a structured four-stage reasoning process:
Clone and Setup Environment
git clone <repository-url>
cd MCoT-4M-XL
# Activate the fourm environment (required)
conda activate fourm
# OR if using venv:
source fourm_env/bin/activate
Install Dependencies
pip install -r requirements.txt
torch>=2.0.0
torchvision>=0.15.0
transformers>=4.30.0
datasets>=2.12.0
Pillow>=9.5.0
numpy>=1.24.0
opencv-python>=4.7.0
safetensors>=0.3.1
wandb>=0.15.0
rclone>=1.60.0
fourm (can be installed using the 4M repository)
4M-XL-21 Fine-tuned Model
models/4m-xl-21-finetuned/4M-XL-T2I Model
models/4m-xl-t2i/# Install rclone if not already installed
curl https://rclone.org/install.sh | sudo bash
# Configure Google Drive access
rclone config
# Download models automatically
python scripts/download_models.py
models/
├── 4m-xl-21-finetuned/
│ ├── model.safetensors
│ ├── config.json
│ └── tokenizer.json
└── 4m-xl-t2i/
├── model.safetensors
├── config.json
└── tokenizer.json
MCoT-4M-XL/
├── fourm/ # Core 4M framework
│ ├── data/
│ │ ├── mcot_dataset.py # MCoT dataset integration
│ │ ├── unified_datasets.py # Multi-source dataset handling
│ │ └── pretrain_utils.py # Data preprocessing utilities
│ ├── models/
│ │ ├── fm.py # Base 4M transformer architecture
│ │ ├── mcot_fixed.py # MCoT wrapper and step processor
│ │ └── fm_utils.py # Model utilities and components
│ ├── utils/
│ │ ├── logger.py # Training logging and monitoring
│ │ └── tokenizer/ # Text tokenization utilities
│ └── vq/ # Vector quantization modules
├── cfgs/ # Configuration files
│ ├── default/4m/data/mcot/
│ │ └── mcot.yaml # MCoT data pipeline config
│ └── mcot_training_config.yaml # Main training configuration
├── configs/
│ └── mcot_data_config.json # Dataset integration settings
├── mcot_data/ # MCoT-specific datasets
│ ├── actplan.json # ActPlan dataset (385k+ entries)
│ ├── mcot_dataset_wget.py # Dataset download automation
│ └── mcot_torch_dataset.py # PyTorch dataset implementation
├── models/ # Downloaded model weights
├── scripts/ # Utility scripts
├── run_training_4m_mcot_fsdp.py # Main MCoT training script
├── fourm_inference.py # Inference and evaluation
├── fourm_inference_vqa.py # VQA-specific inference script
└── submit_4m_fsdp_multinode.sh # SLURM submission script
Run VQA fine-tuning to establish baseline:
# Fine-tune 4M on VQAv2 dataset using our VQAv2 training script
python vqav2_training_script.py \
--model_path models/4m-xl-21/ \
--dataset_path data/vqa2017/ \
--output_dir models/4m-xl-21-finetuned/ \
--epochs 4
# Run inference on fine-tuned VQA models
python fourm_inference_vqa.py \
--model_path models/4m-xl-21-finetuned/ \
--dataset_path data/vqa2017/ \
--output_dir results/vqa_baseline/ \
--split validation
The fourm_inference_vqa.py script performs comprehensive VQA inference with parameter sweeping:
Basic Usage:
python fourm_inference_vqa.py
Configuration:
Edit the script parameters at the top of main_inference():
model_name: HuggingFace model identifier (default: 'EPFL-VILAB/4M-7-T2I_XL_CC12M')finetuned_weights_path: Path to fine-tuned checkpoint (default: 'checkpoint-2.safetensors')test_cases_file: JSON file with test cases (default: 'test_cases.json')output_dir: Directory for results (default: 'vqa_outputs')Test Cases Format:
Create a test_cases.json file:
{
"test_cases": [
{
"image_path": "path/to/image1.jpg",
"questions": [
"What color is the car?",
"How many people are in the image?"
]
},
{
"image_path": "path/to/image2.jpg",
"questions": ["What is the person doing?", "What objects are visible?"]
}
]
}
Parameter Sweeping: The script automatically tests different generation parameters:
Output:
Results are saved as JSON in vqa_outputs/vqa_results.json with:
Single GPU Training:
python run_training_4m_mcot_fsdp.py \
--config cfgs/mcot_training_config.yaml \
--data_config cfgs/default/4m/data/mcot/mcot.yaml \
--output_dir checkpoints/mcot_training/
Multi-GPU Training (Recommended):
torchrun --nproc_per_node=8 run_training_4m_mcot_fsdp.py \
--config cfgs/mcot_training_config.yaml \
--data_config cfgs/default/4m/data/mcot/mcot.yaml \
--output_dir checkpoints/mcot_training/ \
--use_fsdp
SLURM Cluster Training:
sbatch submit_4m_fsdp_multinode.sh
python fourm_inference.py \
--model_path checkpoints/mcot_training/latest/ \
--task mcot_generation \
--prompt "A cat sitting on a red sofa in a living room" \
--steps planning,acting,reflection,correction \
--output_dir results/mcot_inference/
Generate ActPlan dataset from MS COCO:
cd mcot_data/
python mcot_dataset_wget.py --create_actplan --num_workers 8
Our VQA baseline experiments (4 epochs, VQAv2 2017):
Current Status: The complete MCoT training pipeline has been implemented and is ready for deployment. However, due to time constraints and numerous technical challenges encountered during unified dataset creation and integration, we were unable to complete the full training process and run inference on the trained MCoT model.
Implementation Completeness:
Technical Challenges Encountered:
Results Availability: Training results and performance metrics are available at:
cfgs/mcot_training_config.yaml)Key parameters you can modify:
model: "fm_base_12e_12d_swiglu_nobias"
batch_size: 32
epochs: 2
blr: 5e-5
mcot_steps: "planning,acting,reflection,correction"
mcot_planning_weight: 1.0
mcot_acting_weight: 1.2
mcot_reflection_weight: 1.5
mcot_correction_weight: 1.3
enable_mint_features: true
use_act_checkpoint: true
dtype: "bfloat16"
clip_grad: 1.0
cfgs/default/4m/data/mcot/mcot.yaml)Configure dataset sources and preprocessing:
datasets:
- actplan_dataset
- richhf_18k
- seetrue_feedback
- brush_data
preprocessing:
image_size: 224
text_max_length: 512
enable_augmentation: true
CUDA Out of Memory
batch_size: 16
use_act_checkpoint: true
Missing Model Weights
ls -la models/
python scripts/download_models.py
Dataset Loading Errors
rm -rf data/cache/
python mcot_data/mcot_dataset_wget.py --force_download
Note: This is a research project developed for COM-304. The implementation builds upon the original 4M framework and extends it with MCoT capabilities for enhanced multimodal reasoning.
A COM-304 project implementing the Multimodal Chain of Thought (MCoT) extension for the 4M vision model, inspired by the MINT paper. This project extends 4M's capabilities with a four-stage reasoning pipeline for enhanced complex image generation.
Our implementation replaces single-step text-to-image generation with a structured four-stage reasoning process:
Clone and Setup Environment
git clone <repository-url>
cd MCoT-4M-XL
# Activate the fourm environment (required)
conda activate fourm
# OR if using venv:
source fourm_env/bin/activate
Install Dependencies
pip install -r requirements.txt
torch>=2.0.0
torchvision>=0.15.0
transformers>=4.30.0
datasets>=2.12.0
Pillow>=9.5.0
numpy>=1.24.0
opencv-python>=4.7.0
safetensors>=0.3.1
wandb>=0.15.0
rclone>=1.60.0
fourm (can be installed using the 4M repository)
4M-XL-21 Fine-tuned Model
models/4m-xl-21-finetuned/4M-XL-T2I Model
models/4m-xl-t2i/# Install rclone if not already installed
curl https://rclone.org/install.sh | sudo bash
# Configure Google Drive access
rclone config
# Download models automatically
python scripts/download_models.py
models/
├── 4m-xl-21-finetuned/
│ ├── model.safetensors
│ ├── config.json
│ └── tokenizer.json
└── 4m-xl-t2i/
├── model.safetensors
├── config.json
└── tokenizer.json
MCoT-4M-XL/
├── fourm/ # Core 4M framework
│ ├── data/
│ │ ├── mcot_dataset.py # MCoT dataset integration
│ │ ├── unified_datasets.py # Multi-source dataset handling
│ │ └── pretrain_utils.py # Data preprocessing utilities
│ ├── models/
│ │ ├── fm.py # Base 4M transformer architecture
│ │ ├── mcot_fixed.py # MCoT wrapper and step processor
│ │ └── fm_utils.py # Model utilities and components
│ ├── utils/
│ │ ├── logger.py # Training logging and monitoring
│ │ └── tokenizer/ # Text tokenization utilities
│ └── vq/ # Vector quantization modules
├── cfgs/ # Configuration files
│ ├── default/4m/data/mcot/
│ │ └── mcot.yaml # MCoT data pipeline config
│ └── mcot_training_config.yaml # Main training configuration
├── configs/
│ └── mcot_data_config.json # Dataset integration settings
├── mcot_data/ # MCoT-specific datasets
│ ├── actplan.json # ActPlan dataset (385k+ entries)
│ ├── mcot_dataset_wget.py # Dataset download automation
│ └── mcot_torch_dataset.py # PyTorch dataset implementation
├── models/ # Downloaded model weights
├── scripts/ # Utility scripts
├── run_training_4m_mcot_fsdp.py # Main MCoT training script
├── fourm_inference.py # Inference and evaluation
├── fourm_inference_vqa.py # VQA-specific inference script
└── submit_4m_fsdp_multinode.sh # SLURM submission script
Run VQA fine-tuning to establish baseline:
# Fine-tune 4M on VQAv2 dataset using our VQAv2 training script
python vqav2_training_script.py \
--model_path models/4m-xl-21/ \
--dataset_path data/vqa2017/ \
--output_dir models/4m-xl-21-finetuned/ \
--epochs 4
# Run inference on fine-tuned VQA models
python fourm_inference_vqa.py \
--model_path models/4m-xl-21-finetuned/ \
--dataset_path data/vqa2017/ \
--output_dir results/vqa_baseline/ \
--split validation
The fourm_inference_vqa.py script performs comprehensive VQA inference with parameter sweeping:
Basic Usage:
python fourm_inference_vqa.py
Configuration:
Edit the script parameters at the top of main_inference():
model_name: HuggingFace model identifier (default: 'EPFL-VILAB/4M-7-T2I_XL_CC12M')finetuned_weights_path: Path to fine-tuned checkpoint (default: 'checkpoint-2.safetensors')test_cases_file: JSON file with test cases (default: 'test_cases.json')output_dir: Directory for results (default: 'vqa_outputs')Test Cases Format:
Create a test_cases.json file:
{
"test_cases": [
{
"image_path": "path/to/image1.jpg",
"questions": [
"What color is the car?",
"How many people are in the image?"
]
},
{
"image_path": "path/to/image2.jpg",
"questions": ["What is the person doing?", "What objects are visible?"]
}
]
}
Parameter Sweeping: The script automatically tests different generation parameters:
Output:
Results are saved as JSON in vqa_outputs/vqa_results.json with:
Single GPU Training:
python run_training_4m_mcot_fsdp.py \
--config cfgs/mcot_training_config.yaml \
--data_config cfgs/default/4m/data/mcot/mcot.yaml \
--output_dir checkpoints/mcot_training/
Multi-GPU Training (Recommended):
torchrun --nproc_per_node=8 run_training_4m_mcot_fsdp.py \
--config cfgs/mcot_training_config.yaml \
--data_config cfgs/default/4m/data/mcot/mcot.yaml \
--output_dir checkpoints/mcot_training/ \
--use_fsdp
SLURM Cluster Training:
sbatch submit_4m_fsdp_multinode.sh
python fourm_inference.py \
--model_path checkpoints/mcot_training/latest/ \
--task mcot_generation \
--prompt "A cat sitting on a red sofa in a living room" \
--steps planning,acting,reflection,correction \
--output_dir results/mcot_inference/
Generate ActPlan dataset from MS COCO:
cd mcot_data/
python mcot_dataset_wget.py --create_actplan --num_workers 8
Our VQA baseline experiments (4 epochs, VQAv2 2017):
Current Status: The complete MCoT training pipeline has been implemented and is ready for deployment. However, due to time constraints and numerous technical challenges encountered during unified dataset creation and integration, we were unable to complete the full training process and run inference on the trained MCoT model.
Implementation Completeness:
Technical Challenges Encountered:
Results Availability: Training results and performance metrics are available at:
cfgs/mcot_training_config.yaml)Key parameters you can modify:
model: "fm_base_12e_12d_swiglu_nobias"
batch_size: 32
epochs: 2
blr: 5e-5
mcot_steps: "planning,acting,reflection,correction"
mcot_planning_weight: 1.0
mcot_acting_weight: 1.2
mcot_reflection_weight: 1.5
mcot_correction_weight: 1.3
enable_mint_features: true
use_act_checkpoint: true
dtype: "bfloat16"
clip_grad: 1.0
cfgs/default/4m/data/mcot/mcot.yaml)Configure dataset sources and preprocessing:
datasets:
- actplan_dataset
- richhf_18k
- seetrue_feedback
- brush_data
preprocessing:
image_size: 224
text_max_length: 512
enable_augmentation: true
CUDA Out of Memory
batch_size: 16
use_act_checkpoint: true
Missing Model Weights
ls -la models/
python scripts/download_models.py
Dataset Loading Errors
rm -rf data/cache/
python mcot_data/mcot_dataset_wget.py --force_download
Note: This is a research project developed for COM-304. The implementation builds upon the original 4M framework and extends it with MCoT capabilities for enhanced multimodal reasoning.