MedMax: Mixed-Modal Instruction Tuning for Training Biomedical Assistants
[Webpage] [Paper] [Train Dataset 🤗] [Eval Dataset 🤗] [Model 🤗] [Demo 🤗]
.
Recent advancements in mixed-modal generative models have unlocked seamless multimodal information integration, opening transformative possibilities for biomedical AI in image analysis, diagnosis, and dataset creation. However, existing resources are limited data availability, narrow domain coverage, and restricted origins (e.g., medical papers). To address these gaps, we present MedMax, the first large-scale multimodal biomedical instruction-tuning dataset for mixed-modal foundation models. With 1.47 million instances, MedMax encompasses a diverse range of tasks, including multimodal content generation (interleaved image-text data), biomedical image captioning and generation, visual chatting, and report understanding. These tasks span diverse medical domains such as radiology and histopathology. Subsequently, we fine-tune a mixed-modal foundation model on the MedMax dataset, achieving significant performance improvements: a 26% gain over the base Chameleon model and an 18.3% improvement over GPT-4o across 12 downstream biomedical visual question-answering tasks. Additionally, we introduce a unified evaluation suite for biomedical tasks, providing a robust framework to guide the development of next-generation mixed-modal biomedical AI assistants.
Main results.
conda create -n medmax python=3.10
conda activate medmax
pip install -r requirements.txt
pip uninstall transformers.First, install gradio through pip install gradio==5.6.0
Then, Use the following command to launch gradio demo
mkdir -p .gradio
GRADIO_SERVER_NAME=0.0.0.0 GRADIO_TEMP_DIR=.gradio python demo.py -c <your checkpoint folder>
We also give example code for custom inference which can be run with
CUDA_VISIBLE_DEVICES=0 python -m inference.custom_inference
Request access to the MedMax Evaluation Data at https://huggingface.co/datasets/mint-medmax/medmax_eval_data.
Once granted access, install the data to your desired directory eval_data_dir. Unzip the images folder in the cloned data repository.
cd <eval_data_dir>
git lfs install
git clone https://huggingface.co/datasets/mint-medmax/medmax_eval_data
cd medmax_eval_data
tar -xzvf images.tar.gz -C .
Open ended evaluations require an OpenAI API access key which should be entered in evaluations/const.py e.g.
OAI_KEY = <your key here>
Install the MedMax model checkpoint to a predetermined directory ckpt_dir
git lfs install
git clone https://huggingface.co/mint-medmax/medmax_7b
To run the evaluation suite for MedMax 7B run
CUDA_VISIBLE_DEVICES=0 python -m evaluation.eval --ckpt <ckpt_dir> --prompt_processor sft --eval_data_dir <eval_data_dir> --save_dir <output_location> --save_name <save_file_name>
To run the evaluation suite for Chameleon 7B run
CUDA_VISIBLE_DEVICES=0 python -m evaluation.eval --ckpt <ckpt_dir> --prompt_processor chameleon --eval_data_dir <eval_data_dir> --save_dir <output_location> --save_name <save_file_name>
image_path and tokens for credential=YES split.credential=NO split, you can directly skip to the finetuning section.credential=YES split.Specifically, we provide the instructions to get the tokens column once you access to the image_path for the remaining datasets.
img_path column. CUDA_VISIBLE_DEVICES=0,1 torchrun -m --nproc-per-node=2 src.image_tokenization --input <path to a csv file> --output <path to save folder> --ckpt <path to medmax folder>
parquet files with an additional column of img_tokens.img_tokens should range from 0-8191 (8192 tokens). text: the multimodal text with <image> placeholder (this is present in our original dataset)
image_path: path to the image
image_tokens: image tokens from the VQGAN tokenizer (as described in the previous section)
source: (this is present in our original dataset)
task: (this is present in our original dataset)
python src/tokenization.py --input_file <input jsonl file> --tokenizer_file <tokenizer> --output_file <output jsonl filename>
text_tokenized_modified.json on huggingface - https://huggingface.co/mint-medmax/medmax_7b/blob/main/tokenizer/text_tokenizer_modified.json.credential=yes or get the tokens for the same using the instructions above.tokens element in each row of the jsonl data.CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 -m training.train --train_data <train.jsonl> --val_data <val.jsonl> --ckpt <path to Anole-7b-v0.1-hf> --ds training/ds_config.json --output_dir <path to output dir> --epoch 3 --bs 1 --save_strategy steps --warmup_ratio 0.1 --name <exp name> --lr 1e-4 --bf16 --wandb --wandb_entity <name of wandb entity>
CUDA_VISIBLE_DEVICES=1 python -m src.load_lora_and_merge --ckpt_path <path to output dir/checkpoint-number> --output_dir <path to the output dir for the merged model> --base_path <path to the Anole-7b-1.0-hf model>
CUDA_VISIBLE_DEVICES=1 python -m src.bin_to_pth --trained_ckpt <path to the merged checkpoint> --original_ckpt <original Anole model in the chameleon's format> --new_ckpt <save folder with the chameleon's format>
Note: During finetuning, there might be an error due to versioning of the transformers and deepspeed. To fix this, we comment this line that was throwing the error.
# if compare_versions("transformers", "<", "4.33"):
# from transformers.deepspeed import HfDeepSpeedConfig, unset_hf_deepspeed_config
# else:
from transformers.integrations import HfDeepSpeedConfig, unset_hf_deepspeed_config
in "/opt/conda/envs/medmax/lib/python3.10/site-packages/accelerate/utils/dataclasses.py", line 1295.
CUDA_VISIBLE_DEVICES=0 python -m transformers.models.chameleon.convert_chameleon_weights_to_hf --input_dir medmax_7b --model_size 7B --output_dir medmax_7b_hf
See Instructions
Python
98.9%
MedMax: Mixed-Modal Instruction Tuning for Training Biomedical Assistants
[Webpage] [Paper] [Train Dataset 🤗] [Eval Dataset 🤗] [Model 🤗] [Demo 🤗]
.
Recent advancements in mixed-modal generative models have unlocked seamless multimodal information integration, opening transformative possibilities for biomedical AI in image analysis, diagnosis, and dataset creation. However, existing resources are limited data availability, narrow domain coverage, and restricted origins (e.g., medical papers). To address these gaps, we present MedMax, the first large-scale multimodal biomedical instruction-tuning dataset for mixed-modal foundation models. With 1.47 million instances, MedMax encompasses a diverse range of tasks, including multimodal content generation (interleaved image-text data), biomedical image captioning and generation, visual chatting, and report understanding. These tasks span diverse medical domains such as radiology and histopathology. Subsequently, we fine-tune a mixed-modal foundation model on the MedMax dataset, achieving significant performance improvements: a 26% gain over the base Chameleon model and an 18.3% improvement over GPT-4o across 12 downstream biomedical visual question-answering tasks. Additionally, we introduce a unified evaluation suite for biomedical tasks, providing a robust framework to guide the development of next-generation mixed-modal biomedical AI assistants.
Main results.
conda create -n medmax python=3.10
conda activate medmax
pip install -r requirements.txt
pip uninstall transformers.First, install gradio through pip install gradio==5.6.0
Then, Use the following command to launch gradio demo
mkdir -p .gradio
GRADIO_SERVER_NAME=0.0.0.0 GRADIO_TEMP_DIR=.gradio python demo.py -c <your checkpoint folder>
We also give example code for custom inference which can be run with
CUDA_VISIBLE_DEVICES=0 python -m inference.custom_inference
Request access to the MedMax Evaluation Data at https://huggingface.co/datasets/mint-medmax/medmax_eval_data.
Once granted access, install the data to your desired directory eval_data_dir. Unzip the images folder in the cloned data repository.
cd <eval_data_dir>
git lfs install
git clone https://huggingface.co/datasets/mint-medmax/medmax_eval_data
cd medmax_eval_data
tar -xzvf images.tar.gz -C .
Open ended evaluations require an OpenAI API access key which should be entered in evaluations/const.py e.g.
OAI_KEY = <your key here>
Install the MedMax model checkpoint to a predetermined directory ckpt_dir
git lfs install
git clone https://huggingface.co/mint-medmax/medmax_7b
To run the evaluation suite for MedMax 7B run
CUDA_VISIBLE_DEVICES=0 python -m evaluation.eval --ckpt <ckpt_dir> --prompt_processor sft --eval_data_dir <eval_data_dir> --save_dir <output_location> --save_name <save_file_name>
To run the evaluation suite for Chameleon 7B run
CUDA_VISIBLE_DEVICES=0 python -m evaluation.eval --ckpt <ckpt_dir> --prompt_processor chameleon --eval_data_dir <eval_data_dir> --save_dir <output_location> --save_name <save_file_name>
image_path and tokens for credential=YES split.credential=NO split, you can directly skip to the finetuning section.credential=YES split.Specifically, we provide the instructions to get the tokens column once you access to the image_path for the remaining datasets.
img_path column. CUDA_VISIBLE_DEVICES=0,1 torchrun -m --nproc-per-node=2 src.image_tokenization --input <path to a csv file> --output <path to save folder> --ckpt <path to medmax folder>
parquet files with an additional column of img_tokens.img_tokens should range from 0-8191 (8192 tokens). text: the multimodal text with <image> placeholder (this is present in our original dataset)
image_path: path to the image
image_tokens: image tokens from the VQGAN tokenizer (as described in the previous section)
source: (this is present in our original dataset)
task: (this is present in our original dataset)
python src/tokenization.py --input_file <input jsonl file> --tokenizer_file <tokenizer> --output_file <output jsonl filename>
text_tokenized_modified.json on huggingface - https://huggingface.co/mint-medmax/medmax_7b/blob/main/tokenizer/text_tokenizer_modified.json.credential=yes or get the tokens for the same using the instructions above.tokens element in each row of the jsonl data.CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 -m training.train --train_data <train.jsonl> --val_data <val.jsonl> --ckpt <path to Anole-7b-v0.1-hf> --ds training/ds_config.json --output_dir <path to output dir> --epoch 3 --bs 1 --save_strategy steps --warmup_ratio 0.1 --name <exp name> --lr 1e-4 --bf16 --wandb --wandb_entity <name of wandb entity>
CUDA_VISIBLE_DEVICES=1 python -m src.load_lora_and_merge --ckpt_path <path to output dir/checkpoint-number> --output_dir <path to the output dir for the merged model> --base_path <path to the Anole-7b-1.0-hf model>
CUDA_VISIBLE_DEVICES=1 python -m src.bin_to_pth --trained_ckpt <path to the merged checkpoint> --original_ckpt <original Anole model in the chameleon's format> --new_ckpt <save folder with the chameleon's format>
Note: During finetuning, there might be an error due to versioning of the transformers and deepspeed. To fix this, we comment this line that was throwing the error.
# if compare_versions("transformers", "<", "4.33"):
# from transformers.deepspeed import HfDeepSpeedConfig, unset_hf_deepspeed_config
# else:
from transformers.integrations import HfDeepSpeedConfig, unset_hf_deepspeed_config
in "/opt/conda/envs/medmax/lib/python3.10/site-packages/accelerate/utils/dataclasses.py", line 1295.
CUDA_VISIBLE_DEVICES=0 python -m transformers.models.chameleon.convert_chameleon_weights_to_hf --input_dir medmax_7b --model_size 7B --output_dir medmax_7b_hf
See Instructions
Python
98.9%