Spaces:
Sleeping
Sleeping
File size: 7,091 Bytes
e84842d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
"""
Copyright (c) 2022, salesforce.com, inc.
All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""
import os
import torch
import torch.nn.functional as F
from lavis.common.dist_utils import download_cached_file
from lavis.common.registry import registry
from lavis.common.utils import get_abs_path, is_url
from lavis.models.base_model import MomentumDistilationMixin
from lavis.models.blip_models.blip import BlipBase
from lavis.models.blip_models.blip_outputs import BlipIntermediateOutput, BlipOutput
from lavis.models.blip_models.nlvr_encoder import BertModel
from lavis.models.vit import VisionTransformerEncoder, interpolate_pos_embed
from torch import nn
from transformers import BertConfig
@registry.register_model("blip_nlvr")
class BlipNLVR(BlipBase, MomentumDistilationMixin):
"""
Class for BLIP NLVR model.
Supported model types:
- base: model with pre-trained BLIP weights, used as initialization for fine-tuning.
- nlvr: finetuned model on NLVR2 dataset.
Usage:
>>> from lavis.models import load_model
>>> model = load_model("blip_nlvr", "nlvr")
"""
PRETRAINED_MODEL_CONFIG_DICT = {
"nlvr": "configs/models/blip_nlvr.yaml",
}
def __init__(self, image_encoder, text_encoder, num_classes):
super().__init__()
self.tokenizer = self.init_tokenizer()
self.visual_encoder = image_encoder
self.text_encoder = text_encoder
hidden_size = text_encoder.config.hidden_size
self.cls_head = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, num_classes),
)
def forward(self, samples, is_train=True):
"""
Forward function for training and evaluation.
Args:
samples (dict): a dict of input samples, which contains the following keys:
- image0 (torch.Tensor): input image 0, shape (batch_size, 3, H, W), default H=384, W=384.
- image1 (torch.Tensor): input image 1, shape (batch_size, 3, H, W), default H=384, W=384.
- text_input (list): list of strings, each string is a natural language sentence.
- label (torch.LongTensor): ground truth label with shape (batch_size,).
is_train (bool): whether the model is in training mode.
If True, the model will return the loss;
If False, the model will return the prediction.
Examples:
>>> import torch
>>> from lavis.models import load_model
>>> model = load_model("blip_nlvr", "nlvr")
>>> samples = {
... "image0": torch.randn(2, 3, 384, 384),
... "image1": torch.randn(2, 3, 384, 384),
... "text_input": ["there is a ferret in tall grass", "there are lips in one of the images"],
... "label": torch.tensor([0, 1]),
... }
>>> output = model(samples)
>>> output.keys()
odict_keys(['intermediate_output', 'loss'])
"""
text = samples["text_input"]
text = self.tokenizer(text, padding="longest", return_tensors="pt").to(
self.device
)
text.input_ids[:, 0] = self.tokenizer.enc_token_id
targets = samples["label"]
image0 = samples["image0"]
image1 = samples["image1"]
images = torch.cat([image0, image1], dim=0)
image_embeds = self.visual_encoder.forward_features(images)
image_atts = torch.ones(image_embeds.size()[:-1], dtype=torch.long).to(
self.device
)
image0_embeds, image1_embeds = torch.split(image_embeds, targets.size(0))
encoder_output = self.text_encoder(
text.input_ids,
attention_mask=text.attention_mask,
encoder_hidden_states=[image0_embeds, image1_embeds],
encoder_attention_mask=[
image_atts[: image0_embeds.size(0)],
image_atts[image0_embeds.size(0) :],
],
return_dict=True,
)
prediction = self.cls_head(encoder_output.last_hidden_state[:, 0, :])
if is_train:
loss = F.cross_entropy(prediction, targets)
# return {"loss": loss}
return BlipOutput(
loss=loss,
intermediate_output=BlipIntermediateOutput(
image_embeds=torch.stack([image0_embeds, image1_embeds], dim=0),
encoder_output=encoder_output,
),
)
else:
return {"predictions": prediction, "targets": targets}
def predict(self, samples):
output = self.forward(samples, is_train=False)
return output
@classmethod
def from_config(cls, cfg=None):
image_encoder = VisionTransformerEncoder.from_config(cfg)
# text encoder + multimodal encoder
bert_config = BertConfig.from_json_file(get_abs_path(cfg["med_config_path"]))
text_encoder = BertModel(config=bert_config, add_pooling_layer=False)
num_classes = cfg.get("num_classes", 3)
assert num_classes > 1, "Invalid number of classes provided, found {}".format(
num_classes
)
model = cls(
image_encoder=image_encoder,
text_encoder=text_encoder,
num_classes=num_classes,
)
model.load_checkpoint_from_config(cfg)
return model
def load_from_pretrained(self, url_or_filename):
if is_url(url_or_filename):
cached_file = download_cached_file(
url_or_filename, check_hash=False, progress=True
)
checkpoint = torch.load(cached_file, map_location="cpu")
elif os.path.isfile(url_or_filename):
checkpoint = torch.load(url_or_filename, map_location="cpu")
else:
raise RuntimeError("checkpoint url or path is invalid")
state_dict = checkpoint["model"]
state_dict["visual_encoder.pos_embed"] = interpolate_pos_embed(
state_dict["visual_encoder.pos_embed"], self.visual_encoder
)
for key in list(state_dict.keys()):
if "crossattention.self." in key:
new_key0 = key.replace("self", "self0")
new_key1 = key.replace("self", "self1")
state_dict[new_key0] = state_dict[key]
state_dict[new_key1] = state_dict[key]
elif "crossattention.output.dense." in key:
new_key0 = key.replace("dense", "dense0")
new_key1 = key.replace("dense", "dense1")
state_dict[new_key0] = state_dict[key]
state_dict[new_key1] = state_dict[key]
msg = self.load_state_dict(state_dict, strict=False)
print("load checkpoint from %s" % url_or_filename)
print(f"missing keys {msg.missing_keys}")
return msg
|