Vision LLMs with LoRA fine-tuning.
Project description
plimai: Vision LLMs (Large Language Models for Vision) with Efficient LoRA Fine-Tuning
Modular Vision LLMs (Large Language Models for Vision) with Efficient LoRA Fine-Tuning
Build, adapt, and fine-tune vision models with ease and efficiency.
๐ Quick Links
๐ Table of Contents
- Features
- Showcase
- Getting Started
- Supported Python Versions
- Why Plimai?
- Architecture Overview
- Core Modules
- Performance & Efficiency
- Advanced Configuration
- Documentation & Resources
- Testing & Quality
- Examples & Use Cases
- Extending the Framework
- Contributing
- FAQ
- Citation
- Acknowledgements
- License
โจ Features
- ๐ง Plug-and-play LoRA adapters for parameter-efficient fine-tuning
- ๐๏ธ Modular Vision Transformer (ViT) backbone with customizable components
- ๐ฏ Unified model zoo for open-source visual models
- โ๏ธ Easy configuration and extensible codebase
- ๐ Production ready with comprehensive testing and documentation
- ๐พ Memory efficient training with gradient checkpointing support
- ๐ Built-in metrics and visualization tools
- ๐งฉ Modular training loop with LoRA support
- ๐ฏ Unified CLI for fine-tuning and evaluation
- ๐ Extensible callbacks (early stopping, logging, etc.)
- ๐ฆ Checkpointing and resume
- ๐ Mixed precision training
- ๐ง Easy dataset and model extension
- โก Ready for distributed/multi-GPU training
๐ Showcase
plimai is a modular, research-friendly framework for building and fine-tuning Vision Large Language Models (LLMs) with efficient Low-Rank Adaptation (LoRA) support. Whether you're working on image classification, visual question answering, or custom vision tasks, plimai provides the tools you need for parameter-efficient model adaptation.
๐ Getting Started
Here's a minimal example to get you up and running:
pip install plimai
import torch
from plimai.models.vision_transformer import VisionTransformer
from plimai.utils.config import default_config
# Create model
x = torch.randn(2, 3, 224, 224)
model = VisionTransformer(
img_size=default_config['img_size'],
patch_size=default_config['patch_size'],
in_chans=default_config['in_chans'],
num_classes=default_config['num_classes'],
embed_dim=default_config['embed_dim'],
depth=default_config['depth'],
num_heads=default_config['num_heads'],
mlp_ratio=default_config['mlp_ratio'],
lora_config=default_config['lora'],
)
# Forward pass
with torch.no_grad():
out = model(x)
print('Output shape:', out.shape)
For advanced usage, CLI details, and more, see the Documentation and src/plimai/cli/finetune.py.
๐ Supported Python Versions
- Python 3.8+
๐งฉ Why Plimai?
- Parameter-efficient fine-tuning: Plug-and-play LoRA adapters for fast, memory-efficient adaptation with minimal computational overhead
- Modular ViT backbone: Swap or extend components like patch embedding, attention, or MLP heads with ease
- Unified model zoo: Access and experiment with open-source visual models through a consistent interface
- Research & production ready: Clean, extensible codebase with comprehensive configuration options and robust utilities
- Memory efficient: Fine-tune large models on consumer hardware by updating only a small fraction of parameters
๐๏ธ Architecture Overview
plimai is built around a modular Vision Transformer (ViT) backbone, with LoRA adapters strategically injected into attention and MLP layers for efficient fine-tuning. This approach allows you to adapt large pre-trained models using only a fraction of the original parameters.
Model Data Flow
---
config:
layout: dagre
---
flowchart TD
subgraph LoRA_Adapters["LoRA Adapters in Attention and MLP"]
LA1(["LoRA Adapter 1"])
LA2(["LoRA Adapter 2"])
LA3(["LoRA Adapter N"])
end
A(["Input Image"]) --> B(["Patch Embedding"])
B --> C(["CLS Token & Positional Encoding"])
C --> D1(["Encoder Layer 1"])
D1 --> D2(["Encoder Layer 2"])
D2 --> D3(["Encoder Layer N"])
D3 --> E(["LayerNorm"])
E --> F(["MLP Head"])
F --> G(["Output Class Logits"])
LA1 -.-> D1
LA2 -.-> D2
LA3 -.-> D3
LA1:::loraStyle
LA2:::loraStyle
LA3:::loraStyle
classDef loraStyle fill:#e1f5fe,stroke:#0277bd,stroke-width:2px
Architecture Components
Legend:
- Solid arrows: Main data flow through the Vision Transformer
- Dashed arrows: LoRA adapter injection points in encoder layers
- Blue boxes: LoRA adapters for parameter-efficient fine-tuning
Data Flow Steps:
- Input Image (224ร224ร3): Raw image data ready for processing
- Patch Embedding: Image split into 16ร16 patches and projected to embedding dimension
- CLS Token & Positional Encoding: Classification token prepended with learnable position embeddings
- Transformer Encoder Stack: Multi-layer transformer with self-attention and MLP blocks
- LoRA Integration: Low-rank adapters injected into attention and MLP layers
- Efficient Updates: Only LoRA parameters updated during fine-tuning
- LayerNorm: Final normalization of encoder outputs
- MLP Head: Task-specific classification or regression head
- Output: Final predictions (class probabilities, regression values, etc.)
๐งฉ Core Modules
| Module | Description | Key Features |
|---|---|---|
| PatchEmbedding | Image-to-patch conversion and embedding | โข Configurable patch sizes โข Learnable position embeddings โข Support for different input resolutions |
| TransformerEncoder | Multi-layer transformer backbone | โข Self-attention mechanisms โข LoRA adapter integration โข Gradient checkpointing support |
| LoRALinear | Low-rank adaptation layers | โข Configurable rank and scaling โข Memory-efficient updates โข Easy enable/disable functionality |
| MLPHead | Output projection layer | โข Multi-class classification โข Regression support โข Dropout regularization |
| Config System | Centralized configuration management | โข YAML/JSON config files โข Command-line overrides โข Validation and defaults |
| Data Utils | Preprocessing and augmentation | โข Built-in transforms โข Custom dataset loaders โข Efficient data pipelines |
๐ Performance & Efficiency
LoRA Benefits
| Metric | Full Fine-tuning | LoRA Fine-tuning | Improvement |
|---|---|---|---|
| Trainable Parameters | 86M | 2.4M | 97% reduction |
| Memory Usage | 12GB | 4GB | 67% reduction |
| Training Time | 4 hours | 1.5 hours | 62% faster |
| Storage per Task | 344MB | 9.6MB | 97% smaller |
Benchmarks on ViT-Base with CIFAR-100, RTX 3090
Supported Model Sizes
- ViT-Tiny: 5.7M parameters, perfect for experimentation
- ViT-Small: 22M parameters, good balance of performance and efficiency
- ViT-Base: 86M parameters, strong performance across tasks
- ViT-Large: 307M parameters, state-of-the-art results
๐ง Advanced Configuration
LoRA Configuration
lora_config = {
"rank": 16, # Low-rank dimension
"alpha": 32, # Scaling factor
"dropout": 0.1, # Dropout rate
"target_modules": [ # Modules to adapt
"attention.qkv",
"attention.proj",
"mlp.fc1",
"mlp.fc2"
],
"merge_weights": False # Whether to merge during inference
}
Training Configuration
# config.yaml
model:
name: "vit_base"
img_size: 224
patch_size: 16
num_classes: 1000
training:
epochs: 10
batch_size: 32
learning_rate: 1e-4
weight_decay: 0.01
warmup_steps: 1000
lora:
rank: 16
alpha: 32
dropout: 0.1
๐ Documentation & Resources
- ๐ Complete API Reference
- ๐ Tutorials and Examples
- ๐ฌ Research Papers
- ๐ก Best Practices Guide
- ๐ Troubleshooting
Research Papers
- LoRA: Low-Rank Adaptation of Large Language Models
- An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale
- Vision Transformer for Fine-Grained Image Classification
๐งช Testing & Quality
Run the comprehensive test suite:
# Unit tests
pytest tests/unit/
# Integration tests
pytest tests/integration/
# Performance benchmarks
pytest tests/benchmarks/
# All tests with coverage
pytest tests/ --cov=plimai --cov-report=html
Code Quality Tools
# Linting
flake8 src/
black src/ --check
# Type checking
mypy src/
# Security scanning
bandit -r src/
๐ Examples & Use Cases
Image Classification
from plimai import VisionTransformer
from plimai.datasets import CIFAR10Dataset
# Load pre-trained model
model = VisionTransformer.from_pretrained("vit_base_patch16_224")
# Fine-tune on CIFAR-10
dataset = CIFAR10Dataset(train=True, transform=model.default_transform)
model.finetune(dataset, epochs=10, lora_rank=16)
Custom Dataset
from plimai.datasets import ImageFolderDataset
# Your custom dataset
dataset = ImageFolderDataset(
root="/path/to/dataset",
split="train",
transform=model.default_transform
)
# Fine-tune with custom configuration
model.finetune(
dataset,
config_path="configs/custom_config.yaml"
)
๐งฉ Extending the Framework
- Add new datasets in
src/plimai/data/datasets.py - Add new callbacks in
src/plimai/callbacks/ - Add new models in
src/plimai/models/ - Add new CLI tools in
src/plimai/cli/
๐ Documentation
- See code comments and docstrings for details on each module.
- For advanced usage, see the
src/plimai/cli/finetune.pyscript.
๐ค Contributing
We welcome contributions from the community! Here's how you can get involved:
Ways to Contribute
- ๐ Report bugs by opening issues with detailed reproduction steps
- ๐ก Suggest features through feature requests and discussions
- ๐ Improve documentation with examples, tutorials, and API docs
- ๐ง Submit pull requests for bug fixes and new features
- ๐งช Add tests to improve code coverage and reliability
Development Setup
# Clone and setup development environment
git clone https://github.com/plim-ai/plim.git
cd plim
pip install -e ".[dev]"
# Install pre-commit hooks
pre-commit install
# Run tests
pytest tests/
Community Resources
- ๐ฌ GitHub Discussions - Ask questions and share ideas
- ๐ Issue Tracker - Report bugs and request features
- ๐ Contributing Guide - Detailed contribution guidelines
- ๐ฏ Roadmap - See what's planned for future releases
๐ License & Citation
This project is licensed under the MIT License - see the LICENSE file for details.
Citation
If you use plimai in your research, please cite:
@software{plimai2025,
author = {Pritesh Raj},
title = {plimai: Vision LLMs with Efficient LoRA Fine-Tuning},
url = {https://github.com/plim-ai/plim},
year = {2025},
version = {1.0.0}
}
๐ Acknowledgements
We thank the following projects and communities:
- PyTorch - Deep learning framework
- HuggingFace - Transformers and model hub
- timm - Vision model implementations
- PEFT - Parameter-efficient fine-tuning methods
โ FAQ
Q: I get a CUDA out of memory error during training!
A: Try these solutions in order:
- Reduce batch size:
--batch_size 16or--batch_size 8 - Enable gradient checkpointing:
--gradient_checkpointing - Use a smaller model:
--model vit_smallinstead ofvit_base - Reduce LoRA rank:
--lora_rank 8instead of16
Q: How do I add my own dataset?
A: Create a custom dataset class:
from plimai.datasets import BaseDataset
class MyDataset(BaseDataset):
def __init__(self, root, transform=None):
super().__init__(root, transform)
# Your dataset initialization
def __getitem__(self, idx):
# Return (image, label) tuple
pass
Q: Can I use plimai with other vision architectures?
A: Currently plimai focuses on Vision Transformers, but we're working on support for:
- ConvNeXT
- Swin Transformer
- EfficientNet
- ResNet with LoRA
Check our roadmap for updates!
Q: How do I merge LoRA weights for inference?
A: Use the merge functionality:
# Merge LoRA weights into base model
model.merge_lora_weights()
# Save merged model
model.save_pretrained("path/to/merged/model")
๐ License & Citation
This project is licensed under the MIT License - see the LICENSE file for details.
Citation
If you use plimai in your research, please cite:
@software{plimai2025,
author = {Pritesh Raj},
title = {plimai: Vision LLMs with Efficient LoRA Fine-Tuning},
url = {https://github.com/plim-ai/plim},
year = {2025},
version = {1.0.0}
}
๐ Acknowledgements
We thank the following projects and communities:
- PyTorch - Deep learning framework
- HuggingFace - Transformers and model hub
- timm - Vision model implementations
- PEFT - Parameter-efficient fine-tuning methods
Made in India ๐ฎ๐ณ with โค๏ธ by the plimai
Star โญ this repo if you find it useful!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file plimai-0.1.9.tar.gz.
File metadata
- Download URL: plimai-0.1.9.tar.gz
- Upload date:
- Size: 78.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ebf326f68ba651564579aff75d6990bf3cd6daec5adeac9b817210f9d92e4a2
|
|
| MD5 |
027eaf74746489591de863b6ad6ce7cc
|
|
| BLAKE2b-256 |
79b01260996cdb524dfc1410b607b233b63d81499f4c30258823f29ee624dc87
|
File details
Details for the file plimai-0.1.9-py3-none-any.whl.
File metadata
- Download URL: plimai-0.1.9-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55241bb000c2c9c15a78cac9a6bd9172a94841c5046688a7cd64859e84bf7dec
|
|
| MD5 |
e5d0fe24494c83d563b954bf57527f3a
|
|
| BLAKE2b-256 |
73489f71748dfcf86dfe49f217bb7b2170cde11649e19f9e8dd1ea9f619c05b4
|