🚀 「大模型」2小时从0训练65M参数的视觉多模态VLM!🌏 Train a 65M-parameter VLM from scratch in just 2h!
Go to file
jingyaogong 33db76fe10 [update] dataset name 2026-02-05 00:22:58 +08:00
dataset [update] dataset name 2026-02-05 00:22:58 +08:00
images update structure image 2025-04-01 16:31:54 +08:00
model [update] vlm params 2026-02-04 16:41:17 +08:00
scripts [feat] update args 2025-10-30 10:50:54 +08:00
trainer [update] dataset name 2026-02-05 00:22:58 +08:00
.gitignore [feat] update readme 2025-10-22 10:58:52 +08:00
CODE_OF_CONDUCT.md update readme 2024-10-05 00:52:29 +08:00
LICENSE update readme 2024-10-05 00:52:29 +08:00
README.md [feat] update readme 2025-12-27 10:48:54 +08:00
README_en.md [feat] update readme 2025-12-27 10:48:54 +08:00
eval_vlm.py [update] sync minimind 2026-01-15 21:51:58 +08:00
requirements.txt [feat] update datasets 2025-10-30 11:08:30 +08:00

README_en.md

logo

GitHub Repo stars GitHub Code License GitHub last commit GitHub pull request Collection

GitHub Trend

"The Greatest Path is the Simplest"

中文 | English

  • This project aims to train a super-small multimodal vision-language model, MiniMind-V, with just a cost of 1.3 RMB and 1 hours of work, starting from scratch!
  • The smallest version of MiniMind-V is only about \frac{1}{7000} the size of GPT-3, designed to enable fast inference and even training on personal GPUs.
  • MiniMind-V is an extension of the visual capabilities of the MiniMind pure language model.
  • The project includes full code for the minimalist structure of large VLM models, dataset cleaning, pretraining, and supervised fine-tuning (SFT).
  • This is not only the smallest implementation of an open-source VLM model but also a concise tutorial for beginners in vision-language models.
  • The hope is that this project can provide a useful example to inspire others and share the joy of creation, helping to drive progress in the wider AI community!

To avoid misunderstandings, the "1 hours" is based on testing (1 epoch) with an NVIDIA 3090 hardware device (single GPU), and the "1.3 RMB" refers to GPU server rental costs.

📌 Introduction

“Building a plane with Legos is much more exciting than flying in first class!” Is it really as complex as imagined to build a VLM-based multimodal large model? How is the code implementation done? Is the training process difficult? Now, let's explore the answers and feel the joy of creation together!

[!TIP] (As of 2025-02-20) The MiniMind-V series has completed the training of the following model versions, with the smallest requiring only 26M (0.026B) parameters, capable of both image recognition and conversation!

Model (Size) Inference Memory Release
MiniMind2-V (104M) 0.6 GB 2025.02.20
MiniMind2-Small-V (26M) 1.1 GB 2025.02.20
minimind-v-v1-small (27M) 0.6 GB 2024.10.04
minimind-v-v1 (109M) 1.1 GB 2024.10.04

👉Recent Updates

2025-10-24
  • Bug fix: model weights mismatch
  • Adapted to "minimind-1024 update"
  • Code refactoring: training and evaluation scripts standardized
  • Added complete checkpoint resumption support
2025-04-27
  • Compatibility updates
  • Adapted to the new feature in the "minimind" repository
  • Standardized parts of the code
2025-02-20
  • MiniMind2-V updated alongside MiniMind2
  • Significant reduction of all redundant code, standardized code format
  • Major simplification of the model's redundant structure
  • Updated dataset format, expanded with new SFT datasets
  • Better performance than the previous VLM version!
More...

2024-10-05

  • MiniMind-V released on schedule, first open-source release

📌 Quick Start

