Giter Site home page Giter Site logo

mobilevlm's Introduction

MobileVLM: Vision Language Model for Mobile Devices

hf_space Code License githubgithub

  • MobileVLM V2: Faster and Stronger Baseline for Vision Language Model
    arXiv BibTex

    ๐Ÿ“Œ Take a quick look at our MobileVLM V2 architecture
    We introduce MobileVLM V2, a family of significantly improved vision language models upon MobileVLM, which proves that a delicate orchestration of novel architectural design, an improved training scheme tailored for mobile VLMs, and rich high-quality dataset curation can substantially benefit VLMsโ€™ performance. Specifically, MobileVLM V2 1.7B achieves better or on-par performance on standard VLM benchmarks compared with much larger VLMs at the 3B scale. Notably, our 3B model outperforms a large variety of VLMs at the 7B+ scale.

    MobileVLM V2โ€™s architecture. Xv and Xq indicate image and language instruction, respectively, and Ya refers to the text response from the language model MobileLLaMA. The diagram in the lower right corner is a detailed description of LDPv2, i.e., the lightweight downsample projector v2.


  • MobileVLM: A Fast, Strong and Open Vision Language Assistant for Mobile Devices
    arXiv BibTex

    ๐Ÿ“Œ Take a quick look at our MobileVLM architecture
    We present MobileVLM, a competent multimodal vision language model (MMVLM) targeted to run on mobile devices. It is an amalgamation of a myriad of architectural designs and techniques that are mobile-oriented, which comprises a set of language models at the scale of 1.4B and 2.7B parameters, trained from scratch, a multimodal vision model that is pre-trained in the CLIP fashion, cross-modality interaction via an efficient projector. We evaluate MobileVLM on several typical VLM benchmarks. Our models demonstrate on par performance compared with a few much larger models. More importantly, we measure the inference speed on both a Qualcomm Snapdragon 888 CPU and an NVIDIA Jeston Orin GPU, and we obtain state-of-the-art performance of 21.5 tokens and 65.3 tokens per second, respectively.

    The MobileVLM architecture (right) utilizes MobileLLaMA as its language model, intakes Xv and Xq which are image and language instructions as respective inputs and gives Ya as the output language response. LDP refers to a lightweight downsample projector.


๐Ÿ“ธ Release

  • โณ MobileLLaMA Pre-training code.
  • Feb. 26th, 2024: MobileVLM V2 training data and code are available now! Follow the instructions below to train your own mobileVLM V2 model !
  • Feb. 06th, 2024: ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ MobileVLM V2 is out! Paper here! The inference code of MobileVLM V2 is available now! Our MobileVLM V2 weights are publicly available on the HuggingFace website. Enjoy them !
  • Feb. 06th, 2024: The SFT code and dataset of MobileLLaMA are released now! You can train your own chat model.
  • Jan. 23rd, 2024: ๐Ÿš€๐Ÿš€๐Ÿš€ MobileVLM is officially supported by llama.cpp now ! Have a try !
  • Jan. 15th, 2024: Customized llama.cpp for MobileVLM and its deployment instruction on mobile devices.
  • Jan. 11st, 2024: The training and evaluation codes of MobileVLM are available now! Follow these step-by-step instructions below to easily train your own mobileVLM in 5 hours โšก๏ธ !
  • Dec. 31st, 2023: Our MobileVLM weights are uploaded on the HuggingFace website. We also provide inference examples for the MobileLLaMA/MobileVLM model so that anyone can enjoy them early.
  • Dec. 29th, 2023: Our MobileLLaMA weights are uploaded on the HuggingFace website. Enjoy them !
  • Dec. 28th, 2023: ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ We release MobileVLM: A Fast, Strong and Open Vision Language Assistant for Mobile Devices on arxiv. Refer to our paper for more details !

๐Ÿฆ™ Model Zoo

MobileVLM Family

Model LLM GQA SQAI VQAT POPE MMEP MMBdev Avg.
56.1 57.3 41.5 84.5 1196.2 53.2 58.7
MobileVLM V2 1.7B MobileLLaMA 1.4B 59.3 66.7 52.1 84.3 1302.8 57.7 64.2
MobileVLM-3B MobileLLaMA 2.7B 59.0 61.2 47.5 84.9 1288.9 59.6 62.8
MobileVLM V2 3B MobileLLaMA 2.7B 61.1 70.0 57.5 84.7 1440.5 63.2 68.1
MobileVLM V2 7B Vicuna-7B 62.6 74.8 62.3 85.3 1560.7 69.2 72.1

