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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
tags:
|
| 4 |
+
- vision-language
|
| 5 |
+
- mixture-of-experts
|
| 6 |
+
- text-generation
|
| 7 |
+
- vision-transformer
|
| 8 |
+
- pytorch
|
| 9 |
+
model_index:
|
| 10 |
+
- name: SparseFusion
|
| 11 |
+
results:
|
| 12 |
+
- task:
|
| 13 |
+
type: text-generation
|
| 14 |
+
dataset:
|
| 15 |
+
name: Custom Caption Dataset
|
| 16 |
+
type: custom
|
| 17 |
+
metrics:
|
| 18 |
+
- name: Validation Loss
|
| 19 |
+
type: loss
|
| 20 |
+
value: 0.8
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
# SparseFusion
|
| 24 |
+
|
| 25 |
+
**SparseFusion** is a multimodal Mixture-of-Experts (MoE) model integrating a Vision Transformer (ViT) and transformer decoder for image-conditioned text generation. It is built entirely in PyTorch and extends [SeeMOE](https://github.com/AviSoori1x/seemore).
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## 🧠 Model Details
|
| 30 |
+
|
| 31 |
+
- **Name**: SparseFusion
|
| 32 |
+
- **Author**: Derrick Kirimi ([GitHub](https://github.com/DerrickKirimi) · [LinkedIn](https://www.linkedin.com/in/derrick-kirimi-22a470175/) · [Hugging Face](https://huggingface.co/Aptheos))
|
| 33 |
+
- **Model Type**: Vision-Language Model
|
| 34 |
+
- **Architecture**:
|
| 35 |
+
- Vision Encoder: ViT (96×96 images, 16×16 patches, 512-dim patch embeddings)
|
| 36 |
+
- Decoder: Transformer with MoE layers (8 layers, 128-dim, 8 heads)
|
| 37 |
+
- MoE Setup: 8 experts, top-2 routing, expert capacity control
|
| 38 |
+
- Token Fusion: Concatenation of image tokens and character-level encoded text
|
| 39 |
+
- **License**: Apache 2.0
|
| 40 |
+
- **Repository**: [GitHub - DerrickKirimi/SparseFusion](https://github.com/DerrickKirimi/SparseFusion)
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## 🌟 Intended Use
|
| 45 |
+
|
| 46 |
+
- **Primary Use Case**: Image-conditioned text generation for educational and research experimentation
|
| 47 |
+
- **Intended Users**: ML researchers, students, developers
|
| 48 |
+
- **Out-of-Scope Uses**: Not suitable for deployment in production or for generating harmful content
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
## 🏋️♂️ Training & Evaluation
|
| 53 |
+
|
| 54 |
+
### 📅 Dataset
|
| 55 |
+
|
| 56 |
+
- **Text**: Tiny Shakespeare (character-level)
|
| 57 |
+
- **Images**: 300 synthetic image-caption pairs
|
| 58 |
+
|
| 59 |
+
### ⚙️ Training
|
| 60 |
+
|
| 61 |
+
- Trained for 2 epochs on **Google Colab (1 GPU, 12 GB VRAM)**
|
| 62 |
+
- Logging via **Weights & Biases (wandb)**
|
| 63 |
+
|
| 64 |
+
### 📊 Hyperparameters
|
| 65 |
+
|
| 66 |
+
```yaml
|
| 67 |
+
epochs: 2
|
| 68 |
+
batch_size: 16
|
| 69 |
+
learning_rate: 0.001
|
| 70 |
+
n_embd: 128
|
| 71 |
+
n_head: 8
|
| 72 |
+
n_layer: 8
|
| 73 |
+
num_experts: 8
|
| 74 |
+
top_k: 2
|
| 75 |
+
expert_capacity: 32
|
| 76 |
+
img_size: 96
|
| 77 |
+
patch_size: 16
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### 📈 Evaluation
|
| 81 |
+
|
| 82 |
+
- **Validation Loss**: 0.8 after 2 epochs
|
| 83 |
+
- **Summary**:
|
| 84 |
+
- Generates basic coherent text
|
| 85 |
+
- Shows 15% improvement in expert utilization with routing control and load balancing
|
| 86 |
+
|
| 87 |
+
---
|
| 88 |
+
|
| 89 |
+
## 🚀 Usage
|
| 90 |
+
|
| 91 |
+
### 📦 Installation
|
| 92 |
+
|
| 93 |
+
```bash
|
| 94 |
+
pip install torch torchvision transformers huggingface_hub wandb
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
### 🔄 Inference
|
| 98 |
+
|
| 99 |
+
```python
|
| 100 |
+
import torch
|
| 101 |
+
import pickle
|
| 102 |
+
from PIL import Image
|
| 103 |
+
import torchvision.transforms as transforms
|
| 104 |
+
from huggingface_hub import hf_hub_download
|
| 105 |
+
|
| 106 |
+
# Load vocabulary mappings
|
| 107 |
+
stoi = pickle.load(open(hf_hub_download("Aptheos/SparseFusion", "stoi.pkl"), "rb"))
|
| 108 |
+
itos = pickle.load(open(hf_hub_download("Aptheos/SparseFusion", "itos.pkl"), "rb"))
|
| 109 |
+
encode = lambda s: [stoi[c] for c in s]
|
| 110 |
+
decode = lambda l: ''.join([itos[i] for i in l])
|
| 111 |
+
|
| 112 |
+
# Define model architecture
|
| 113 |
+
model = VisionMoELanguageModel(
|
| 114 |
+
n_embd=128, image_embed_dim=512, vocab_size=len(stoi), n_layer=8,
|
| 115 |
+
img_size=96, patch_size=16, num_heads=8, num_blks=3,
|
| 116 |
+
emb_dropout=0.1, blk_dropout=0.1, num_experts=8, top_k=2, expert_capacity=32
|
| 117 |
+
)
|
| 118 |
+
model.load_state_dict(torch.load(hf_hub_download("Aptheos/SparseFusion", "vision_moe_model.pth")))
|
| 119 |
+
model.eval().to("cuda")
|
| 120 |
+
|
| 121 |
+
# Preprocess image
|
| 122 |
+
transform = transforms.Compose([
|
| 123 |
+
transforms.Resize((96, 96)),
|
| 124 |
+
transforms.ToTensor(),
|
| 125 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 126 |
+
])
|
| 127 |
+
image = transform(Image.open("example.jpg")).unsqueeze(0).to("cuda")
|
| 128 |
+
prompt = torch.tensor([encode("A photo of")], dtype=torch.long).to("cuda")
|
| 129 |
+
|
| 130 |
+
# Generate text
|
| 131 |
+
generated = model.generate(image, prompt, max_new_tokens=50)
|
| 132 |
+
print(decode(generated[0].tolist()))
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
To run on CPU:
|
| 136 |
+
|
| 137 |
+
```python
|
| 138 |
+
model.eval().to("cpu")
|
| 139 |
+
image = image.to("cpu")
|
| 140 |
+
prompt = prompt.to("cpu")
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
---
|
| 144 |
+
|
| 145 |
+
## ⚠️ Limitations & Biases
|
| 146 |
+
|
| 147 |
+
### Limitations
|
| 148 |
+
|
| 149 |
+
- The model generates incoherent text (e.g., `"A photo ofiecp ntti<pad><pad>..."`) due to training on a small, synthetic dataset of 300 identical images with simplistic captions.
|
| 150 |
+
- Vision encoder (ViT) is **not pre-trained**, reducing visual feature quality.
|
| 151 |
+
- Character-level tokenization limits text fluency and introduces `<pad>` tokens.
|
| 152 |
+
- Limited training time (2 epochs) restricts deep multimodal learning.
|
| 153 |
+
|
| 154 |
+
### Biases
|
| 155 |
+
|
| 156 |
+
- Synthetic captions create bias toward repetitive language structures.
|
| 157 |
+
- Lack of diverse image inputs may bias the model’s visual representation.
|
| 158 |
+
|
| 159 |
+
---
|
| 160 |
+
|
| 161 |
+
## 🔭 Future Work
|
| 162 |
+
|
| 163 |
+
- Train on larger datasets (e.g., COCO, Flickr30k) for better generalization
|
| 164 |
+
- Use pre-trained ViT backbone (e.g., `timm/vit_small_patch16_224`)
|
| 165 |
+
- Implement subword tokenization (e.g., SentencePiece, BPE)
|
| 166 |
+
- Add modality type embeddings and rotary positional embeddings (RoPE)
|
| 167 |
+
- Visualize expert routing and attention patterns for interpretability
|
| 168 |
+
- Increase training epochs and perform hyperparameter tuning
|
| 169 |
+
|
| 170 |
+
---
|
| 171 |
+
|
| 172 |
+
## 📄 License
|
| 173 |
+
|
| 174 |
+
Licensed under the **Apache 2.0 License** for open research and educational use.
|
| 175 |
+
|
| 176 |
+
---
|
| 177 |
+
|
| 178 |
+
## 📚 Citation
|
| 179 |
+
|
| 180 |
+
```bibtex
|
| 181 |
+
@misc{sparsefusion2025,
|
| 182 |
+
author = {Derrick Kirimi},
|
| 183 |
+
title = {SparseFusion: A Multimodal Mixture-of-Experts Model},
|
| 184 |
+
year = {2025},
|
| 185 |
+
url = {https://huggingface.co/Aptheos/SparseFusion}
|
| 186 |
+
}
|
| 187 |
+
```
|