Sharing my hardware and software configuration (for reference only)
  • CPU: Intel(R) Core(TM) i9-10980XE CPU @ 3.00GHz
  • RAM: 128 GB
  • GPU: NVIDIA GeForce RTX 3090(24GB) * 8
  • Ubuntu==20.04
  • CUDA==12.2
  • Python==3.10.16
  • requirements.txt

Step 0

# Clone the code repository
git clone https://github.com/jingyaogong/minimind-v
# Download the clip model to the ./model/vision_model directory
git clone https://huggingface.co/openai/clip-vit-base-patch16
# or
git clone https://www.modelscope.cn/models/openai-mirror/clip-vit-base-patch16
# Download the minimind language model to the ./out directory (as the base language model for training VLM):
# HuggingFace
https://huggingface.co/jingyaogong/MiniMind2-V-PyTorch/blob/main/llm_512.pth # or llm_768.pth
# Domestic source
https://modelscope.cn/models/gongjy/MiniMind2-V-PyTorch/resolve/master/llm_512.pth # or llm_768.pth

Ⅰ Test an existing model's performance

1. Environment Preparation

pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple

2. Download the model

git clone https://huggingface.co/jingyaogong/MiniMind2-V

3. Command-line Q&A

# load_from='model': load native PyTorch weights, load_from='other path': load transformers format
python eval_vlm.py --load_from model --weight sft_vlm

# Or use transformers format model
python eval_vlm.py --load_from MiniMind2-V

4. Or start the WebUI

python web_demo_vlm.py

Ⅱ Train from scratch

1. Environment Preparation

pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple
Note: Test if Torch can use CUDA
import torch
print(torch.cuda.is_available())

If unavailable, download the whl file from torch_stable for installation. Refer to this link for help.

2. Download Data

Download the required content from the dataset link and place it under ./dataset.

Note: Dataset Details

[Note 1] Previously, extracting 500k fragmented image files could be very slow. From 2025-12-27, dataset format is unified to Parquet with image-text integrated storage, smaller size, no decompression needed, faster loading.

[Note 2] Parquet is a columnar storage format supporting efficient compression and fast reading. To preview data content, run python lm_dataset.py in the dataset/ directory to visualize the first 5 image-text pairs.

Pretrain data:

wget https://hf-mirror.com/datasets/jingyaogong/minimind-v_dataset/resolve/main/pretrain_data.parquet

SFT data:

wget https://hf-mirror.com/datasets/jingyaogong/minimind-v_dataset/resolve/main/sft_data.parquet

Please reserve about ~2GB of space for the dataset. If there is insufficient space for pretrain data, you can try skipping the pretrain training step and proceed directly to SFT training.

3. Start Training

3.1 Pretraining (Learning image description)

# Basic training command (start from LLM weights, train vision_proj only)
python train_pretrain_vlm.py --epochs 4 --from_weight llm

Run pretraining to get pretrain_vlm_*.pth as the pretrained model's output weights (* represents the model dimension, default is 512).

3.2 Supervised Fine-Tuning (Learning image-caption dialogue style)

# Basic training command (start from pretrain weights, full parameter fine-tuning)
python train_sft_vlm.py --epochs 2 --from_weight pretrain_vlm

Perform supervised fine-tuning to get sft_vlm_*.pth as the output weights for the fine-tuned model.

Note: Training Details