MobileLLaMA Family

๐Ÿ”” Usage and License Notices: This project utilizes certain datasets and checkpoints that are subject to their respective original licenses. Users must comply with all terms and conditions of these original licenses. This project is licensed permissively under the Apache 2.0 license and does not impose any additional constraints. LLaVA

๐Ÿ› ๏ธ Install

  1. Clone this repository and navigate to MobileVLM folder

    git clone https://github.com/Meituan-AutoML/MobileVLM.git
    cd MobileVLM
  2. Install Package

    conda create -n mobilevlm python=3.10 -y
    conda activate mobilevlm
    pip install --upgrade pip
    pip install -r requirements.txt

๐Ÿ—๏ธ Quick Start

Example for MobileLLaMA model inference

import torch
from transformers import LlamaTokenizer, LlamaForCausalLM

model_path = 'mtgv/MobileLLaMA-1.4B-Chat'

tokenizer = LlamaTokenizer.from_pretrained(model_path)
model = LlamaForCausalLM.from_pretrained(
    model_path, torch_dtype=torch.float16, device_map='auto',
)

prompt = 'Q: What is the largest animal?\nA:'
input_ids = tokenizer(prompt, return_tensors="pt").input_ids

generation_output = model.generate(
    input_ids=input_ids, max_new_tokens=32
)
print(tokenizer.decode(generation_output[0]))

Example for MobileVLM/MobileVLM V2 model inference

from scripts.inference import inference_once
# model_path = "mtgv/MobileVLM-1.7B" # MobileVLM
model_path = "mtgv/MobileVLM_V2-1.7B" # MobileVLM V2
image_file = "assets/samples/demo.jpg"
prompt_str = "Who is the author of this book?\nAnswer the question using a single word or phrase."
# (or) What is the title of this book?
# (or) Is this book related to Education & Teaching?

args = type('Args', (), {
    "model_path": model_path,
    "image_file": image_file,
    "prompt": prompt_str,
    "conv_mode": "v1",
    "temperature": 0, 
    "top_p": None,
    "num_beams": 1,
    "max_new_tokens": 512,
    "load_8bit": False,
    "load_4bit": False,
})()

inference_once(args)

๐Ÿชœ Step-by-step Tutorial

MobileVLM

The training process of MobileVLM V2 is divided into two stages:

  • stage I: pre-training
    • โ„๏ธ frozen vision encoder + ๐Ÿ”ฅ learnable LDP V2 projector + ๐Ÿ”ฅ learnable LLM
    • this training process takes around 3~5 hours for MobileVLM V2-1.7B/3B on 8x A100 (80G) with a batch size of 256 and an average of approximately 38G/51G of GPU memory required.
  • stage II: multi-task training
    • โ„๏ธ frozen vision encoder + ๐Ÿ”ฅ learnable LDP V2 projector + ๐Ÿ”ฅ learnable LLM
    • this training process takes around 9~12 hours for MobileVLM V2-1.7B/3B on 8x A100 (80G) with a batch size of 128 and an average of approximately 45G/52G of GPU memory required.
  • note: if you are interest in MobileVLM V1 training recipe, please refer to our previous README.

1๏ธโƒฃ Prepare MobileLLaMA checkpoints

Similar to MobileVLM, please firstly download MobileLLaMA chatbot checkpoints from huggingface website (๐Ÿค— 1.7B, 2.7B). Please note that this is optional (it depends on your working environment), run the training script we provide below and the model will be automatically downloaded by the transformers library.

