How baai-dcai/m3d works
Purpose and Entry Points
M3D is a Python/PyTorch system for 3D medical-image multimodal learning: image–text retrieval, report/caption generation, VQA, localization, and volumetric segmentation. Main operational entry points are Hugging Face AutoModelForCausalLM loading of the custom model, local demos, training programs, shell launchers, and per-task benchmark scripts. Paths: LaMed/src/demo/demo.py, LaMed/src/train/train.py, Bench/eval/eval_vqa.py.
Data Lifecycle
Raw M3D-Cap, VQA, Seg, and RefSeg datasets are externally downloaded, then transformed into local training/benchmark artifacts; example .npy volumes live under Data/data/examples/. Dataset adapters combine task sources, prompt formats, and medical-term dictionaries into model-ready multimodal samples. Paths: Data/process/m3d_cap_data_prepare.py, Data/process/m3d_seg_data_preprocess.py, LaMed/src/dataset/multi_dataset.py.
Multimodal Model Assembly
The central LaMed architecture combines a pretrained 3D visual encoder, a projector that converts visual features into language-model token space, and a selectable causal LLM implementation. Image patch placeholders in text are replaced/aligned with projected volume features before language generation. Paths: LaMed/src/model/lamed_arch.py, LaMed/src/model/multimodal_encoder/builder.py, LaMed/src/model/multimodal_projector/spatial_pooling_projector.py.
Language and Vision Backbones
Language backbones are custom Hugging Face remote-code-compatible variants for Llama and Phi-3. The visual side includes a 3D ViT and a separate M3D-CLIP training path used to obtain recommended vision weights. This establishes a boundary between external pretrained checkpoints and task-specific fusion/training. Paths: LaMed/src/model/language_model/lamed_llama.py, LaMed/src/model/language_model/lamed_phi3.py, LaMed/src/model/CLIP.py.
Segmentation Path
For mask-producing prompts, LaMed routes language-conditioned/task signals to SegVol, a volumetric Segment Anything-derived module. It contains image and prompt encoders, mask decoder, predictor, and automatic mask generator; inference returns generated text plus segmentation logits, later thresholded into masks. Paths: LaMed/src/model/segmentation_module/segvol.py, LaMed/src/model/segmentation_module/builder.py, LaMed/src/model/segmentation_module/segment_anything_volumetric/modeling/sam.py.
Training and Model Packaging
Training is explicitly staged: pretraining freezes vision and LLM while fitting the multimodal projector on image–text pairs; instruction tuning unfreezes non-LLM components and applies LoRA to the LLM across multitask data. A custom trainer coordinates this, while scripts select Llama/Phi variants and training phase. LoRA adapters can be merged into a Hugging Face-format model. Paths: LaMed/src/train/lamed_trainer.py, LaMed/script/pretrain_phi3.sh, LaMed/src/utils/merge_lora_weights_and_save_hf_model.py.
Evaluation, Runtime, and External Dependencies
Benchmark evaluation is a separate package with task-specific scoring for captioning, retrieval, positioning, segmentation, and VQA, plus optional LLM-based report evaluation. Runtime is Python with PyTorch, Transformers, NumPy, and GPU-oriented mixed/distributed training via Hugging Face Accelerate and DeepSpeed. External services/artifacts include Hugging Face model and dataset repositories, upstream Phi-3/Llama weights, M3D-CLIP weights, SegVol weights, and GPT-4V-oriented evaluation code. Paths: Bench/eval/eval_caption.py, Bench/eval/eval_seg.py, Bench/eval/eval_with_llm.py.
Main entry points: lamed_trainer.py LaMed trainer · lamed_arch.py LaMed multimodal assembly · merge_lora_weights_and_save_hf_model.py HF model packaging · multi_dataset.py Multitask dataset adapter
Components 21
Data pipeline
- M3D task datasets · Downloaded Cap, VQA, Seg, and RefSeg sources.
- Dataset preparation
Data/process/m3d_cap_data_prepare.py - Seg preprocessing
Data/process/m3d_seg_data_preprocess.py - Multitask dataset adapter · Combines task data, prompts, and medical terminology.
LaMed/src/dataset/multi_dataset.py - Example volumes
LaMed model
- 3D visual encoder
LaMed/src/model/multimodal_encoder/builder.py - Spatial pooling projector · Maps volume features into LLM token space.
LaMed/src/model/multimodal_projector/spatial_pooling_projector.py - LaMed multimodal assembly · Replaces image patch placeholders with projected visual features.
LaMed/src/model/lamed_arch.py - LaMed Llama
LaMed/src/model/language_model/lamed_llama.py - LaMed Phi-3
LaMed/src/model/language_model/lamed_phi3.py - SegVol · Provides image/prompt encoding and mask decoding.
LaMed/src/model/segmentation_module/segvol.py - Volumetric SAM model
LaMed/src/model/segmentation_module/segment_anything_volumetric/modeling/sam.py
Training & packaging
- M3D-CLIP training · Produces recommended vision weights.
LaMed/src/model/CLIP.py - Training entry point
LaMed/src/train/train.py - LaMed trainer · Runs projector pretraining and multitask LoRA instruction tuning.
LaMed/src/train/lamed_trainer.py - Phi-3 pretraining launcher
LaMed/script/pretrain_phi3.sh - HF model packaging · Merges adapters and saves Hugging Face model format.
LaMed/src/utils/merge_lora_weights_and_save_hf_model.py
Runtime & evaluation
- External checkpoints · Includes Llama/Phi-3, M3D-CLIP, and SegVol weights.
- Local demo
LaMed/src/demo/demo.py - Task benchmarks · Scores captioning, retrieval, localization, segmentation, and VQA.
Bench/eval/eval_vqa.py - LLM report evaluation
Bench/eval/eval_with_llm.py
Connections 25
- M3D task datasets → Dataset preparation: caption data
- M3D task datasets → Seg preprocessing: segmentation data
- Dataset preparation → Multitask dataset adapter: training artifacts
- Seg preprocessing → Multitask dataset adapter: mask artifacts
- Example volumes → Local demo: demo volumes
- Multitask dataset adapter → LaMed trainer: multitask samples
- Phi-3 pretraining launcher → Training entry point: selects phase
- Training entry point → LaMed trainer: configures
- LaMed trainer → LaMed multimodal assembly: trains
- LaMed trainer → Spatial pooling projector: pretrains then tunes
- LaMed trainer → HF model packaging: LoRA adapters
- M3D-CLIP training → 3D visual encoder: vision weights
- External checkpoints → 3D visual encoder: pretrained weights
- External checkpoints → LaMed Llama: base weights
- External checkpoints → LaMed Phi-3: base weights
- External checkpoints → SegVol: SegVol weights
- 3D visual encoder → Spatial pooling projector: volume features
- Spatial pooling projector → LaMed multimodal assembly: visual tokens
- LaMed Llama → LaMed multimodal assembly: selectable LLM
- LaMed Phi-3 → LaMed multimodal assembly: selectable LLM
- Volumetric SAM model → SegVol: SAM components
- LaMed multimodal assembly → SegVol: mask prompts
- HF model packaging → Local demo: loadable HF model
- HF model packaging → Task benchmarks: evaluated model
- Task benchmarks → LLM report evaluation: optional report scoring