Spaces:
Running
Running
File size: 9,625 Bytes
0d735a2 |
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
from dataclasses import dataclass, field
import json
import logging
import os
import math
import pickle
from typing import Optional
from argparse import Namespace
from data.file_dataset import FileDataset
import torch
from fairseq import metrics
from fairseq.tasks import register_task
from models import search
from data.mm_data.vqa_gen_dataset import VqaGenDataset
from data import data_utils
from tasks.ofa_task import OFAConfig, OFATask
from utils.trie import Trie
logger = logging.getLogger(__name__)
@dataclass
class VqaGenConfig(OFAConfig):
max_object_length: int = field(
default=30, metadata={"help": "the maximum object sequence length"}
)
ans2label_dict: Optional[str] = field(
default='{"no": 0, "yes":1}',
metadata={"help": 'answer to label dict'},
)
ans2label_file: Optional[str] = field(
default=None,
metadata={"help": "path to load ans2label file"},
)
add_object: bool = field(
default=False,
metadata={"help": "add object to encoder"},
)
valid_batch_size: int = field(
default=20,
metadata={"help": "valid batch size per step"},
)
prompt_type: Optional[str] = field(
default=None,
metadata={"help": "prompt_type"},
)
uses_ema: Optional[bool] = field(
default=False,
metadata={"help": "whether to use ema"},
)
@register_task("vqa_gen", dataclass=VqaGenConfig)
class VqaGenTask(OFATask):
def __init__(self, cfg: VqaGenConfig, src_dict, tgt_dict):
super().__init__(cfg, src_dict, tgt_dict)
self.ans2label_dict = None
if self.cfg.ans2label_file is not None:
self.ans2label_dict = pickle.load(open(self.cfg.ans2label_file, "rb"))
else:
self.ans2label_dict = json.loads(self.cfg.ans2label_dict)
self.uses_ema = self.cfg.uses_ema
def load_dataset(self, split, epoch=1, combine=False, **kwargs):
paths = self.cfg.data.split(',')
assert len(paths) > 0
if split == 'train':
table_path = paths[(epoch - 1) % (len(paths) - 1)]
else:
table_path = paths[-1]
dataset = FileDataset(table_path, self.cfg.selected_cols)
self.datasets[split] = VqaGenDataset(
split,
dataset,
self.bpe,
self.src_dict,
self.tgt_dict,
max_src_length=self.cfg.max_src_length,
max_object_length=self.cfg.max_object_length,
max_tgt_length=self.cfg.max_tgt_length,
patch_image_size=self.cfg.patch_image_size,
add_object=self.cfg.add_object,
constraint_trie=self.constraint_trie,
imagenet_default_mean_and_std=self.cfg.imagenet_default_mean_and_std,
prompt_type=self.cfg.prompt_type
)
def build_model(self, cfg):
model = super().build_model(cfg)
answer_item_list = []
self.index2ans = {}
self.constraint_trie = Trie(self.tgt_dict.eos())
for i, answer in enumerate(self.ans2label_dict.keys()):
answer_item = self.tgt_dict.encode_line(
line=self.bpe.encode(' ' + answer),
add_if_not_exist=False,
append_eos=False
).long()
answer_item_list.append(answer_item)
self.index2ans[i] = answer
self.constraint_trie.insert([self.tgt_dict.bos()] + answer_item.tolist() + [self.tgt_dict.eos()])
constraint_mask_list = []
for answer_item in answer_item_list:
constraint_mask = torch.zeros((len(answer_item)+1, len(self.tgt_dict))).bool()
for i in range(len(answer_item)+1):
constraint_prefix_token = [self.src_dict.bos()] + answer_item[:i].tolist()
constraint_nodes = self.constraint_trie.get_next_layer(constraint_prefix_token)
constraint_mask[i][constraint_nodes] = True
constraint_mask_list.append(constraint_mask)
self.valid_answers_list = []
self.valid_constraint_masks_list = []
for i in range(0, len(answer_item_list), self.cfg.valid_batch_size):
self.valid_answers_list += [answer_item_list[i:i+self.cfg.valid_batch_size]]
self.valid_constraint_masks_list += [constraint_mask_list[i:i+self.cfg.valid_batch_size]]
return model
def build_generator(
self, models, args, seq_gen_cls=None, extra_gen_cls_kwargs=None, prefix_allowed_tokens_fn=None,
):
seq_generator = super().build_generator(models, args, seq_gen_cls, extra_gen_cls_kwargs, prefix_allowed_tokens_fn)
seq_generator.constraint_trie = self.constraint_trie
return seq_generator
def valid_step(self, sample, model, criterion, **extra_kwargs):
loss, sample_size, logging_output = super().valid_step(sample, model, criterion)
if self.uses_ema:
assert 'ema_model' in extra_kwargs and extra_kwargs['ema_model'] is not None
if self.uses_ema:
eval_model = extra_kwargs['ema_model']
else:
eval_model = model
eval_model.eval()
with torch.no_grad():
encoder_out = eval_model.encoder(
sample["net_input"]["src_tokens"],
src_lengths=sample["net_input"]["src_lengths"],
patch_images=sample["net_input"]["patch_images"],
patch_masks=sample["net_input"]["patch_masks"]
)
device = sample["net_input"]["src_tokens"].device
eos_item = torch.tensor([self.src_dict.eos()])
pad = self.src_dict.pad()
valid_result = []
for valid_answers, valid_constraint_masks in zip(self.valid_answers_list, self.valid_constraint_masks_list):
valid_size = len(valid_answers)
valid_tgt_items = [
torch.cat([torch.tensor(decoder_prompt[1:]), valid_answer, eos_item])
for decoder_prompt in sample["decoder_prompts"] for valid_answer in valid_answers
]
valid_prev_items = [
torch.cat([torch.tensor(decoder_prompt), valid_answer])
for decoder_prompt in sample["decoder_prompts"] for valid_answer in valid_answers
]
valid_constraint_mask_items = [
torch.cat([torch.zeros(len(decoder_prompt)-1, valid_constraint_mask.size(1)).bool(), valid_constraint_mask], dim=0)
for decoder_prompt in sample["decoder_prompts"] for valid_constraint_mask in valid_constraint_masks
]
valid_tgt = data_utils.collate_tokens(valid_tgt_items, pad_idx=pad, left_pad=False).to(device)
valid_prev_output = data_utils.collate_tokens(valid_prev_items, pad_idx=pad, left_pad=False).to(device)
valid_constraint_masks = data_utils.collate_tokens(valid_constraint_mask_items, pad_idx=pad, left_pad=False).to(device)
new_encoder_out = {}
new_encoder_out["encoder_out"] = [
encoder_out["encoder_out"][0].repeat_interleave(valid_size, dim=1)
]
new_encoder_out["encoder_padding_mask"] = [
encoder_out["encoder_padding_mask"][0].repeat_interleave(valid_size, dim=0)
]
new_encoder_out["position_embeddings"] = [
encoder_out["position_embeddings"][0].repeat_interleave(valid_size, dim=0)
]
decoder_out = eval_model.decoder(valid_prev_output, encoder_out=new_encoder_out)
decoder_out[0].masked_fill_(~valid_constraint_masks, -math.inf)
lprobs = eval_model.get_normalized_probs(decoder_out, log_probs=True)
scores = lprobs.gather(dim=-1, index=valid_tgt.unsqueeze(-1)).squeeze(-1)
scores = scores.masked_fill(valid_tgt.eq(self.tgt_dict.pad()), 0)
scores = scores.masked_fill((~valid_constraint_masks).all(2), 0)
scores = scores.sum(1)
scores = scores.view(-1, valid_size)
valid_result.append(scores)
valid_result = torch.cat(valid_result, dim=-1)
predicts = valid_result.argmax(1).tolist()
hyps = [self.index2ans[predict_index] for predict_index in predicts]
scores = [ref_dict.get(hyp, 0) for ref_dict, hyp in zip(sample['ref_dict'], hyps)]
logging_output["_vqa_score_sum"] = sum(scores)
logging_output["_vqa_cnt"] = len(scores)
return loss, sample_size, logging_output
def reduce_metrics(self, logging_outputs, criterion):
super().reduce_metrics(logging_outputs, criterion)
def sum_logs(key):
import torch
result = sum(log.get(key, 0) for log in logging_outputs)
if torch.is_tensor(result):
result = result.cpu()
return result
def compute_score(meters):
score = meters["_vqa_score_sum"].sum / meters["_vqa_cnt"].sum
score = score if isinstance(score, float) else score.item()
return round(score, 4)
if sum_logs("_vqa_cnt") > 0:
metrics.log_scalar("_vqa_score_sum", sum_logs("_vqa_score_sum"))
metrics.log_scalar("_vqa_cnt", sum_logs("_vqa_cnt"))
metrics.log_derived("vqa_score", compute_score) |