Spaces:
Running
Running
File size: 12,512 Bytes
650c5f6 |
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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# ------------------------------------------------------------------------
# Modified from OFA (https://github.com/OFA-Sys/OFA)
# Copyright 2022 The OFA-Sys Team.
# All rights reserved.
# This source code is licensed under the Apache 2.0 license
# found in the LICENSE file in the root directory.
# ------------------------------------------------------------------------
# Modifications Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
from dataclasses import dataclass, field
import logging
from typing import Optional
import os
import math
import numpy as np
import torch
from fairseq import metrics
from fairseq.tasks import register_task
from tasks.base_task import BaseTask, BaseConfig, load_bert_pretrained_weights
from data.refcoco_dataset import RefcocoDataset
from data.file_dataset import FileDataset
logger = logging.getLogger(__name__)
COO = 0 # <COO> class
SEP = 1 # <SEP> class
EOS = 2 # <EOS> class
bos_index = 0 # index for bos token
sep_index = 3 # index for separator token
@dataclass
class RefcocoConfig(BaseConfig):
eval_acc: bool = field(
default=False, metadata={"help": "evaluation with accuracy"}
)
eval_args: Optional[str] = field(
default='{}',
metadata={
"help": 'generation args, e.g., \'{"beam": 4, "lenpen": 0.6}\', as JSON string'
},
)
uses_ema: Optional[bool] = field(
default=False,
metadata={"help": "whether to use ema"},
)
eval_print_samples: bool = field(
default=False, metadata={"help": "print sample generations during validation"}
)
max_image_size: int = field(
default=512, metadata={"help": "max image size for normalization"}
)
scst: bool = field(
default=False, metadata={"help": "Self-critical sequence training"}
)
scst_args: str = field(
default='{}',
metadata={
"help": 'generation args for Self-critical sequence training, as JSON string'
},
)
@register_task("refcoco", dataclass=RefcocoConfig)
class RefcocoTask(BaseTask):
def __init__(self, cfg: RefcocoConfig, src_dict, tgt_dict):
super().__init__(cfg, src_dict, tgt_dict)
def load_dataset(self, split, epoch=1, combine=False, **kwargs):
paths = self.cfg.data.split(',')
assert len(paths) > 0
if split == 'train':
file_path = paths[(epoch - 1) % (len(paths) - 1)]
else:
file_path = paths[-1]
dataset = FileDataset(file_path, self.cfg.selected_cols)
self.datasets[split] = RefcocoDataset(
split,
dataset,
self.bpe,
self.src_dict,
self.tgt_dict,
max_src_length=self.cfg.max_src_length,
max_tgt_length=self.cfg.max_tgt_length,
patch_image_size=self.cfg.patch_image_size,
imagenet_default_mean_and_std=self.cfg.imagenet_default_mean_and_std,
num_bins=self.cfg.num_bins,
max_image_size=self.cfg.max_image_size
)
def build_model(self, cfg):
model = super().build_model(cfg)
bert_path = "../../pretrained_weights/bert-base-uncased-pytorch_model.bin"
if os.path.exists(bert_path):
load_bert_pretrained_weights(model.encoder.bert, bert_path)
if cfg._name == 'polyformer_b':
swin_path = "../../pretrained_weights/swin_base_patch4_window12_384_22k.pth"
else:
swin_path = "../../pretrained_weights/swin_large_patch4_window12_384_22k.pth"
if os.path.exists(swin_path):
model.encoder.embed_images.init_weights(pretrained=swin_path)
return model
def _calculate_ap_score(self, hyps, refs, thresh=0.5):
interacts = torch.cat(
[torch.where(hyps[:, :2] < refs[:, :2], refs[:, :2], hyps[:, :2]),
torch.where(hyps[:, 2:] < refs[:, 2:], hyps[:, 2:], refs[:, 2:])],
dim=1
)
area_predictions = (hyps[:, 2] - hyps[:, 0]) * (hyps[:, 3] - hyps[:, 1])
area_targets = (refs[:, 2] - refs[:, 0]) * (refs[:, 3] - refs[:, 1])
interacts_w = interacts[:, 2] - interacts[:, 0]
interacts_h = interacts[:, 3] - interacts[:, 1]
area_interacts = interacts_w * interacts_h
ious = area_interacts / (area_predictions + area_targets - area_interacts + 1e-6)
return ((ious >= thresh) & (interacts_w > 0) & (interacts_h > 0)).float()
def valid_step(self, sample, model, criterion):
loss, sample_size, logging_output = criterion(model, sample)
model.eval()
if self.cfg.eval_acc:
hyps, refs = self._inference(sample, model)
scores = self._calculate_ap_score(hyps.float(), refs.float())
logging_output["_score_sum"] = scores.sum().item()
logging_output["_score_cnt"] = scores.size(0)
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["_score_sum"].sum / meters["_score_cnt"].sum
score = score if isinstance(score, float) else score.item()
return round(score, 4)
if sum_logs("_score_cnt") > 0:
metrics.log_scalar("_score_sum", sum_logs("_score_sum"))
metrics.log_scalar("_score_cnt", sum_logs("_score_cnt"))
metrics.log_derived("score", compute_score)
def _inference(self, sample, model):
hyps = self.inference_step(model, sample)
refs = sample['region_coords'].float()
hyps = hyps * self.cfg.max_image_size
hyps[:, ::2] /= sample['w_resize_ratios'].unsqueeze(1)
hyps[:, 1::2] /= sample['h_resize_ratios'].unsqueeze(1)
return hyps, refs
def inference_step(self, model, sample):
with torch.no_grad():
if isinstance(model, list):
model = model[0]
min_len = 6
max_len = 210
model.eval()
img = sample["net_input"]["patch_images"]
b = img.shape[0]
prev_output_token_11 = [[bos_index] for _ in range(b)]
prev_output_token_12 = [[bos_index] for _ in range(b)]
prev_output_token_21 = [[bos_index] for _ in range(b)]
prev_output_token_22 = [[bos_index] for _ in range(b)]
delta_x1 = [[0] for _ in range(b)]
delta_y1 = [[0] for _ in range(b)]
delta_x2 = [[1] for _ in range(b)]
delta_y2 = [[1] for _ in range(b)]
gen_out = [[] for _ in range(b)]
n_bins = self.cfg.num_bins
unfinish_flag = np.ones(b)
i = 0
encoder_out = model.encoder(
sample['net_input']['src_tokens'],
src_lengths=sample['net_input']['src_lengths'],
att_masks=sample['net_input']['att_masks'],
patch_images=sample['net_input']['patch_images'],
patch_masks=sample['net_input']['patch_masks'],
token_embeddings=None,
return_all_hiddens=False,
sample_patch_num=None
)
while i < max_len and unfinish_flag.any():
prev_output_tokens_11_tensor = torch.tensor(np.array(prev_output_token_11)).to(img.device).long()
prev_output_tokens_12_tensor = torch.tensor(np.array(prev_output_token_12)).to(img.device).long()
prev_output_tokens_21_tensor = torch.tensor(np.array(prev_output_token_21)).to(img.device).long()
prev_output_tokens_22_tensor = torch.tensor(np.array(prev_output_token_22)).to(img.device).long()
delta_x1_tensor = torch.tensor(np.array(delta_x1)).to(img.device)
delta_x2_tensor = torch.tensor(np.array(delta_x2)).to(img.device)
delta_y1_tensor = torch.tensor(np.array(delta_y1)).to(img.device)
delta_y2_tensor = torch.tensor(np.array(delta_y2)).to(img.device)
net_output = model.decoder(
prev_output_tokens_11_tensor,
prev_output_tokens_12_tensor,
prev_output_tokens_21_tensor,
prev_output_tokens_22_tensor,
delta_x1_tensor,
delta_y1_tensor,
delta_x2_tensor,
delta_y2_tensor,
code_masks=None,
encoder_out=encoder_out,
features_only=False,
alignment_layer=None,
alignment_heads=None,
src_lengths=sample['net_input']['src_lengths'],
return_all_hiddens=False
)
cls_output = net_output[0]
cls_type = torch.argmax(cls_output, 2)
reg_output = net_output[1]
for j in range(b):
if unfinish_flag[j] == 1: # prediction is not finished
cls_j = cls_type[j, i].item()
if cls_j == COO or (cls_j == EOS and i < min_len):
output_j_x, output_j_y = reg_output[j, i].cpu().numpy()
output_j_x = min(output_j_x, 1)
output_j_y = min(output_j_y, 1)
gen_out[j].extend([output_j_x, output_j_y])
output_j_x = output_j_x * (n_bins - 1)
output_j_y = output_j_y * (n_bins - 1)
output_j_x_floor = math.floor(output_j_x)
output_j_y_floor = math.floor(output_j_y)
output_j_x_ceil = math.ceil(output_j_x)
output_j_y_ceil = math.ceil(output_j_y)
# tokenization
prev_output_token_11[j].append(output_j_x_floor * n_bins + output_j_y_floor + 4)
prev_output_token_12[j].append(output_j_x_floor * n_bins + output_j_y_ceil + 4)
prev_output_token_21[j].append(output_j_x_ceil * n_bins + output_j_y_floor + 4)
prev_output_token_22[j].append(output_j_x_ceil * n_bins + output_j_y_ceil + 4)
delta_x = output_j_x - output_j_x_floor
delta_y = output_j_y - output_j_y_floor
elif cls_j == SEP:
gen_out[j].append(2) # insert 2 indicating separator tokens
prev_output_token_11[j].append(sep_index)
prev_output_token_12[j].append(sep_index)
prev_output_token_21[j].append(sep_index)
prev_output_token_22[j].append(sep_index)
delta_x = 0
delta_y = 0
else: # eos is predicted and i >= min_len
unfinish_flag[j] = 0
gen_out[j].append(-1)
prev_output_token_11[j].append(2) # 2 is eos token
prev_output_token_12[j].append(2) # 2 is eos token
prev_output_token_21[j].append(2) # 2 is eos token
prev_output_token_22[j].append(2) # 2 is eos token
delta_x = 0
delta_y = 0
else: # prediction is finished
gen_out[j].append(-1)
prev_output_token_11[j].append(1) # 1 is padding token
prev_output_token_12[j].append(1)
prev_output_token_21[j].append(1)
prev_output_token_22[j].append(1)
delta_x = 0
delta_y = 0
delta_x1[j].append(delta_x)
delta_y1[j].append(delta_y)
delta_x2[j].append(1 - delta_x)
delta_y2[j].append(1 - delta_y)
i += 1
print("inference step: ", i)
return gen_out
|