1nslyn/REG2025-slidechat-training

0

stars

10

commits

Python

primary language

Aug 21, 2025

updated

README

SlideChat-Virchow2: Pathology Report Generation with 2560D Feature Adaptation

Python 3.8+ PyTorch License

SlideChat-Virchow2 is an advanced pathology report generation system that adapts the SlideChat architecture to leverage 2560-dimensional Virchow2 features for automated WSI report generation. This repository contains the complete training pipeline for the REG2025 Challenge.

🔬 Overview

This system addresses automated pathology report generation from whole-slide images (WSIs) through three key innovations:

  • Dynamic Feature Adaptation: Extended SlideChat from 512D to 2560D Virchow2 embeddings
  • LongNet Architecture: Efficient processing of long patch sequences with dilated attention
  • Two-Stage Training: Cross-domain alignment + visual instruction fine-tuning with LoRA

✨ Key Features

  • 🧬 2560D Virchow2 Features: Full support for high-dimensional pathology foundation model features
  • 🚀 TRIDENT Integration: Complete WSI preprocessing pipeline with tissue segmentation
  • 📊 REG2025 Metrics: Comprehensive evaluation with ROUGE, BLEU, KEY, and EMB scores
  • 🎯 Production-Ready: Scalable to 8000+ samples on HPC clusters
  • 📈 W&B Monitoring: Real-time training visualization and experiment tracking
  • 💾 Memory Optimized: DeepSpeed ZeRO-2, 4-bit quantization, gradient accumulation

🚀 Quick Start

Environment Setup

# Activate the reg2025 conda environment
conda activate reg2025

# Install dependencies
pip install -r requirements.txt

Training

# Stage 1: Cross-domain alignment
python scripts/run_training.py --stage 1

# Stage 2: Visual instruction fine-tuning (automatically loads best Stage 1 checkpoint)
python scripts/run_training.py --stage 2 --stage1-checkpoint models/run_X/checkpoints/iter_Y.pth

Evaluation

# Evaluate trained model
python scripts/evaluate_reg2025.py --model_path models/run_X --test_data data/annotations/val.json

🏗️ Architecture

Core Components

  1. WSI Preprocessing (TRIDENT/)

    • Tissue segmentation with HEST
    • Patch extraction at 20× magnification
    • Virchow2 feature encoding (2560D)
  2. SlideChat Model (SlideChat/xtuner/)

    • Patch Encoder: CONCH features → contextual embeddings
    • Slide Encoder: LongNet architecture for WSI sequences
    • Multimodal Projector: 2560D → 1024D dimension alignment
    • Base LLM: Qwen2.5-7B-Instruct with 4-bit quantization
  3. Training Pipeline

    • Stage 1: Projector-only training for cross-domain alignment
    • Stage 2: Full model fine-tuning with LoRA adaptation
    • Validation Hook: Integrated REG2025 metrics during training

Data Flow

WSI → TRIDENT → 2560D Features → SlideChat Encoder → LongNet → Qwen2.5 → Report

📁 Project Structure

├── SlideChat/              # Modified XTuner framework with 2560D support
├── TRIDENT/                # WSI preprocessing toolkit
├── reg/                    # REG2025 evaluation metrics
├── configs/                # Training configurations
│   ├── reg2025_stage1.py  # Stage 1: Cross-domain alignment
│   └── reg2025_stage2.py  # Stage 2: Visual instruction tuning
├── scripts/                # Training and evaluation scripts
├── data/                   # Training data and features
├── models/                 # Model checkpoints and outputs
└── manuscript/             # Research paper and documentation

📊 Results

Training Performance

  • Dataset: 10 processed WSI samples (PIT_01_*)
  • Training Time: ~12 hours (800 iterations, Stage 1)
  • Final Loss: 0.0021 (Stage 1)
  • Memory Usage: ~8-12GB GPU with optimizations

REG2025 Evaluation Metrics

  • Best REG2025 Score: 0.276
  • ROUGE-L F1: 0.012
  • BLEU-4: 0.0
  • KEY Jaccard: 0.014
  • EMB Cosine: 0.767

