Nep-CLIP: CLIP for Nepali
Nep-CLIP gives CLIP a Nepali text encoder, so Nepali text and images can be matched directly, without translating to English first. It supports Nepali image-text retrieval and zero-shot image classification with Nepali labels.
English CLIP cannot read Nepali: given Nepali text, it scores close to zero on every benchmark. Nep-CLIP closes most of that gap.
- 💻 Code, evaluation and demo app: github.com/sagarsishir51/Nep-CLIP
- 🎓 Master's thesis, Thapathali Campus, Tribhuvan University
Models
| Model | File | Nepali text encoder | Final training loss (MSE) |
|---|---|---|---|
| Nep-CLIP (RoBERTa) | nep-clip-roberta/model.pt | IRIISNEPAL/RoBERTa_Nepali_110M | 0.0139 |
| Nep-CLIP (BERT) | nep-clip-nepaliBERT/model.pt | Shushant/nepaliBERT | 0.0163 |
Both are used with the original, unchanged CLIP image encoder (openai/clip-vit-base-patch32). Only the text side is new.
How it works
Cross-lingual knowledge distillation:
- Teacher: the frozen CLIP text encoder embeds an English caption.
- Student: a Nepali text encoder plus a linear projection (768 → 512) embeds the Nepali translation of the same caption.
- Training: the student is trained to produce the same embedding as the teacher (MSE loss).
- Result: the student lands in CLIP's embedding space, so it works with CLIP's image encoder without any image training.
Training details
| Setting | Value |
|---|---|
| Training data | Nepali translations of CC3M and MS-COCO captions, with preprocessing |
| Loss | MSE between student and teacher text embeddings |
| Optimizer | Adam, learning rate 5e-5, 1,000 warmup steps |
| Batch size | 64 |
| Epochs | 4 (checkpoint at step 150,000) |
| Max text length | 77 tokens (CLIP's limit) |
| Trainable parts | Nepali text encoder + projection (CLIP is frozen) |
Usage
Requires torch, transformers 5.x (tested with 5.12.1), huggingface_hub and Pillow. The RoBERTa Nepali tokenizer does not load with older transformers versions such as 4.38.
import torch
import torch.nn as nn
import torch.nn.functional as F
from PIL import Image
from huggingface_hub import hf_hub_download
from transformers import AutoModel, AutoTokenizer, CLIPModel, CLIPProcessor
# Choose a model
STUDENT_ID, CKPT_FILE = "IRIISNEPAL/RoBERTa_Nepali_110M", "nep-clip-roberta/model.pt"
# STUDENT_ID, CKPT_FILE = "Shushant/nepaliBERT", "nep-clip-nepaliBERT/model.pt"
# Nepali text encoder: student model + linear projection into CLIP's 512-d space
ckpt = torch.load(hf_hub_download("sagarsishir51/Nep-CLIP", CKPT_FILE), map_location="cpu")
tokenizer = AutoTokenizer.from_pretrained(STUDENT_ID, do_lower_case=False, strip_accents=False)
student = AutoModel.from_pretrained(STUDENT_ID)
student.load_state_dict(ckpt["student_state_dict"])
projection = nn.Linear(student.config.hidden_size, 512)
projection.load_state_dict(ckpt["projection_state_dict"])
student.eval(); projection.eval()
# Image encoder: the original, unchanged CLIP ViT-B/32
clip = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").eval()
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
labels = ["बिरालो", "कुकुर", "घोडा"] # cat, dog, horse
image = Image.open("cat.jpg").convert("RGB")
with torch.no_grad():
tok = tokenizer(labels, padding="max_length", truncation=True, max_length=77, return_tensors="pt")
text_emb = F.normalize(projection(student(**tok).pooler_output), dim=-1)
image_emb = clip.get_image_features(**processor(images=image, return_tensors="pt"))
image_emb = F.normalize(getattr(image_emb, "pooler_output", image_emb), dim=-1)
probs = (100 * image_emb @ text_emb.T).softmax(dim=-1)[0]
for label, p in zip(labels, probs.tolist()):
print(f"{label}: {p:.3f}")
For image retrieval, encode a Nepali query with the text encoder and rank images by cosine similarity with their CLIP image embeddings.
Results
The best Nep-CLIP model against the best multilingual baseline (NLLB-CLIP or M-CLIP) and the English upper bound:
| Task | Benchmark | Metric | Nep-CLIP (best) | Best baseline | CLIP-English* (English input) |
|---|---|---|---|---|---|
| Image → Text retrieval | Flickr30k | R@1 | 74.80 (RoBERTa) | 59.20 (M-CLIP) | 79.50 |
| Text → Image retrieval | Flickr30k | R@1 | 52.78 (RoBERTa) | 47.22 (NLLB-CLIP) | 58.76 |
| Image → Text retrieval | XTD10 | R@1 | 55.60 (RoBERTa) | 44.20 (NLLB-CLIP) | 56.00 |
| Zero-shot classification | Babel-ImageNet Nepali | Top-1 | 52.13 (RoBERTa) | 39.87 (M-CLIP) | 84.72 |
| Zero-shot classification | CIFAR-10 | Top-1 | 89.93 (BERT) | 82.50 (NLLB-CLIP) | 89.49 |
| Zero-shot classification | CIFAR-100 | Top-1 | 39.10 (BERT) | 30.29 (NLLB-CLIP) | 49.53 |
| Zero-shot classification | Local Nepali dataset | Top-1 | 33.40 (RoBERTa) | 26.20 (NLLB-CLIP) | 55.40 |
English CLIP given Nepali text instead scores below 3% Top-1 / R@1 on every benchmark except CIFAR-10 (11.98%). Full results for every model and metric are in the GitHub repository.
Limitations
- Nepal-specific concepts are hard. On the local benchmark, Nep-CLIP (RoBERTa) reaches 53.20 Top-1 on global concepts but only 13.60 on Nepal-specific ones. English CLIP drops in the same way, so the weakness comes largely from CLIP's image encoder, which was never trained on these concepts.
- The image side is CLIP's. Nep-CLIP inherits CLIP ViT-B/32's image understanding, resolution and biases.
- Text length: 77 tokens at most.
- Translated data: training and most evaluation captions are machine-translated, so translation errors affect both training and measured scores.
- Checkpoint size: each
model.ptalso contains optimizer and scheduler states from training, which make the files much larger than the model weights alone.
Intended use and license
These checkpoints are provided for educational purposes only and may not be used commercially. The code in the GitHub repository is MIT-licensed; that license does not cover these checkpoints. The base models (CLIP, NepaliBERT, RoBERTa Nepali 110M) keep their own licenses.
Citation
A citation will be added once the paper is published.
Acknowledgements
Built on OpenAI CLIP, NepaliBERT and RoBERTa Nepali 110M, and trained on translated Conceptual Captions (CC3M) and MS-COCO captions.