Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Multimodal pre-trained models

Multimodal pre-trained models

Multimodal pre-trained models (Multimodal Pre-trained Models) refer to models that can simultaneously process and understandmultiple data modalities(such as text, images, audio, etc.). Unlike traditional single-modality models, these models learn the associations and correspondences between different modalities through large-scale pre-training.

Core advantages of multimodal learning

  1. Information complementarity: different modalities can provide complementary information (e.g., images provide visual information, text provides semantic information)
  2. Enhanced robustness: when one modality's data is missing or of poor quality, other modalities can provide support
  3. Expanded application scenarios: support richer cross-modal tasks (such as image-text retrieval and image caption generation)

CLIP: A milestone in image-text contrastive learning

Basic concepts

CLIP (Contrastive Language-Image Pre-training) is a multimodal model proposed by OpenAI in 2021 that establishes the association between images and text through contrastive learning.

CLIP consists of two core components:

Workflow:

  1. Input:
  2. Image and text pairs (e.g., a photo of a dog + the description "a photo of a dog").
  3. - Image and text pairs (e.g., a photo of a dog + the description "a photo of a dog").
    1. Encoding:
    2. The image encoder extracts image features and the text encoder extracts text features.
    3. - The image encoder extracts image features and the text encoder extracts text features.
      1. Contrastive learning:
      2. Compute the similarity matrix of all image-text pairs, and optimize the model through a loss function (e.g., InfoNCE) so that matched pairs' features move closer and unmatched pairs' features move apart.
      3. - Compute the similarity matrix of all image-text pairs, and optimize the model through a loss function (e.g., InfoNCE) so that matched pairs' features move closer and unmatched pairs' features move apart.

        The feature vectors output by both are mapped into the same semantic space, aligning the image and text representations through contrastive learning.

        Explanation of key parts in the figure

        Table part: contrastive learning matrix

        The table shows the similarity computation of image-text pairs (assuming there are N texts and 4 images):

        Goals:
        Maximize the similarity on the diagonal (correct pairs, e.g. I1-T1), minimize the off-diagonal similarity (incorrect pairs, e.g. I1-T2). This is the core idea of contrastive learning.

        Example section

        - "Pepper the aussie pup" (a photo of an Australian Shepherd dog). - "Planer car dog" (possibly noise or a mislabel; it should actually be template text of the form "A photo of a (object)"). - "A photo of a (object)" is a text prompt template commonly used during CLIP pre-training to generalize across different categories (e.g., "a photo of a dog").

        Model architecture

        1. Dual-encoder structure:
        2. Image encoder: commonly Vision Transformer (ViT) or ResNet
        3. Text encoder: based on the Transformer architecture
        4. - Image encoder: commonly Vision Transformer (ViT) or ResNet - Text encoder: based on the Transformer architecture
          1. Contrastive learning objective:
          2. Positive sample pairs (matching image-text pairs) are close in the feature space
          3. Negative sample pairs (non-matching image-text pairs) are far apart in the feature space
          4. - Positive sample pairs (matching image-text pairs) are close in the feature space - Negative sample pairs (non-matching image-text pairs) are far apart in the feature space

            Training process

            Example

            Pseudocode showing the core training logic of CLIP

            image_features

            =

            image_encoder

            (

            image_batch

            )

             

            Image feature extraction

            text_features

            =

            text_encoder

            (

            text_batch

            )

               

            Text feature extraction

            Compute the similarity matrix

            logits

            =

            torch.

            matmul

            (

            image_features

            ,

            text_features.

            T

            )

            labels

            =

            torch.

            arange

            (

            batch_size

            )

             

            The diagonal represents positive samples

            Symmetric contrastive loss

            loss_img

            =

            cross_entropy

            (

            logits

            ,

            labels

            )

            loss_txt

            =

            cross_entropy

            (

            logits.

            T

            ,

            labels

            )

            total_loss

            =

            (

            loss_img + loss_txt

            )

            /

            2

            Application scenarios

            1. Zero-shot image classification: Classify new categories without fine-tuning
            2. Image-text retrieval: Enable efficient text-to-image or image-to-text search
            3. Content moderation: Identify image content that does not match the text description

            DALL-E: The magic of text-to-image generation

            Basic concepts

            DALL-E is a text-to-image generation model developed by OpenAI, capable of generating high-quality images from natural language descriptions.

            Technical features

            1. Two-stage training:

            2. Stage 1: A discrete variational autoencoder (dVAE) compresses images into visual tokens
            3. Stage 2: An autoregressive Transformer learns the mapping from text to visual tokens
            4. - Stage 1: A discrete variational autoencoder (dVAE) compresses images into visual tokens - Stage 2: An autoregressive Transformer learns the mapping from text to visual tokens
              1. Key innovations:

              2. Treat image generation as a sequence prediction problem
              3. Use a Transformer model with 12-billion parameters
              4. - Treat image generation as a sequence prediction problem - Use a Transformer model with 12-billion parameters

                Example of the generation process

                Example

                Pseudocode showing the DALL-E generation pipeline

                text

                =

                "A Shiba Inu in a spacesuit playing video games on a space station"

                text_tokens

                =

                tokenizer

                (

                text

                )

                 

                Text encoding

                image_tokens

                =

                transformer.

                generate

                (

                text_tokens

                )

                 

                Generate visual tokens

                image

                =

                dvae.

                decode

                (

                image_tokens

                )

                 

                Decode into an image

                Model evolution

                Version Major improvements Generation capability
                DALL-E 1 Base architecture 256x256 resolution
                DALL-E 2 Diffusion model 1024x1024 resolution, more accurate
                DALL-E 3 Integration with ChatGPT More sophisticated prompt understanding

                Other important multimodal models

                ALIGN (Google)

                Flamingo (DeepMind)

                BEiT-3 (Microsoft)


                Application challenges of multimodal models

                1. Data requirements: Require massive high-quality multimodal aligned data
                2. Computational cost: Training these models requires enormous computing resources
                3. Evaluation difficulties: Lack of unified evaluation standards for multimodal tasks
                4. Bias issues: May amplify social biases in training data

                Hands-on exercise: Zero-shot classification with CLIP

                Example

                import

                clip

                import

                torch

                from

                PIL

                import

                Image

                Load the model and preprocessing

                device

                =

                "cuda"

                if

                torch.

                cuda

                .

                is_available

                (

                )

                else

                "cpu"

                model

                ,

                preprocess

                =

                clip.

                load

                (

                "ViT-B/32"

                ,

                device

                =

                device

                )

                Prepare the input

                image

                =

                preprocess

                (

                Image.

                open

                (

                "dog.jpg"

                )

                )

                .

                unsqueeze

                (

                0

                )

                .

                to

                (

                device

                )

                text_inputs

                =

                clip.

                tokenize

                (

                [

                "a dog"

                ,

                "a cat"

                ,

                "a bird"

                ]

                )

                .

                to

                (

                device

                )

                Compute features

                with

                torch.

                no_grad

                (

                )

                :

                    image_features

                =

                model.

                encode_image

                (

                image

                )

                    text_features

                =

                model.

                encode_text

                (

                text_inputs

                )

                   

                Compute similarity

                logits

                =

                (

                image_features

                @

                text_features.

                T

                )

                .

                softmax

                (

                dim

                =

                1

                )

                print

                (

                "Prediction probability:"

                ,

                logits.

                cpu

                (

                )

                .

                numpy

                (

                )

                )


                Future directions

                1. More efficient architectures: Reduce computational cost and increase inference speed
                2. Fusion of more modalities: Add more modalities such as audio and video
                3. Causal understanding capability: Enhance the model's deep understanding of multimodal content
                4. Controllable generation: Improve precise control and editability of generated content

                Multimodal pretrained models are reshaping human-computer interaction; from CLIP's cross-modal understanding to DALL-E's creative generation, these technologies open up entirely new possibilities for AI applications.