Formula: REG2025 Score = 0.15 × (ROUGE + BLEU) + 0.4 × KEY + 0.3 × EMB

⚙️ Configuration

Stage 1 (Cross-domain Alignment)

# Key settings for projector-only training
freeze_llm = True
freeze_visual_encoder = True
batch_size = 1
accumulative_counts = 4
lr = 5e-4
max_epochs = 100
save_steps = 50
evaluation_freq = 50

Stage 2 (Visual Instruction Fine-tuning)

# Settings for full model fine-tuning
freeze_llm = False
lora_config = dict(r=16, alpha=32)
batch_size = 1
accumulative_counts = 8
lr = 2e-5
max_epochs = 50

🔧 Key Modifications

SlideChat Adaptations

  • SlideChat/xtuner/dataset/utils.py: Dynamic 2560D feature loading
  • SlideChat/xtuner/dataset/llava.py: WSI-specific data handling
  • SlideChat/xtuner/model/llava.py: LongNet integration for long sequences

Custom Components

  • scripts/reg2025_validation_hook.py: MMEngine hook for integrated evaluation
  • scripts/fix_slidechat_dimensions.py: Automated patching for 2560D support

📖 Citation

If you use this work, please cite:

@article{yin2025slidechat,
  title={SlideChat-Virchow2: Pathology Report Generation with 2560D Feature Adaptation and LongNet Architecture for REG2025 Challenge},
  author={Yin, Shuolin and Ma, Jun},
  journal={arXiv preprint},
  year={2025}
}

🤝 Acknowledgments

  • SlideChat: Foundation vision-language architecture
  • TRIDENT: WSI preprocessing and feature extraction toolkit
  • Virchow2: 2560D pathology foundation model features
  • XTuner: Training framework and optimization utilities

📄 License

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

Contributors

1nslyn

10 commits

1nslyn/REG2025-slidechat-training

0

stars

10

commits

Python

primary language

Aug 21, 2025

updated

README

SlideChat-Virchow2: Pathology Report Generation with 2560D Feature Adaptation

Python 3.8+ PyTorch License

SlideChat-Virchow2 is an advanced pathology report generation system that adapts the SlideChat architecture to leverage 2560-dimensional Virchow2 features for automated WSI report generation. This repository contains the complete training pipeline for the REG2025 Challenge.

🔬 Overview

This system addresses automated pathology report generation from whole-slide images (WSIs) through three key innovations:

  • Dynamic Feature Adaptation: Extended SlideChat from 512D to 2560D Virchow2 embeddings
  • LongNet Architecture: Efficient processing of long patch sequences with dilated attention
  • Two-Stage Training: Cross-domain alignment + visual instruction fine-tuning with LoRA

✨ Key Features

  • 🧬 2560D Virchow2 Features: Full support for high-dimensional pathology foundation model features
  • 🚀 TRIDENT Integration: Complete WSI preprocessing pipeline with tissue segmentation
  • 📊 REG2025 Metrics: Comprehensive evaluation with ROUGE, BLEU, KEY, and EMB scores
  • 🎯 Production-Ready: Scalable to 8000+ samples on HPC clusters
  • 📈 W&B Monitoring: Real-time training visualization and experiment tracking
  • 💾 Memory Optimized: DeepSpeed ZeRO-2, 4-bit quantization, gradient accumulation

🚀 Quick Start

Environment Setup

# Activate the reg2025 conda environment
conda activate reg2025

# Install dependencies
pip install -r requirements.txt

Training

# Stage 1: Cross-domain alignment
python scripts/run_training.py --stage 1

# Stage 2: Visual instruction fine-tuning (automatically loads best Stage 1 checkpoint)
python scripts/run_training.py --stage 2 --stage1-checkpoint models/run_X/checkpoints/iter_Y.pth

Evaluation

