Documentation
¶
Overview ¶
Package model handles CLIP ONNX model downloading, loading, and inference.
Index ¶
- Constants
- Variables
- func EnsureModels(progressFn func(filename string, downloaded, total int64)) error
- func FilePath(name string) (string, error)
- func IsUnicodeLetter(r rune) bool
- func ModelsDir() (string, error)
- func PreprocessImage(path string) ([]float32, error)
- type CLIPSession
- type ModelFile
- type Tokenizer
Constants ¶
const BaselineCategory = "uncategorized"
BaselineCategory is the internal label for the baseline "catch-all" prompt used to prevent false-positive classification.
Variables ¶
var RequiredFiles = []ModelFile{
{
Name: "model.onnx",
URL: hfBaseURL + "/onnx/model.onnx",
},
{
Name: "vocab.json",
URL: hfBaseURL + "/vocab.json",
},
{
Name: "merges.txt",
URL: hfBaseURL + "/merges.txt",
},
}
RequiredFiles defines all files needed for CLIP inference.
Functions ¶
func EnsureModels ¶
EnsureModels checks that all required files exist, downloading any that are missing.
func IsUnicodeLetter ¶
IsUnicodeLetter checks if a rune is a unicode letter (exported for testing).
func PreprocessImage ¶
PreprocessImage loads an image file and returns a float32 tensor in [1, 3, 224, 224] CHW format, normalized for CLIP.
Types ¶
type CLIPSession ¶
type CLIPSession struct {
// contains filtered or unexported fields
}
CLIPSession holds a loaded CLIP model ready for inference.
func NewCLIPSession ¶
func NewCLIPSession(explicitPath string) (*CLIPSession, error)
NewCLIPSession creates a new CLIP inference session. If explicitPath is empty, it tries the embedded library first, then platform defaults.
func (*CLIPSession) Classify ¶
Classify runs zero-shot classification on an image against the given categories. A baseline "uncategorized" prompt is injected to prevent false positives (especially with few categories). Returns a map of category names to their similarity scores (after softmax), including the baseline.
func (*CLIPSession) Destroy ¶
func (c *CLIPSession) Destroy()
Destroy releases resources held by the CLIP session.
type ModelFile ¶
type ModelFile struct {
Name string
URL string
SHA256 string // expected hash (empty = skip verification)
}
ModelFile describes a file to download.
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer implements CLIP's BPE tokenization.
func LoadTokenizer ¶
LoadTokenizer loads the tokenizer from vocab.json and merges.txt files.
func TokenizerFromModelsDir ¶
TokenizerFromModelsDir loads the tokenizer from the standard models directory.
func (*Tokenizer) Encode ¶
Encode tokenizes a text string and returns token IDs padded/truncated to contextLen.
func (*Tokenizer) EncodeCategories ¶
EncodeCategories tokenizes a batch of category labels using CLIP's prompt template.