2๏ธโƒฃ Prepare data

  • For convenience, assume your working directory /path/to/project/mobilevlm as work_dir:

    • cd ${work_dir} && mkdir -p data/pretrain_data data/finetune_data data/benchmark_data
  • prepare pre-training data

    • cd ${work_dir}/data/pretrain_data
    • download the ShareGPT4V-PT from here, which is provided by ShareGPT4V team.
  • prepare multi-task training data

  • prepare benchmark data

    • We evaluate models on a diverse set of 6 benchmarks, i.e. GQA, MMBench, MME, POPE, SQA, TextVQA. We do not evaluate using beam search to make the inference process consistent with the chat demo of real-time outputs. You should follow these instructions to manage the datasets.

    • Data Download Instructions
      • download some useful data/scripts pre-collected by us.
        • unzip benchmark_data.zip && cd benchmark_data
        • bmk_dir=${work_dir}/data/benchmark_data
      • gqa
        • download its image data following the official instructions here
        • cd ${bmk_dir}/gqa && ln -s /path/to/gqa/images images
      • mme
        • download the data following the official instructions here.
        • cd ${bmk_dir}/mme && ln -s /path/to/MME/MME_Benchmark_release_version images
      • pope
        • download coco from POPE following the official instructions here.
        • cd ${bmk_dir}/pope && ln -s /path/to/pope/coco coco && ln -s /path/to/coco/val2014 val2014
      • sqa
        • download images from the data/scienceqa folder of the ScienceQA repo.
        • cd ${bmk_dir}/sqa && ln -s /path/to/sqa/images images
      • textvqa
        • download images following the instructions here.
        • cd ${bmk_dir}/textvqa && ln -s /path/to/textvqa/train_images train_images
      • mmbench
        • no action is needed.
  • organize the data directory as follows after downloading all of them:

    • Data Structure Tree
      .
      โ”œโ”€โ”€ benchmark_data
      โ”‚ย ย  โ”œโ”€โ”€ gqa
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ convert_gqa_for_eval.py
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ eval.py
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ images -> /path/to/your/gqa/images
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ llava_gqa_testdev_balanced.jsonl
      โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ testdev_balanced_questions.json
      โ”‚ย ย  โ”œโ”€โ”€ mmbench
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ convert_mmbench_for_submission.py
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ eval.py
      โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ mmbench_dev_en_20231003.tsv
      โ”‚ย ย  โ”œโ”€โ”€ mme
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ calculation.py
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ convert_answer_to_mme.py
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ images -> /path/to/your/MME/MME_Benchmark_release_version
      โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ llava_mme.jsonl
      โ”‚ย ย  โ”œโ”€โ”€ pope
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ coco -> /path/to/your/pope/coco
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ eval.py
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ llava_pope_test.jsonl
      โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ val2014 -> /path/to/your/coco/val2014
      โ”‚ย ย  โ”œโ”€โ”€ sqa
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ eval.py
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ images -> /path/to/your/scienceqa/images
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ llava_test_CQM-A.json
      โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ pid_splits.json
      โ”‚ย ย  โ”‚ย ย  โ””โ”€โ”€ problems.json
      โ”‚ย ย  โ””โ”€โ”€ textvqa
      โ”‚ย ย      โ”œโ”€โ”€ eval.py
      โ”‚ย ย      โ”œโ”€โ”€ llava_textvqa_val_v051_ocr.jsonl
      โ”‚ย ย      โ”œโ”€โ”€ TextVQA_0.5.1_val.json
      โ”‚ย ย      โ””โ”€โ”€ train_images -> /path/to/your/textvqa/train_images
      โ”œโ”€โ”€ finetune_data
      โ”‚   โ”œโ”€โ”€ llava_v1_5_mix665k.json
      โ”‚   โ”œโ”€โ”€ MobileVLM_V2_FT_Mix2M.json
      โ”‚   โ”œโ”€โ”€ coco
      โ”‚   โ”‚   โ”œโ”€โ”€ train2017
      โ”‚   โ”‚   โ””โ”€โ”€ val2017
      โ”‚   โ”œโ”€โ”€ gqa
      โ”‚   โ”‚   โ””โ”€โ”€ images
      โ”‚   โ”œโ”€โ”€ iconqa_data
      โ”‚   โ”‚   โ””โ”€โ”€ iconqa
      โ”‚   โ”‚    ย ย  โ””โ”€โ”€ train
      โ”‚   โ”‚    ย ย   ย ย  โ”œโ”€โ”€ choose_img
      โ”‚   โ”‚    ย ย   ย ย  โ”œโ”€โ”€ choose_txt
      โ”‚   โ”‚    ย ย   ย ย  โ””โ”€โ”€ fill_in_blank
      โ”‚   โ”œโ”€โ”€ ocr_vqa
      โ”‚   โ”‚   โ””โ”€โ”€ images
      โ”‚   โ”œโ”€โ”€ sam
      โ”‚   โ”‚   โ””โ”€โ”€ images
      โ”‚   โ”œโ”€โ”€ SBU
      โ”‚   โ”‚   โ””โ”€โ”€ images
      โ”‚   โ”œโ”€โ”€ ScienceQA
      โ”‚   โ”‚   โ””โ”€โ”€ train
      โ”‚   โ”œโ”€โ”€ share_textvqa
      โ”‚   โ”‚   โ””โ”€โ”€ images
      โ”‚   โ”œโ”€โ”€ textvqa
      โ”‚   โ”‚   โ””โ”€โ”€ train_images
      โ”‚   โ”œโ”€โ”€ vg
      โ”‚   โ”‚   โ”œโ”€โ”€ VG_100K
      โ”‚   โ”‚   โ””โ”€โ”€ VG_100K_2
      โ”‚   โ”œโ”€โ”€ web-celebrity
      โ”‚   โ”‚   โ””โ”€โ”€ images
      โ”‚   โ”œโ”€โ”€ web-landmark
      โ”‚   โ”‚   โ””โ”€โ”€ images
      โ”‚   โ””โ”€โ”€ wikiart
      โ”‚       โ””โ”€โ”€ images
      โ””โ”€โ”€ pretrain_data
          โ”œโ”€โ”€ share-captioner_coco_lcs_sam_1246k_1107.json
          โ”œโ”€โ”€ blip_laion_cc_sbu_558k.json
          โ”œโ”€โ”€ images
          โ”œโ”€โ”€ coco
          โ”‚ย ย  โ””โ”€โ”€ train2017
          โ”œโ”€โ”€ llava
          โ”‚ย ย  โ””โ”€โ”€ llava_pretrain
          โ””โ”€โ”€ sam
           ย ย  โ””โ”€โ”€ images
      

