Skip to main content

Minimal pure-PyTorch CLIP implementation compatible with OpenAI CLIP safetensors.

Project description

nanoclip

Minimal pure-PyTorch CLIP implementation compatible with OpenAI CLIP weights on Hugging Face (safetensors).

Install

uv sync

Usage

import torch
from PIL import Image
from nanoclip import CLIPModel, NanoCLIPProcessor

# Loads from Hugging Face and caches under ~/.cache/nanoclip/hf/
model = CLIPModel.from_pretrained(
    "openai/clip-vit-large-patch14",
    only_local_files=False,
)
processor = NanoCLIPProcessor.from_pretrained(
    "openai/clip-vit-large-patch14",
    only_local_files=False,
)
model.eval()

image = Image.open("/path/to/image.jpg").convert("RGB")
encoded = processor(
    text=["a photo of a cat", "a photo of a dog"],
    images=image,
    return_tensors="pt",
    padding=True,
)

with torch.no_grad():
    outputs = model(input_ids=encoded["input_ids"], pixel_values=encoded["pixel_values"])
    # outputs["logits_per_image"], outputs["logits_per_text"]

Set only_local_files=True to disallow downloads and require files to already exist in cache_dir.

Quick Inference

uv run python scripts/infer_image_text.py \
  --image assets/image.jpg \
  --texts "image of a cat" \
  --texts "image of a dog" \
  --cache-dir ~/.cache/nanoclip/hf

Expected output:

Best match: image of a cat

Scores:
0.999235	image of a cat
0.000765	image of a dog

Tests

uv run pytest

Compare with Transformers

uv run --group transformers python scripts/compare_nano_with_transformers.py \
  --model-id openai/clip-vit-large-patch14 \
  --cache-dir ~/.cache/nanoclip/hf

The script loads both models from the same --cache-dir root and asserts embeddings/logits are numerically close.

Compare Processor with Transformers

uv run --group transformers python scripts/compare_nano_processor_with_transformers.py \
  --model-id openai/clip-vit-large-patch14 \
  --cache-dir ~/.cache/nanoclip/hf

The script compares input_ids, attention_mask, and pixel_values between NanoCLIPProcessor and transformers.CLIPProcessor.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nanoclip-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nanoclip-0.1.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file nanoclip-0.1.0.tar.gz.

File metadata

  • Download URL: nanoclip-0.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for nanoclip-0.1.0.tar.gz
Algorithm Hash digest
SHA256 08a386106c84a779773066cbd094117d54e15875a3caf65391e1d48f0d38cd14
MD5 6ad4750c5b1e4737e86112ce1c8570ee
BLAKE2b-256 58d0dddbd16bfce17331269632a8aba9d809cc927c228594f51f35d07970242d

See more details on using hashes here.

File details

Details for the file nanoclip-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nanoclip-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for nanoclip-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 341a70bec14248e27070a439bd2797e3c6043e7d5fa5e9717d69ac86d4affa09
MD5 2d4208b968ce1e8fb05ffc103f05d759
BLAKE2b-256 eb1a53aa83b9a217c4d0031dae5fe9839ff0756d115342cb2b2415988c0e95a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page