ReSEM is a knowledge-grounded reasoning segmentation framework for electron microscopy images. It is designed to understand natural-language scientific queries, identify the relevant microscopic structures or material regions, produce precise segmentation masks, and provide explanatory responses grounded in domain knowledge. The project includes model checkpoints, training and inference scripts, and the ReasonEM dataset for building EM segmentation models with stronger reasoning and instruction-following abilities.
pip install -r requirements.txt
pip install flash-attn --no-build-isolation
The ReasonEM dataset can be downloaded from this link.
The training and evaluation data should be organized as follows.
├── dataset_path
│ ├── ceramic
│ │ ├── images
│ │ ├── labels
│ │ ├── train_d_qa.json
│ │ └── val_d_qa.json
...
The json files should contain the following fields:
[
{
"image_name": "images/...png",
"is_sentence": true,
"shapes": [
{
"label": "target",
"labels": [
"target"
],
"shape_type": "mask",
"image_name": "images/...png",
"mask_name": "labels/...png",
"class_name": "...",
"color_id": ...,
"description": "...",
"qa_list": [
{
"question": "...",
"answer": "..."
},
...
]
},
...
]
},
...
]
To train LISA-7B or 13B, you need to follow the instruction to merge the LLaVA delta weights. Typically, we use the final weights LLaVA-Lightning-7B-v1-1 and LLaVA-13B-v1-1 merged from liuhaotian/LLaVA-Lightning-7B-delta-v1-1 and liuhaotian/LLaVA-13b-delta-v1-1, respectively. For Llama2, we can directly use the LLaVA full weights liuhaotian/llava-llama-2-13b-chat-lightning-preview.
Download SAM ViT-H pre-trained weights from the link.
For sft training, run the following command:
deepspeed --master_port=24999 train_ds.py \
--version="PATH_TO_LLaVA" \
--dataset_dir='dataset_path' \
--vision_pretrained="PATH_TO_SAM" \
--dataset="reason_seg_em" \
--val_dataset="reason_seg_em" \
--reason_seg_data="organelle||ceramic..." \
--sample_rates="1" \
--exp_name="..." \
--steps_per_epoch="..." \
--epochs="..." \
--lr="..." \
--use_gpt_qa \
--train_from_scratch
For grpo training, run the following command:
deepspeed --master_port=24998 train_grpo_ds.py \
--version="sft_huggingface_path" \
--dataset_dir='dataset_path' \
--log_base_dir='runs' \
--vision_pretrained="PATH_TO_SAM" \
--dataset="reason_seg_em" \
--val_dataset="reason_seg_em" \
--reason_seg_data="organelle||ceramic..." \
--sample_rates="1" \
--batch_size="..." \
--num_generations="..." \
--exp_name="..." \
--steps_per_epoch="..." \
--epochs="..." \
--lr="..." \
--use_gpt_qa \
--lora_module_full_finetune \
--reward_weights ... \
Refer to scripts/train.sh for detailed examples and more training settings.
When training is finished, to get the full model weight:
cd ./runs/<exp_name>/ckpt_model && python zero_to_fp32.py . ../pytorch_model.bin
Merge the LoRA weights of pytorch_model.bin, save the resulting model into your desired path in the Hugging Face format:
CUDA_VISIBLE_DEVICES="" python merge_lora_weights_and_save_hf_model.py \
--version="PATH_TO_LLaVA" \
--weight="PATH_TO_pytorch_model.bin" \
--save_path="PATH_TO_SAVED_MODEL"
Refer to scripts/merge.sh for detailed examples and more settings.
deepspeed --master_port=24999 train_ds.py \
--version="PATH_TO_LISA_HF_Model_Directory" \
--dataset_dir='dataset_path' \
--vision_pretrained="PATH_TO_SAM" \
--dataset="reason_seg_em" \
--val_dataset="reason_seg_em" \
--reason_seg_data="organelle||ceramic..." \
--sample_rates="1" \
--exp_name="..." \
--steps_per_epoch="..." \
--epochs="..." \
--lr="..." \
--use_gpt_qa \
To chat with ReSEM-13B-llama2-sft or ReSEM-13B-llama2-grpo:
CUDA_VISIBLE_DEVICES=0 python batch_chat.py --version='ReSEM-13B-llama2-xxx'
python batch_chat.py --precision='bf16' \
--version="ReSEM-13B-llama2-xxx" \
--chat_json="chat_sample.json"
chat_sample.json is arranged as:
[
{
"image":"image_path",
"prompt": "...",
"class": "..."
},
...
]
If you find this project useful in your research, please consider citing:









