GithubHelp home page GithubHelp logo

Comments (2)

ebsmothers avatar ebsmothers commented on August 22, 2024 1

Hi @XinhaoMei, thanks for using the repo! For ALBEF we only provide the fine-tuning code, so if you want to use the starting checkpoints for pretraining you just need to remap the state dict keys. We often don't keep this code in the repo as it is a bit manual, we instead do it once and then provide our remapped checkpoints (for ALBEF you can find them here, though these ones are already pretrained). Below is an example of how you can do this for pretrained ViT. Lmk if it makes sense, happy to also provide something similar for the BERT mapping.

import torch
from torchmultimodal.models.albef.image_encoder import ALBEFVisionEncoder

# Function to convert pretrained ViT state dict into TorchMultimodal state dict
def map_vit_state_dict(vit_ckpt):
    mapped_state_dict = {}
    encoder_layer_mapping = {
        'norm1': 'ln_1',
        'norm2': 'ln_2',
        'attn.qkv.weight': 'self_attention.in_proj_weight',
        'attn.qkv.bias': 'self_attention.in_proj_bias',
        'attn.proj': 'self_attention.out_proj',
        'norm2': 'ln_2',
        'mlp.fc1': 'mlp.0',
        'mlp.fc2': 'mlp.3'
    }

    misc_mapping = {
        'cls_token': 'encoder.class_token',
        'pos_embed': 'encoder.encoder.pos_embedding',
        'patch_embed.proj': 'encoder.conv_proj',
        'norm': 'encoder.encoder.ln'
    }

    for k, v in vit_ckpt['model'].items():
        if 'blocks' in k:
            layer_num = k.split('.')[1]
            new_k = k.replace('blocks.', 'encoder.encoder.layers.encoder_layer_')
            param_name = [x for x in encoder_layer_mapping.keys() if x in new_k][0]
            new_k = new_k.replace(param_name, encoder_layer_mapping[param_name])
            mapped_state_dict[new_k] = v
        else:
            param_names = [x for x in misc_mapping.keys() if x in k]
            if len(param_names) != 0:
                param_name = param_names[0]
                new_k = k.replace(param_name, misc_mapping[param_name])
                mapped_state_dict[new_k] = v
    return mapped_state_dict

# Load ALBEF vision encoder from TorchMultimodal
tmm_albef_vision_encoder = ALBEFVisionEncoder(
    hidden_size=768,
    image_size=224,
    patch_size=16,
    num_hidden_layers=12,
    num_attention_heads=12,
    mlp_dim=3072,
    dropout=0.0,
    attention_dropout=0.0,
    layer_norm_eps=1e-6
)

# Load pretrained ViT from ALBEF pretraining
vit_ckpt = torch.hub.load_state_dict_from_url(
    url="https://dl.fbaipublicfiles.com/deit/deit_base_patch16_224-b5f2ef4d.pth",
    map_location="cpu", check_hash=True
)

# Map state dict and load into TorchMultimodal ViT class
mapped_state_dict = map_vit_state_dict(vit_ckpt)
tmm_albef_vision_encoder.load_state_dict(mapped_state_dict)

from multimodal.

XinhaoMei avatar XinhaoMei commented on August 22, 2024

Thank you for your quick reply!
Understood, I will have a try.

Many thanks!

from multimodal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.