100% accuracy is the goal
Published: 2025-10-18
URL: https://paulabartabajo.substack.com/p/lets-fine-tune-a-small-vision-model
Today is part 3 of the hands-on series on Visual Language Models. If you missed the previous 2 lessons, here they are Part 1 → Evaluation loop and baselining ML Engineer, Tinkerer & Teacher > Father of 1.. sorry 2 kids","photourl":"https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/58f15b6d-7bd7-4ef9-8a43-3603562cd3bd307x307.png","isguest":false,"bestsellertier":null}],"postdate":"2025-09-27T08:01:47.598Z","coverimage":"https://substackcdn.com/image/fetch/$s!FiwK!,fauto,qauto:good,flprogressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82570609-e435-402c-b688-1ba79b356a53960x540.jpeg","coverimagealt":null,"canonicalurl":"https://paulabartabajo.substack.com/p/lets-build-an-image-classification","sectionname":null,"videouploadid":null,"id":174600963,"type":"newsletter","reactioncount":18,"commentcount":0,"publicationid":881497,"publicationname":"Real-World Machine Learning","publicationlogourl":"https://substackcdn.com/image/fetch/$s!WyaS!,fauto,qauto:good,flprogressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c684bb-b9c3-43ec-8c9b-057f9e8dce19500x500.png","belowTheFold":false,"youtubeurl":null,"showlinks":null,"feedurl":null}"> Part 2 → Structured output generation to increase robustness ML Engineer, Tinkerer & Teacher > Father of 1.. sorry 2 kids","photourl":"https://bucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com/public/images/58f15b6d-7bd7-4ef9-8a43-3603562cd3bd307x307.png","isguest":false,"bestsellertier":null}],"postdate":"2025-10-11T08:05:19.118Z","coverimage":"https://substackcdn.com/image/fetch/$s!7bDZ!,fauto,qauto:good,flprogressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3339adf-ad81-4b00-8171-9064ba1df5c4960x540.gif","coverimagealt":null,"canonicalurl":"https://paulabartabajo.substack.com/p/how-to-get-json-out-of-a-visual-language","sectionname":null,"videouploadid":null,"id":175790729,"type":"newsletter","reactioncount":14,"commentcount":0,"publicationid":881497,"publicationname":"Real-World Machine Learning","publicationlogourl":"https://substackcdn.com/image/fetch/$s!WyaS!,fauto,qauto:good,flprogressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa2c684bb-b9c3-43ec-8c9b-057f9e8dce19500x500.png","belowTheFold":false,"youtubeurl":null,"showlinks":null,"feedurl":null}"> Today I will show you how to fine-tune LFM2-VL-450M by Liquid AI to achieve 100% accuracy on the cats vs dogs classification task.
You can find all the source code in this open-source repository. Give it a star on Github ⭐ if you get value from it Go to the code But before we start... What is fine-tuning? Fine-tuning is all about adjusting the model’s parameters to improve its performance on a specific task. Yes, fine tuning is just a fancy name for “training” which is something ML scientists and engineers have done for decades.
What is the problem? 10 years ago there were almost no models with more than 100M parameters. Nowadays, Language Models typically have billions of parameters, so it is not feasible to adjust all the parameter of the model. You either do not have enough memory to load the network parameters (plus gradients you need for your backward pass) or, even if you load it, the forward and backward pass are so slow that you can’t train the model in a reasonable time. To solve this problem, crazy scientists invented a technique called LoRA, which stands for Low-Rank Adaptation.
What is LoRA? Instead of modifying the billions of parameters in your model, LoRA freezes the original weights and adds small “adapter” layers that learn the task-specific adjustments. This trick works because the updates needed during fine-tuning typically have low “intrinsic rank”, meaning they can be represented with far fewer parameters than you’d expect. Ok, enough talking. Let’s move to the implementation. Step 1 → Split the dataset into train and eval We cannot evaluate our fine-tuned model on the same samples we use for fine-tuning, as it would be cheating.
Therefore, I created a script to split the dataset into train and eval sets. We will use the train split to fine-tune the model, and the eval split to evaluate the model. $ make train-test-split \ INPUTDATASETNAME=microsoft/catsvsdogs \ OUTPUTDIR=Paulescu/catsvsdogs \ TRAINSIZE=0.9 \ SEED=42The final dataset is available on Hugging Face Hub as Paulescu/catsvsdogs. Step 2. Create fine-tuning configuration There are lots of hyperparameters you need to play with to get the best fine-tuning results, including model parameters dataset parameters general training hyperparameters LoRA-specific hyperparameters Hence, I recommend you extract them into an external config file (in this case a yaml file), and do not hardcode them in your code.
You can find the exact configuration I am using in configs/finetunecatsvs_dogs.yaml. seed: 23
Model parameters
modelname: LiquidAI/LFM2-VL-450M maxseq_length: 2048
Dataset parameters
datasetname: Paulescu/catsvsdogs datasetsamples: 20000 datasetimagecolumn: image datasetlabelcolum: labels trainsplitratio: 0.9 label_mapping: 0: “cat” 1: “dog”
Prompt parameters
systemprompt: | You are a veterinarian specialized in analyzing pictures of cats and dogs You excel at identifying the type of animal from a picture userprompt: | What animal in the following list is the one you see in the picture?
- cat
- dog
Provide your answer as a single animal from the list without any additional text.
General training hyperparameters
learningrate: 5e-4 numtrainepochs: 1 batchsize: 1 gradientaccumulationsteps: 16 optim: adamw8bit warmupratio: 0.1 weightdecay: 0.01 loggingsteps: 10 eval_steps: 100
LoRA-specific hyperparameters
usepeft: true lorar: 8 loraalpha: 16 loradropout: 0.05 lorabias: none userslora: false loratargetmodules:
- q_proj
- k_proj
- v_proj
- o_proj
- gate_proj
- up_proj
- down_proj
Step 3 → Supervised Fine Tuning with TRL + Modal TRL is a library by Hugging Face that provides a high-level API for supervised fine-tuning. Modal is a serverless compute platform that offers GPU acceleration for your fine-tuning job.
With TRL we get the business logic, and with Modal we get the infrastructur we need to run it. If you want to get into the details, take a look at the finetune.py file. You can run the fine-tune with the following command: $ make fine-tune CONFIGFILE=finetunecatsvs_dogs.yamlI used Weights and Biases to track the fine-tuning process. You can see the results from this run on this dashboard.
The training loss will almost always go down, because neural nets are good at fitting anything. No matter if the data is noisy or not, they will find a way to fit it. Hence, to actually find out if the model is learning more than just noise, you should take a look at the evaluation loss, which in our case runs every 100 steps. Things look good so far.
But that is not enough. We need to re-run the evaluation pipeline we built at the beginning of this tutorial, to see if the fine-tuned model is actually better than the original model. In the configs/catsvsdogs_v4.yaml file I replaced the original LiquidAI/LFM2-VL-450M model with the checkpoint of the fine-tuned model I got after 600 steps.
Here is the complete configuration file: seed: 23
Model parameters
model: /modelcheckpoints/LFM2-VL-450M-Paulescu--catsvsdogs-20251017-233939/checkpoint-600 structuredgeneration: false
Dataset parameters
dataset: “Paulescu/catsvsdogs” nsamples: 100 split: test systemprompt: | You are a veterinarian specialized in analyzing pictures of cats and dogs You excel at identifying the type of animal from a picture user_prompt: | What animal in the following list is the one you see in the picture?
- cat
- dog
Provide your answer as a single animal from the list without any additional text. imagecolumn: “image” labelcolumn: “labels” labelmapping: 0: “cat” 1: “dog”And now is the time to run the evaluation again: $ make evaluate CONFIGFILE=catsvsdogs_v4.yamlThe output should look like this: Accuracy: 1.0 ✅ Evaluation completed successfullyYES, YES, YES!
The fine-tuned model is 100% accurate on the eval set. It took us 3 weeks but we finally made it. Take a moment to celebrate. You deserve it. Next week we will go one step further, and solve a harder problem. Either this car brand and model identification problem, or something else. We will see. You can find all the source code in this open-source repository. Give it a star on Github ⭐ if you get value from it Go to the code Before you leave Next Tuesday I will be hosting a live Ask Me Anything session at the Liquid AI Discord Community.
You can ask anything you want to know about Liquid AI models and tools, and share problems and frustrations you have when building AI systems. I am there to listen and to help you. Join the Liquid AI Discord Community so you can join the event Join TODAY I hope you learned something new today, And I am looking forward to seeing you next Tuesday Enjoy the weekend Pau