# Evaluate trained model
python scripts/evaluate_reg2025.py --model_path models/run_X --test_data data/annotations/val.json

🏗️ Architecture

Core Components

  1. WSI Preprocessing (TRIDENT/)

    • Tissue segmentation with HEST
    • Patch extraction at 20× magnification
    • Virchow2 feature encoding (2560D)
  2. SlideChat Model (SlideChat/xtuner/)

    • Patch Encoder: CONCH features → contextual embeddings
    • Slide Encoder: LongNet architecture for WSI sequences
    • Multimodal Projector: 2560D → 1024D dimension alignment
    • Base LLM: Qwen2.5-7B-Instruct with 4-bit quantization
  3. Training Pipeline

    • Stage 1: Projector-only training for cross-domain alignment
    • Stage 2: Full model fine-tuning with LoRA adaptation
    • Validation Hook: Integrated REG2025 metrics during training

Data Flow

WSI → TRIDENT → 2560D Features → SlideChat Encoder → LongNet → Qwen2.5 → Report

📁 Project Structure

├── SlideChat/              # Modified XTuner framework with 2560D support
├── TRIDENT/                # WSI preprocessing toolkit
├── reg/                    # REG2025 evaluation metrics
├── configs/                # Training configurations
│   ├── reg2025_stage1.py  # Stage 1: Cross-domain alignment
│   └── reg2025_stage2.py  # Stage 2: Visual instruction tuning
├── scripts/                # Training and evaluation scripts
├── data/                   # Training data and features
├── models/                 # Model checkpoints and outputs
└── manuscript/             # Research paper and documentation

📊 Results

Training Performance

  • Dataset: 10 processed WSI samples (PIT_01_*)
  • Training Time: ~12 hours (800 iterations, Stage 1)
  • Final Loss: 0.0021 (Stage 1)
  • Memory Usage: ~8-12GB GPU with optimizations

REG2025 Evaluation Metrics

  • Best REG2025 Score: 0.276
  • ROUGE-L F1: 0.012
  • BLEU-4: 0.0
  • KEY Jaccard: 0.014
  • EMB Cosine: 0.767

Formula: REG2025 Score = 0.15 × (ROUGE + BLEU) + 0.4 × KEY + 0.3 × EMB

⚙️ Configuration

Stage 1 (Cross-domain Alignment)

# Key settings for projector-only training
freeze_llm = True
freeze_visual_encoder = True
batch_size = 1
accumulative_counts = 4
lr = 5e-4
max_epochs = 100
save_steps = 50
evaluation_freq = 50

Stage 2 (Visual Instruction Fine-tuning)

# Settings for full model fine-tuning
freeze_llm = False
lora_config = dict(r=16, alpha=32)
batch_size = 1
accumulative_counts = 8
lr = 2e-5
max_epochs = 50

🔧 Key Modifications

SlideChat Adaptations

  • SlideChat/xtuner/dataset/utils.py: Dynamic 2560D feature loading
  • SlideChat/xtuner/dataset/llava.py: WSI-specific data handling
  • SlideChat/xtuner/model/llava.py: LongNet integration for long sequences

Custom Components

  • scripts/reg2025_validation_hook.py: MMEngine hook for integrated evaluation
  • scripts/fix_slidechat_dimensions.py: Automated patching for 2560D support

📖 Citation

If you use this work, please cite:

@article{yin2025slidechat,
  title={SlideChat-Virchow2: Pathology Report Generation with 2560D Feature Adaptation and LongNet Architecture for REG2025 Challenge},
  author={Yin, Shuolin and Ma, Jun},
  journal={arXiv preprint},
  year={2025}
}

🤝 Acknowledgments

  • SlideChat: Foundation vision-language architecture
  • TRIDENT: WSI preprocessing and feature extraction toolkit
  • Virchow2: 2560D pathology foundation model features
  • XTuner: Training framework and optimization utilities

📄 License

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

Contributors

1nslyn

10 commits

Languages

Python

96.9%

TeX

1.9%

Jupyter Notebook

1.1%