# Openvision Unified Visual Encoder

> Learn a single visual representation supporting both image understanding and generation by combining VAE-based generative training with contrastive learning objectives. Use when building multimodal systems that need unified image representations for both comprehension and generation tasks.

- Skill: `adu2021/openvision-unified-visual-encoder` (Agent Skill)
- Install (CLI): `npx skillmds@latest add adu2021/openvision-unified-visual-encoder`
- Raw SKILL.md: https://api.skillmd.com/api/skills/adu2021/openvision-unified-visual-encoder/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: MIT
- Author: adu2021 (https://skillmd.com/u/adu2021)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/adu2021/openvision-unified-visual-encoder

---


# OpenVision 3: Unified Visual Encoder

This skill demonstrates how to train a single visual encoder that excels at both image understanding (comprehension) and generation (creation) tasks, unifying two typically separate concerns through combined VAE and contrastive objectives.

## When to Use
- Building multimodal models needing both image understanding and generation
- Creating unified representations for efficiency (one encoder, multiple tasks)
- Systems where image generation and understanding must be aligned
- Vision-language models requiring rich image representations
- Scenarios where parameter efficiency is valuable (single encoder vs. dual)

## When NOT to Use
- Specialized tasks where separate decoders outperform unified approaches
- Real-time systems where combined training adds overhead
- Domains where separate encoders are already well-established
- Simple classification (unified encoder may be overkill)

## Key Concept
Traditionally, image understanding and generation require separate encoders:
- **Understanding**: Discriminative encoders (contrastive learning, classification)
- **Generation**: Generative encoders (VAE, diffusion models)

OpenVision 3 achieves both with a unified encoder through joint training:

1. **Contrastive Learning**: Learn discriminative features for understanding
2. **VAE Reconstruction**: Maintain generative capability through reconstruction
3. **Shared Representation**: Single latent space for both tasks

The encoder learns representations that are both discriminative (good for classification/matching) and generative (good for reconstruction/synthesis).

## Implementation Pattern

Combine VAE and contrastive losses for unified encoder:

```python
# Pseudocode for unified visual encoder training
class UnifiedVisualEncoder:
    def __init__(self, encoder, decoder):
        self.encoder = encoder
        self.decoder = decoder  # VAE decoder for generation

    def forward(self, image):
        # Encode image
        latent = self.encoder(image)
        return latent

    def compute_unified_loss(self, batch_images):
        loss = 0

        # Component 1: Contrastive learning (understanding)
        # Use pairs or triplets of similar/different images
        contrastive_loss = self.contrastive_loss(batch_images)
        loss += contrastive_loss

        # Component 2: VAE reconstruction (generation)
        # Reconstruct images from latent representation
        reconstruction_loss = self.vae_reconstruction_loss(batch_images)
        loss += reconstruction_loss

        # Component 3: Optional KL divergence (for generative modeling)
        kl_loss = self.kl_divergence_loss()
        loss += 0.1 * kl_loss  # Weight KL component

        return loss

    def contrastive_loss(self, batch_images):
        # Get encodings for batch
        latents = self.encoder(batch_images)

        # Contrastive learning: maximize similarity within augmentation,
        # minimize across different images
        # Using NT-Xent loss or similar

        similarities = compute_pairwise_similarity(latents)
        loss = contrastive_criterion(similarities, labels)
        return loss

    def vae_reconstruction_loss(self, batch_images):
        # Reconstruct images from latent
        latents = self.encoder(batch_images)
        reconstructed = self.decoder(latents)

        # MSE or perceptual loss
        loss = mse_loss(reconstructed, batch_images)
        return loss

    def generate_image(self, latent_code):
        # Use decoder for generation
        return self.decoder(latent_code)

    def understand_image(self, image):
        # Use encoder for understanding
        latent = self.encoder(image)
        return latent  # Use for downstream tasks
```

## Key Results
- Single encoder achieves strong performance on both understanding and generation
- Parameter efficiency: one encoder vs. separate understanding/generation models
- Learned representations capture both discriminative and generative aspects
- Scalable across multiple model sizes

## Research Context
OpenVision 3 challenges the assumption that understanding and generation require separate encoders. By jointly training on both objectives, the encoder learns richer representations that capture essential visual information across both domains, enabling efficient unified vision systems.

