Documentation
¶
Overview ¶
Package vision provides local image understanding via ONNX-based VLM models (Florence-2). Supports image captioning (describe) and visual question answering (ask) with a shared inference engine.
Index ¶
- Constants
- Variables
- func DefaultModelsDir() string
- func ExpandPrompt(prompt string) string
- func InitModel(modelsDir, variant string, force bool) error
- func IsReady(modelsDir, variant string) bool
- func ListModels()
- func PreprocessImage(path string) ([]float32, error)
- func PreprocessImageFromImage(img image.Image, targetSize int) ([]float32, error)
- func VariantDir(modelsDir, variant string) string
- type Engine
- type EngineConfig
- type ModelFile
- type ModelVariant
- type Sampler
- type TaskPrompt
- type Tokenizer
Constants ¶
const ( // InputSize is the expected image dimension (768×768). InputSize = 768 // Channels is the number of color channels. Channels = 3 // HiddenDim is the hidden dimension size for all sub-models. HiddenDim = 768 // NumDecoderLayers is the number of decoder layers. NumDecoderLayers = 6 // NumAttentionHeads is the number of attention heads. NumAttentionHeads = 12 // HeadDim is the dimension per attention head. HeadDim = 64 // ImageSeqLength is the number of visual tokens output by vision_encoder. ImageSeqLength = 577 // MaxTokens is the maximum number of tokens to generate per inference. MaxTokens = 512 // VocabSize is the Florence-2 vocabulary size. VocabSize = 51289 // DecoderStartTokenID is the decoder start token (also EOS). DecoderStartTokenID = 2 // EOSTokenID is the end-of-sequence token ID. EOSTokenID = 2 // PadTokenID is the padding token ID. PadTokenID = 1 )
Model constants for Florence-2-base-ft (Heliosoph ONNX export).
const DefaultModelVariant = "base-int8"
Variables ¶
var ( MeanRGB = [3]float32{0.485, 0.456, 0.406} StdRGB = [3]float32{0.229, 0.224, 0.225} )
ImageNet normalization parameters (used by Florence-2).
var AvailableModelVariants = []ModelVariant{
{ID: "base-int8", Description: "Florence-2 Base INT8 (quantized)", Size: "~270 MB"},
}
Functions ¶
func DefaultModelsDir ¶
func DefaultModelsDir() string
func ExpandPrompt ¶
ExpandPrompt expands a Florence-2 task token into its full prompt text. Tokens without an expansion (e.g. <VQA>) are returned as-is.
func PreprocessImage ¶
PreprocessImage decodes an image file and preprocesses it into a normalized CHW float32 tensor suitable for Florence-2 encoder input.
Steps:
- Decode image from file
- Resize to InputSize×InputSize (224×224)
- Convert to CHW layout
- Normalize using ImageNet mean/std
func PreprocessImageFromImage ¶
PreprocessImageFromImage preprocesses a decoded image for Florence-2.
func VariantDir ¶
Types ¶
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine wraps the four Florence-2 ONNX sub-models into a single inference engine.
func NewEngine ¶
func NewEngine(cfg *EngineConfig) (*Engine, error)
NewEngine creates and initializes the vision engine with all four ONNX sessions.
type EngineConfig ¶
type ModelFile ¶
ModelFile describes a downloadable model file.
func ModelFiles ¶
ModelFiles returns the list of files to download for a given variant.
type ModelVariant ¶
ModelVariant describes a Florence-2 ONNX model variant.
func ResolveModelVariant ¶
func ResolveModelVariant(id string) (ModelVariant, error)
type Sampler ¶
Sampler handles token sampling strategies for autoregressive generation.
func DefaultSampler ¶
func DefaultSampler() *Sampler
DefaultSampler returns a sampler with greedy decoding.
func NewSampler ¶
NewSampler creates a sampler with the given parameters. temperature=0 means greedy (always pick highest probability).
type TaskPrompt ¶
type TaskPrompt string
TaskPrompt is a Florence-2 task token that controls the model's output mode.
const ( TaskCaption TaskPrompt = "<CAPTION>" TaskDetailedCaption TaskPrompt = "<DETAILED_CAPTION>" TaskMoreDetailedCaption TaskPrompt = "<MORE_DETAILED_CAPTION>" TaskOD TaskPrompt = "<OD>" )
type Tokenizer ¶
type Tokenizer struct {
// contains filtered or unexported fields
}
Tokenizer implements GPT-2's byte-level BPE tokenizer, used by Florence-2.
Usage:
tk, err := NewTokenizer(vocabPath, mergesPath)
ids := tk.Encode("<DETAILED_CAPTION>")
text := tk.Decode(ids)
func NewTokenizer ¶
NewTokenizer loads a GPT-2 BPE tokenizer from vocab.json and merges.txt. vocabPath is the path to vocab.json, mergesPath is the path to merges.txt.
func (*Tokenizer) EncodeTokens ¶
EncodeTokens converts a string to token strings (for debugging).