Training Features:

  • Support checkpoint resumption: add --from_resume 1 parameter to continue from last interruption
  • Support GPU count changes: automatically convert steps when GPU count changes during resumption
  • Atomic saving: use temporary file + replacement mechanism to prevent weight corruption from interruption
  • Each save generates out/**.pth (model weights) and checkpoints/**_resume.pth (training state) files
# To resume training after interruption, use the same command and add --from_resume 1
python train_sft_vlm.py --epochs 4 --from_resume 1

Parameter Description:

  • --from_weight: base weight name (llm, pretrain_vlm, none, etc.)
  • --save_weight: save weight prefix name
  • --from_resume: whether to resume training (0=start from scratch, 1=continue from checkpoint)
  • --freeze_llm: whether to freeze LLM parameters (pretrain use only)
  • More details can be found in the code

4. Test the Model's Performance

Ensure that the model *.pth file you want to test is located in the ./out/ directory. You can also directly download the pre-trained *.pth file from here.

# Test SFT model (default)
python eval_vlm.py --weight sft_vlm

# Test Pretrain model
python eval_vlm.py --weight pretrain_vlm

[!TIP] The training scripts are based on PyTorch's native framework and support multi-card acceleration. If your device has N (N>1) GPUs:

Single-machine N-card training method (DDP, supports multi-machine multi-card cluster)

torchrun --nproc_per_node N train_xxx.py
Note: Other Details

Single-machine N-card training (DeepSpeed)

deepspeed --master_port 29500 --num_gpus=N train_xxx.py

You can enable wandb logging during training:

# You need to log in: wandb login
torchrun --nproc_per_node N train_xxx.py --use_wandb
# and
python train_xxx.py --use_wandb

By adding the --use_wandb parameter, you can log the training process, and after training is complete, you can view the process on the wandb website. You can specify the project name and run name by modifying the wandb_project and wandb_run_name parameters.

[Note]: After June 2025, the domestic network environment cannot directly connect to WandB. The MiniMind project by default switches to using SwanLab as the training visualization tool (fully compatible with WandB API), that is, just change import wandb to import swanlab as wandb, no other changes are needed.

📌 VLM Detail

The base language model of MiniMind-V (VLM), MiniMind (LLM), comes from the twin project minimind. For detailed information on the model structure, training specifics, principles, and testing results, please refer to the minimind project. To reduce redundancy, the discussion on LLM-related topics is omitted here, assuming you have a basic understanding of MiniMind (LLM).

Even if you are not very familiar with the details of LLMs, you can still follow the "Quick Start" guide to train a MiniMind-V, as it remains unaffected and the repository focuses on the lowest cost for out-of-the-box use!

MiniMind-V's structure adds two submodules, a Visual Encoder and a feature projection, with a modality-mixing branch to support inputs from multiple modalities: LLM-structure LLM-structure

[Important] Some Interesting Thoughts

Let's take a moment to think about two questions:

  • What is a Large Language Model (LLM)?
  • What is a multimodal model?

This article perfectly aligns with my thoughts:
Although the name "large language model" (LLM) contains the word "language," they are actually not closely related to language; this is just a historical issue. A more accurate name would be self-regressive Transformer or something else. LLMs are more of a general statistical modeling technology, mainly using a self-regressive Transformer to simulate token flows. These tokens can represent text, images, audio, action choices, and even molecules—anything, really.
Therefore, as long as the problem can be converted into a process of simulating a series of discrete tokens, LLM can theoretically solve it. In fact, with the increasing maturity of large language model technologies, we may see more and more problems falling under this modeling paradigm. In other words, the problem is fixed in using LLM to "predict the next token," but the role and meaning of the tokens differ in each domain.

ZJU-LiXi has also mentioned a similar viewpoint (roughly stated below):
Text, video, audio, actions, etc., are considered "multimodal" signals in human perception, but the term "modality" is essentially just a classification concept based on how humans store information. Just like .txt and .png files, though they differ in visual presentation and higher-level forms, they are fundamentally the same. The concept of " multimodal" arose simply because humans need to categorize these signals based on different sensory dimensions.
However, for machines, regardless of the signal's "modality," they are ultimately presented as a sequence of binary " monomodal" numbers. Machines do not differentiate the origin of these signals; they just process and analyze the information contained within these sequences.

Personally, I think Generative Pretrained Transformer (GPT) is a more fitting term than **Large Language Model (LLM) **, and I prefer to use "GPT" to represent models in the LLM/VLM/GPT-like architecture series rather than to ride on OpenAI's coattails.

To summarize what GPTs do in one sentence:

A GPT model predicts the next, next-next, next-next-next token, etc., based on the current token... until the model outputs the end token; here, the "token" doesn’t necessarily have to be text!

> For an LLM model, if we need to understand an "image," we just treat the "image" as a special "foreign language" that has never been encountered before, and translate it into the "LLM language" via a "foreign language dictionary."
> For an LLM model, if we need to understand "audio," we just treat "audio" as a special "foreign language" that has never been encountered before, and translate it into the "LLM language" via a "foreign language dictionary."
> ...

To obtain MiniMind-V, we only need to do these 2 things:

  1. Use the "foreign language dictionary" that is good at translating images, to translate the image from the " foreign language" into a model-understandable "LLM language."
  2. Fine-tune the LLM so that it and the "foreign language dictionary" go through a period of adaptation, thereby better understanding images.

The "foreign language dictionary" is referred to as the Visual Encoder model.
Like LlaVA, Qwen-VL, and other visual language models, MiniMind-V also uses the open-source Clip series models as the Visual Encoder.
Specifically, we use clip-vit-base-patch16, a classic Visual Encoder based on the ViT-B/16 architecture for describing image-text information.
The input image size is 224x224, and because the Patch size is 16×16, it generates 14*14=196 tokens as the input to the encoder layer, which produces a 1×768 dimensional embedding vector for calculating error with the text.
We don’t need the final embedding representation, so we only take the output from the encoder layer, which is the output feature from the core ViT backbone.
It receives the feature of size 196×768 from the previous layer, which we use as 196 visual tokens to input into MiniMind-V.
After obtaining the image encoder features, the integration with the LLM requires aligning the 768-dimensional visual tokens with the LLM's text tokens, and mapping the image features into the same space as text embeddings. In other words, the image features and native visual tokens cannot be directly treated the same; they require cross-modal feature alignment.
LlaVA-1 uses a simple unbiased linear transformation to achieve this, with great success, and MiniMind-V does the same.

llava-structure

With that, the internal structural changes of MiniMind-V are now fully presented.


Next, let's briefly discuss the changes in the external input and output of MiniMind-V.

The input to the VLM is still a segment of text containing special <image> placeholders.
After computing the text embedding, the vector generated by the image encoder can be projected onto the corresponding embedding part of the placeholder, replacing the original placeholder embedding.
For example:

<image>\nWhat is in this image?

In minimind-v, the image is replaced by a 196-character @@@...@@@ placeholder. The reason for using 196 characters is explained earlier:
Any image is encoded by the Clip model as 196×768-dimensional tokens,
thus the minimind-v prompt becomes:

@@@......@@@\nWhat is this image describing?

After calculating the embedding and projection, and replacing the image token part, the entire calculation process to output is no different from that of the LLM part.

input

At this point, all the details of MiniMind-V have been presented. The MiniMind-V model subclass completely inherits from MiniMind, and is generated with minimal changes based on the latter, with core algorithm modifications < 50 lines, making the migration difficulty very low. Therefore, there may be differences with models like LlAVA, but the overall idea remains consistent.

📌 Experiment

Ⅰ Dataset

Source: Chinese-LLaVA-Vision
Contains approximately 570,000 pre-trained images from CC-3M and COCO 2014;
llava-en-zh-300k
Contains 300k instruction fine-tuning data and 150k images.
The Q&A content has been translated, with better support for Chinese, further organized and resized.

(pretrain_vlm_data.jsonl) Pre-training dataset format:

{
  "conversations": [
    {
      "role": "user",
      "content": "Provide a brief description of the given image.\n<image>"
    },
    {
      "role": "assistant",
      "content": "Olive oil is a healthy ingredient for free use."
    }
  ],
  "image": "GCC_train_002582585.jpg"
}

(sft_vlm_data.jsonl) Single image instruction fine-tuning dataset format:

{
  "conversations": [
    {
      "role": "user",
      "content": "What impact does the location of the alarm clock have on sleep quality?<image>"
    },
    {
      "role": "assistant",
      "content": "Place the digital alarm clock on the nightstand..."
    }
  ],
  "image": "train-00000-of-00001_image_0_0.jpg"
}

Dataset download link: (ModelScope | HuggingFace)

Ⅱ Training

train_pretrain_vlm

Pre-training learns general image knowledge from a dataset of 595K samples, such as a deer is a deer, a dog is a dog.

train_sft_vlm

Instruction fine-tuning learns the real Q&A format for image-related questions from a dataset of 300K real conversations, which better aligns with human communication habits.

During training, the visual encoder, i.e., the CLIP model's gradients, are frozen, and only the Projection and LLM parts are trained.
In pre-training, only the last layer parameters of Projection and LLM are learnable.
In instruction fine-tuning, all parameters of Projection and LLM are learnable.

Training Time and Loss Trend (for reference only)

Pretrain [512+8] & [768+16]
input

SFT [512+8] & [768+16]
input

Ⅲ Model Weights

(Native PyTorch *.pth weight files) Download link:
(ModelScope | HuggingFace)

(Transformers format models)
Download link:
(ModelScope | HuggingFace)

Note: The Transformers version is the MiniMind-V model after single-image instruction fine-tuning

📌 Test

Effect Test

Single Image Dialogue

Image MiniMind2-V MiniMind2-V-Small
city-traffic                              The image shows a busy city street with tall buildings on both sides of a long road. The street is packed with cars, trucks, and buses, along with many other vehicles driving on the road. There are many cars visible on the street, some driving at high speed, while others are parked at the side. Additionally, there is a bus parked on the right side of the street. Traffic lights are visible on the street, indicating a busy city environment. The image depicts a bustling urban scene with several cars and a truck driving down the city street. There are many traffic lights visible, some on the left side of the street, and others on the right. Several pedestrians can be seen on the street, some standing closer to the road while others are farther away. A parking sign is located on the left side of the image, hinting at the city environment. Two cars are visible on the street, one on the right and the other on the left, with another one also on the left side. This image captures a typical day in a city environment.
astronaut                              The image shows an astronaut in a spacesuit sitting in a large spacecraft. They appear to be embarking on or disembarking from a space mission. Behind the astronaut, there is a rocket launch platform, likely to support the astronaut's mission during the journey. Additionally, an aircraft is parked near a hangar, further suggesting this is an aviation event. There are a few people around the aircraft, some standing close, possibly observing or waiting for the spacecraft to prepare for takeoff. The scene shows a soldier wearing a helmet standing on a large airplane. This aircraft appears to be a military one, likely preparing to board another plane. Another person stands in front, possibly observing the flight process. There are several people around the airplane, some standing on the left side, others on the right. They seem to be watching the pilot's performance. Additionally, a truck is parked near the left side, likely to observe the flight process more closely.
dog-woman-sea                              The image shows a woman sitting on the beach, holding a white dog in her hands. She appears to be a woman sitting on the sand, looking at her. A dog is also sitting beside her, looking relaxed and comfortable. Other beachgoers are scattered across the beach, some sitting, while others are farther away. A boat can be seen in the background, indicating that this is a popular beach destination for tourists. Two people are sitting on the beach, one lounging lazily on the sand, while the other is sitting. They seem to be enjoying their time by the sea. There are a few beach chairs, one close to the left side of the sand, another in the middle. Additionally, a dog is lying on the sand, adding to the relaxed atmosphere of the scene.
rainbow-falls                              The photo captures a beautiful natural scene with high mountains in the background. By the water, a large fountain spans across the surface, attracting many visitors. There are several people on the water's surface, some standing or sitting around the fountain, while others walk in the water. Overall, this image portrays a beautiful and peaceful environment where people can enjoy scenic views of nature. Under a beautiful blue sky, a massive white waterfall hangs above with a huge stream of wet water. This waterfall is located on a mountain, adding a charming and serene atmosphere to the whole scene. In the background of this image, several boats can be seen, some near the water's edge, others farther away. These boats seem to be preparing for scenic or outdoor activities.
elderly-reading                              The image shows a man sitting on a park bench next to a green chair. There is an open book beside him with the words "reading" written on it, suggesting he may be reading. The park has a bench and a park seat, adding life to the surrounding environment. There are several cars and a truck nearby, indicating this is a public space. Additionally, a person can be seen standing at different locations in the park, possibly waiting to cross the road or walk further. An elderly person wearing shorts sits on a park bench surrounded by trees. He seems to be reading a book, possibly engaged in reading. In the background, there is another bench providing ample seating for the scene. You can also see a chair and a table in the background, suggesting this may be an outdoor seating area where people can relax.
panda-grassland                              The image shows a white brown bear sitting on the grass, next to a large bear with brown spots. This bear seems shy or playful as it lies on the grass, resting and looking relaxed. In this image, a brown bear is strolling on the grass. The bear occupies much of the frame, seemingly walking in its natural environment on the grass. In the background, there are several trees, adding natural elements to the scene. A bird is flying near the middle of the scene, bringing a lively atmosphere to the image.
bicycle-flowers                              The image shows a beautiful vase filled with colorful flowers and bouquets. The bouquets are scattered throughout the vase, creating a visually pleasant scene. The colorful flowers in the vase create a delightful visual. These flowers are placed on a table, likely displayed to showcase their beauty. The scene shows a green and purple bicycle parked next to a building, placed near a large tree. This bicycle is situated nearby, adding some color to the scene. In addition to the bicycle, there are other bicycles, including one in the foreground and another near the center of the background. The presence of the bicycles suggests they may be parked there.
dance                              The woman in the image is wearing a white dress with a black tennis skirt. She is performing a tennis match, likely part of the competition. Several chairs are visible in the background, possibly set up for the audience or other spectators. Additionally, a bench is placed on the left side of the scene, providing a place for people to rest. A woman in white clothing stands on a stage holding a white frisbee. She seems to be participating in a stage dance or competition. Several other people are present in the scene, one standing on the left side of the stage, another on the right, and a third person standing near the right of the venue. The stage has several spectators, some standing, others sitting, with some remaining standing. This appears to be a joyful festival or event.

Effect Summary:

Visual signals are treated as a special foreign language by LLMs, so the "language learning" ability highly depends on the LLM's capacity. The stronger the LLM, the more powerful the corresponding VLM, and the performance boost becomes significant.

Future Areas for Improvement:

> Simpler projection-based cross-modal feature alignment, which may be inferior compared to Cross-Attention.
> The Clip model could try larger, more powerful large series for finer-grained token representations of image features, as they are still coarse.
> The resolution is not high, theoretically only 224×224 (the minimind-v dataset is set to 128×128 for space saving).
> ...

📌 Acknowledge

[!TIP] If you find MiniMind-V helpful, please consider giving it a ⭐ on GitHub.
Given the limited expertise, there may be unknown issues, and we welcome everyone to discuss, correct, or submit PRs to improve the project in Issues.
Your support is the driving force behind continuous improvements to the project. Thank you!

🤝 Contributors

😊 Acknowledgments

@xinyanghuang7: Multi-image VLM branch | Repository provided up to this version

Reference Links & Thanks to the following excellent papers or projects

🫶Supporter

github contribution grid snake animation github contribution grid snake animation Star History Chart

🎓 Citation

If you find MiniMind-V helpful in your research or work, please cite:

@misc{minimind,
  title={MiniMind-V: Train a Tiny VLM from scratch},
  author={Jingyao Gong},
  year={2024},
  howpublished={https://github.com/jingyaogong/minimind-v}
}

License

This repository is licensed under the Apache-2.0 License.