CoCa<T>

Models & Types · AiDotNet.VisionLanguage.Generative

CoCa (Contrastive Captioners): dual-loss model combining contrastive and captioning objectives.

Inheritance: NeuralNetworkBase<T> → VisionLanguageModelBase<T> → CoCa<T>

Implements: IGenerativeVisionLanguageModel<T>

For Beginners

CoCa combines two training objectives in one model: contrastive learning (matching images to text descriptions) and captioning (generating text from images). This dual approach produces strong image-text embeddings that work well for both retrieval and generation tasks, making it a versatile foundation model. Default values follow the original paper settings.

How It Works

CoCa (Yu et al., TMLR 2022) combines contrastive learning with generative captioning in a single model. The image encoder and the unimodal text decoder share a contrastive loss, while a multimodal text decoder generates captions with cross-attention to image features.

References:

  • Paper: “CoCa: Contrastive Captioners are Image-Text Foundation Models” (Yu et al., TMLR 2022)

Example

// Create a CoCa model combining contrastive and captioning objectives
// for both image-text retrieval and generative captioning
var architecture = new NeuralNetworkArchitecture<double>(
    inputType: InputType.TwoDimensional,
    taskType: NeuralNetworkTaskType.ImageClassification,
    inputHeight: 224, inputWidth: 224, inputDepth: 3, outputSize: 512);
            
// ONNX inference mode with pre-trained model
var model = new CoCa<double>(architecture, "coca.onnx");
            
// Training mode with native layers
var trainModel = new CoCa<double>(architecture, new CoCaOptions());

Constructors

Constructor Summary
CoCa(NeuralNetworkArchitecture<T> architecture, CoCaOptions options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer)
CoCa(NeuralNetworkArchitecture<T> architecture, string modelPath, CoCaOptions options)

Properties

Property Summary
int DecoderEmbeddingDim { get; }
int EmbeddingDimension { get; }
int MaxGenerationLength { get; }

Methods

Method Summary
ModelMetadata<T> GetModelMetadata()
Tensor<T> EncodeImage(Tensor<T> image)
Tensor<T> GenerateFromImage(Tensor<T> image, string prompt) Generates text using CoCa’s dual contrastive-captioning architecture.