Basic Model Loading
create_model()
The core function for creating CLIP models with flexible configuration options.Model architecture name (e.g., ‘ViT-B-32’, ‘RN50’) or schema-prefixed path:
- Built-in:
'ViT-B-32' - HuggingFace Hub:
'hf-hub:org/repo' - Local directory:
'local-dir:/path/to/model'
Pretrained weights source. Can be:
- Tag name (e.g., ‘openai’, ‘laion2b_s34b_b79k’)
- Local file path (e.g., ‘/path/to/weights.pt’)
- Ignored if model_name uses schema prefix
Device to load model on (‘cpu’, ‘cuda’, etc.)
Model precision: ‘fp32’, ‘fp16’, ‘bf16’, ‘pure_fp16’, ‘pure_bf16’
Whether to JIT compile the model
Override default image size for the model
Directory for caching downloaded weights
Loading Schemas
HuggingFace Hub
Load models directly from HuggingFace Hub using thehf-hub: schema:
- Downloads
open_clip_config.jsonfrom the repo - Looks for weights files (
.safetensors,.bin,.pth) - Merges preprocessing configuration
Local Directory
Load from a local directory containing model config and weights:Local directory must contain:
open_clip_config.jsonwith model configuration- Weight file (searched in order):
open_clip_model.safetensors,pytorch_model.bin,model.pth, etc.
Local File Path
Load weights from a specific file:Advanced Loading Options
Tower-Specific Weights
Load separate weights for image and text towers:Load default pretrained weights for image tower (timm models)
Load default pretrained weights for text tower (HuggingFace models)
Path to custom image tower weights (loaded after full model)
Path to custom text tower weights (loaded after full model)
Custom Model Configuration
Override model architecture parameters:create_model_and_transforms()
Convenience function that returns model with preprocessing transforms:(model, train_transform, val_transform). The transforms handle:
- Image resizing and cropping
- Normalization with correct mean/std
- Data augmentation (training only)
create_model_from_pretrained()
Strictly requires pretrained weights (raises error if weights can’t be loaded):Whether to return preprocessing transform. If False, returns only model.
Listing Available Models
Weight Loading Options
Whether to load the resolved pretrained weights. Set to False for random initialization.
Raise error if pretrained weights cannot be loaded
Use
weights_only=True for torch.load (safer, prevents arbitrary code execution)