3๏ธโƒฃ Run everything with one click!

LANGUAGE_MODEL=/path/to/your/MobileLLaMA-1.4B-Chat  # or 2.7B
VISION_MODEL=/path/to/your/clip-vit-large-patch14-336
bash run.sh mobilevlm_v2_1.7b pretrain-finetune-test ${LANGUAGE_MODEL} ${VISION_MODEL}

# (test-only) bash run.sh mobilevlm_v2_1.7b test /path/to/your/own/checkpoint
# (3B) bash run.sh mobilevlm_v2_3b pretrain-finetune-test ${LANGUAGE_MODEL} ${VISION_MODEL}
  • Note ๐Ÿงญ: We place all running commands in run.sh so they can be run with one click for simplification. If you would like to modify some super-parameters to observe their impact, please dive into run.sh to explore.

MobileLLaMA

The SFT(supervised fine-tuning) process of MobileLLaMA:

  • please refer to MobileLLaMA_SFT.md for the env, dataset and training code of our MobileLLaMA SFT.
  • this training process takes around 3~5 hours for MobileLLaMA 1.4B/2.7B on 8x A100 (80G)

Note: You may skip MobileLLaMA training processes and directly start with MobileVLM, leveraging our pre-trained MobileLLaMA model from huggingface website (๐Ÿค— 1.7B, 2.7B). .

๐Ÿ“ฒ Deployment on Mobile Devices

MobileVLM now is officially supported by llama.cpp. We are looking for more cooperation with open-source communities on the deployment of mobile devices.

  • llama.cpp: the repository of official llama.cpp. Step-by-step deployment instructions are provided here.

๐Ÿค Acknowledgments

  • LLaVA: the codebase we built upon. Thanks for their wonderful work! ๐Ÿ‘
  • Vicuna: the amazing open-sourced large language model!
  • llama.cpp: the great open-sourced framework for the inference of LLaMA model in pure C/C++!

โœ๏ธ Reference

If you find MobileVLM or MobileLLaMA useful in your research or applications, please consider giving a star โญ and citing using the following BibTeX:

@article{chu2023mobilevlm,
  title={Mobilevlm: A fast, reproducible and strong vision language assistant for mobile devices},
  author={Chu, Xiangxiang and Qiao, Limeng and Lin, Xinyang and Xu, Shuang and Yang, Yang and Hu, Yiming and Wei, Fei and Zhang, Xinyu and Zhang, Bo and Wei, Xiaolin and others},
  journal={arXiv preprint arXiv:2312.16886},
  year={2023}
}

@article{chu2024mobilevlm,
  title={MobileVLM V2: Faster and Stronger Baseline for Vision Language Model},
  author={Chu, Xiangxiang and Qiao, Limeng and Zhang, Xinyu and Xu, Shuang and Wei, Fei and Yang, Yang and Sun, Xiaofei and Hu, Yiming and Lin, Xinyang and Zhang, Bo and others},
  journal={arXiv preprint arXiv:2402.03766},
  year={2024}
}

๐ŸŒŸ Star History

Star History Chart

mobilevlm's People

Contributors

er-muyue avatar littlesunlxy avatar linxinyang avatar yangyang-dlut avatar cxxgtxy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.