For the Chinese version of the README, please refer to 中文文档.
dataprocess folder, and dataset-related code is in the dataset folder. Data preprocessing mainly includes path merging, QA data concatenation, feature insertion token processing, etc.qwen folder. By overriding the forward method of QWenModel, multimodal feature injection is achieved.CLIP_VIT and SIGLIP_VIT, with relevant code in the visual folder, which also includes other backbone networks.model.py file under the model folder.We use a multilingual dataset, mainly including the COCO2017 dataset and the AI Challenger image Chinese description dataset:
detail_23k and complex_reasoning_77k, which can effectively enhance the richness of the model's descriptions.In VLM, the visual part uses the CLIP or SIGLIP model, which has already achieved preliminary semantic alignment, and uses a two-layer MLP for feature mapping. By overriding the forward method of QWenModel, the corresponding image tokens are replaced with visual features.
If you wish to replace the model architecture, please modify this part.
| AI Challenger | COCO | complex_reasoning_77k.json | detail_23k.json |
|---|---|---|---|
| AI Challenger | COCO 2017 | complex_reasoning_77k.json | detail_23k.json |
Please store the datasets according to the paths in the configuration file. Of course, the paths can be customized.
Please note that this path needs to be consistent with data/ for the model to read.
After downloading the data, use process_image.py for preprocessing.
Use pip install to install requirements.txt:
pip install -r requirements.txt
Model training adopts the method of freezing the image model, and LLM uses the LoRA method to reduce training pressure. The parameters to be trained include the visual feature mapping layer and the LoRA parameters in the LLM. Since the mapping layer is initialized with untrained parameters, to balance the optimization speed of the model parameters, a larger learning rate is set for the mapping layer than for the LoRA part.
Run the train.sh in the root directory, and you can configure the relevant parameters for experiments.
sh train.sh
Through the above steps, you can start the training process and train the multimodal model.
The model weights will be saved in the --output_dir, and this path can also be customized.
train.sh Script AnalysisCUDA_VISIBLE_DEVICES=0 torchrun --nproc_per_node=1 --master_port=25642 train.py \
--lora_rank 128 \
--lora_dropout 0.10 \
--per_device_train_batch_size 4 \
--gradient_accumulation_steps 1 \
--num_train_epochs 2 \
--save_steps 1000 \
--save_total_limit 5 \
--learning_rate 3e-5 \
--seed 42 \
--ddp_find_unused_parameters False \
--feature_proj_lr 1e-4 \
--remove_unused_columns false \
--logging_steps 100 \
--output_dir ./weights/train_V1_5 \
--target_modules "c_attn|w1|w2" \
--image_map /home/u2023111315/Basic-Vision-Language-Model/data/image_map_b.json \
--captions_file /home/u2023111315/Basic-Vision-Language-Model/data/captions_b.json
train.pyRun the test.sh in the root directory, and you can configure the relevant parameters for experiments.
sh test.sh
The code will read images from the folder for Q&A.
test.sh Script Analysispython test.py --base_language_model Qwen/Qwen-7B-Chat --base_value_model openai/clip-vit-large-patch14 --model_weights ./weights/train_V1_5/checkpoint-10000/ --image_path ./test_img/1.jpg --prompt "Describe the colors appearing in the image<|extra_0|>"
If you want to test the model directly, the pre-trained weights provided are as follows:
You can directly download the relevant files and test them.
test.pyQwen/Qwen-7B-Chat.openai/clip-vit-large-patch14.checkpoint-10000 saved during training../test_img/1.jpg.Thanks to the great work of the following projects 🙌:
If you have any questions or ideas, feel free to contact me 😊:
I will reply as soon as I see the email!
43 commits
1 commits
Python
78.1%
Cuda
18.6%
C++
3.0%
For the Chinese version of the README, please refer to 中文文档.
dataprocess folder, and dataset-related code is in the dataset folder. Data preprocessing mainly includes path merging, QA data concatenation, feature insertion token processing, etc.qwen folder. By overriding the forward method of QWenModel, multimodal feature injection is achieved.CLIP_VIT and SIGLIP_VIT, with relevant code in the visual folder, which also includes other backbone networks.model.py file under the model folder.We use a multilingual dataset, mainly including the COCO2017 dataset and the AI Challenger image Chinese description dataset:
detail_23k and complex_reasoning_77k, which can effectively enhance the richness of the model's descriptions.In VLM, the visual part uses the CLIP or SIGLIP model, which has already achieved preliminary semantic alignment, and uses a two-layer MLP for feature mapping. By overriding the forward method of QWenModel, the corresponding image tokens are replaced with visual features.
If you wish to replace the model architecture, please modify this part.
| AI Challenger | COCO | complex_reasoning_77k.json | detail_23k.json |
|---|---|---|---|
| AI Challenger | COCO 2017 | complex_reasoning_77k.json | detail_23k.json |
Please store the datasets according to the paths in the configuration file. Of course, the paths can be customized.
Please note that this path needs to be consistent with data/ for the model to read.
After downloading the data, use process_image.py for preprocessing.
Use pip install to install requirements.txt:
pip install -r requirements.txt
Model training adopts the method of freezing the image model, and LLM uses the LoRA method to reduce training pressure. The parameters to be trained include the visual feature mapping layer and the LoRA parameters in the LLM. Since the mapping layer is initialized with untrained parameters, to balance the optimization speed of the model parameters, a larger learning rate is set for the mapping layer than for the LoRA part.
Run the train.sh in the root directory, and you can configure the relevant parameters for experiments.
sh train.sh
Through the above steps, you can start the training process and train the multimodal model.
The model weights will be saved in the --output_dir, and this path can also be customized.
train.sh Script AnalysisCUDA_VISIBLE_DEVICES=0 torchrun --nproc_per_node=1 --master_port=25642 train.py \
--lora_rank 128 \
--lora_dropout 0.10 \
--per_device_train_batch_size 4 \
--gradient_accumulation_steps 1 \
--num_train_epochs 2 \
--save_steps 1000 \
--save_total_limit 5 \
--learning_rate 3e-5 \
--seed 42 \
--ddp_find_unused_parameters False \
--feature_proj_lr 1e-4 \
--remove_unused_columns false \
--logging_steps 100 \
--output_dir ./weights/train_V1_5 \
--target_modules "c_attn|w1|w2" \
--image_map /home/u2023111315/Basic-Vision-Language-Model/data/image_map_b.json \
--captions_file /home/u2023111315/Basic-Vision-Language-Model/data/captions_b.json
train.pyRun the test.sh in the root directory, and you can configure the relevant parameters for experiments.
sh test.sh
The code will read images from the folder for Q&A.
test.sh Script Analysispython test.py --base_language_model Qwen/Qwen-7B-Chat --base_value_model openai/clip-vit-large-patch14 --model_weights ./weights/train_V1_5/checkpoint-10000/ --image_path ./test_img/1.jpg --prompt "Describe the colors appearing in the image<|extra_0|>"
If you want to test the model directly, the pre-trained weights provided are as follows:
You can directly download the relevant files and test them.
test.pyQwen/Qwen-7B-Chat.openai/clip-vit-large-patch14.checkpoint-10000 saved during training../test_img/1.jpg.Thanks to the great work of the following projects 🙌:
If you have any questions or ideas, feel free to contact me 😊:
I will reply as soon as I see the email!
43 commits
1 commits
Python
78.1%
Cuda
18.6%
C++
3.0%