-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from ParadoxZW/master
MMNasNet for VQA-v2 is supported.
- Loading branch information
Showing
13 changed files
with
588 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Network | ||
MODEL_USE: mmnasnet | ||
ARCH: { | ||
enc: [SA, SA, SA, SA, FFN, FFN, FFN, FFN, SA, FFN, FFN, FFN], | ||
dec: [GA, GA, FFN, FFN, GA, FFN, RSA, GA, FFN, GA, RSA, FFN, RSA, SA, FFN, RSA, GA, FFN] | ||
} | ||
HIDDEN_SIZE: 1024 | ||
REL_HBASE: 128 | ||
REL_SIZE: 64 | ||
MULTI_HEAD: 8 | ||
DROPOUT_R: 0.1 | ||
FLAT_MLP_SIZE: 1024 | ||
FLAT_GLIMPSES: 1 | ||
FLAT_OUT_SIZE: 2048 | ||
|
||
# Execution | ||
BATCH_SIZE: 64 | ||
LR_BASE: 0.00007 # 5e-5 for train+val+vg->test | ||
LR_DECAY_R: 0.2 | ||
LR_DECAY_LIST: [10, 12] | ||
WARMUP_EPOCH: 3 | ||
MAX_EPOCH: 13 | ||
GRAD_NORM_CLIP: 1.0 | ||
GRAD_ACCU_STEPS: 1 | ||
LOSS_FUNC: bce | ||
LOSS_REDUCTION: sum | ||
OPT: Adam | ||
OPT_PARAMS: {betas: '(0.9, 0.98)', eps: '1e-9'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Network | ||
MODEL_USE: mmnasnet | ||
ARCH: { | ||
enc: [SA, SA, SA, SA, FFN, FFN, FFN, FFN, SA, FFN, FFN, FFN], | ||
dec: [GA, GA, FFN, FFN, GA, FFN, RSA, GA, FFN, GA, RSA, FFN, RSA, SA, FFN, RSA, GA, FFN] | ||
} | ||
HIDDEN_SIZE: 512 | ||
REL_HBASE: 64 | ||
REL_SIZE: 64 | ||
MULTI_HEAD: 8 | ||
DROPOUT_R: 0.1 | ||
FLAT_MLP_SIZE: 512 | ||
FLAT_GLIMPSES: 1 | ||
FLAT_OUT_SIZE: 1024 | ||
|
||
# Execution | ||
BATCH_SIZE: 64 | ||
LR_BASE: 0.00012 # 1e-4 for train+val+vg->test | ||
LR_DECAY_R: 0.2 | ||
LR_DECAY_LIST: [10, 12] | ||
WARMUP_EPOCH: 3 | ||
MAX_EPOCH: 13 | ||
GRAD_NORM_CLIP: 1.0 | ||
GRAD_ACCU_STEPS: 1 | ||
LOSS_FUNC: bce | ||
LOSS_REDUCTION: sum | ||
OPT: Adam | ||
OPT_PARAMS: {betas: '(0.9, 0.98)', eps: '1e-9'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# -------------------------------------------------------- | ||
# OpenVQA | ||
# Written by Zhenwei Shao https://github.com/ParadoxZW | ||
# -------------------------------------------------------- | ||
|
||
import torch.nn as nn | ||
import torch | ||
from openvqa.core.base_dataset import BaseAdapter | ||
from openvqa.utils.make_mask import make_mask | ||
|
||
|
||
class Adapter(BaseAdapter): | ||
def __init__(self, __C): | ||
super(Adapter, self).__init__(__C) | ||
self.__C = __C | ||
|
||
|
||
def relation_embedding(self, f_g): | ||
x_min, y_min, x_max, y_max = torch.chunk(f_g, 4, dim=2) # [bs, n_obj, 1] | ||
|
||
cx = (x_min + x_max) * 0.5 # [bs, n_obj, 1] | ||
cy = (y_min + y_max) * 0.5 # [bs, n_obj, 1] | ||
w = (x_max - x_min) + 1. # [bs, n_obj, 1] | ||
h = (y_max - y_min) + 1. # [bs, n_obj, 1] | ||
|
||
delta_x = cx - cx.transpose(-1, -2) | ||
delta_x = torch.clamp(torch.abs(delta_x / w), min=1e-3) | ||
delta_x = torch.log(delta_x) # [bs, n_obj, n_obj] | ||
|
||
delta_y = cy - cy.transpose(-1, -2) | ||
delta_y = torch.clamp(torch.abs(delta_y / h), min=1e-3) | ||
delta_y = torch.log(delta_y) # [bs, n_obj, n_obj] | ||
|
||
delta_w = torch.log(w / w.transpose(-1, -2)) # [bs, n_obj, n_obj] | ||
delta_h = torch.log(h / h.transpose(-1, -2)) # [bs, n_obj, n_obj] | ||
size = delta_h.size() | ||
|
||
delta_x = delta_x.view(size[0], size[1], size[2], 1) | ||
delta_y = delta_y.view(size[0], size[1], size[2], 1) | ||
delta_w = delta_w.view(size[0], size[1], size[2], 1) | ||
delta_h = delta_h.view(size[0], size[1], size[2], 1) # [bs, n_obj, n_obj, 1] | ||
position_mat = torch.cat( | ||
(delta_x, delta_y, delta_w, delta_h), -1) # [bs, n_obj, n_obj, 4] | ||
|
||
return position_mat | ||
|
||
def vqa_init(self, __C): | ||
imgfeat_linear_size = __C.FEAT_SIZE['vqa']['FRCN_FEAT_SIZE'][1] | ||
if __C.USE_BBOX_FEAT: | ||
self.bbox_linear = nn.Linear(5, __C.BBOXFEAT_EMB_SIZE) | ||
imgfeat_linear_size += __C.BBOXFEAT_EMB_SIZE | ||
self.frcn_linear = nn.Linear(imgfeat_linear_size, __C.HIDDEN_SIZE) | ||
|
||
|
||
def gqa_init(self, __C): | ||
imgfeat_linear_size = __C.FEAT_SIZE['gqa']['FRCN_FEAT_SIZE'][1] | ||
if __C.USE_BBOX_FEAT: | ||
self.bbox_linear = nn.Linear(5, __C.BBOXFEAT_EMB_SIZE) | ||
imgfeat_linear_size += __C.BBOXFEAT_EMB_SIZE | ||
self.frcn_linear = nn.Linear(imgfeat_linear_size, __C.HIDDEN_SIZE) | ||
|
||
if __C.USE_AUX_FEAT: | ||
self.grid_linear = nn.Linear(__C.FEAT_SIZE['gqa']['GRID_FEAT_SIZE'][1], __C.HIDDEN_SIZE) | ||
|
||
|
||
def clevr_init(self, __C): | ||
self.grid_linear = nn.Linear(__C.FEAT_SIZE['clevr']['GRID_FEAT_SIZE'][1], __C.HIDDEN_SIZE) | ||
|
||
|
||
def vqa_forward(self, feat_dict): | ||
frcn_feat = feat_dict['FRCN_FEAT'] | ||
bbox_feat = feat_dict['BBOX_FEAT'] | ||
|
||
img_feat_mask = make_mask(frcn_feat) | ||
|
||
if self.__C.USE_BBOX_FEAT: | ||
bbox_feat = self.bbox_proc(bbox_feat) | ||
bbox_feat = self.bbox_linear(bbox_feat) | ||
frcn_feat = torch.cat((frcn_feat, bbox_feat), dim=-1) | ||
img_feat = self.frcn_linear(frcn_feat) | ||
rel_embed = self.relation_embedding(bbox_feat) | ||
|
||
return img_feat, rel_embed, img_feat_mask | ||
|
||
|
||
def gqa_forward(self, feat_dict): | ||
frcn_feat = feat_dict['FRCN_FEAT'] | ||
bbox_feat = feat_dict['BBOX_FEAT'] | ||
grid_feat = feat_dict['GRID_FEAT'] | ||
|
||
img_feat_mask = make_mask(frcn_feat) | ||
|
||
if self.__C.USE_BBOX_FEAT: | ||
bbox_feat = self.bbox_linear(bbox_feat) | ||
frcn_feat = torch.cat((frcn_feat, bbox_feat), dim=-1) | ||
img_feat = self.frcn_linear(frcn_feat) | ||
|
||
if self.__C.USE_AUX_FEAT: | ||
grid_feat_mask = make_mask(grid_feat) | ||
img_feat_mask = torch.cat((img_feat_mask, grid_feat_mask), dim=-1) | ||
grid_feat = self.grid_linear(grid_feat) | ||
img_feat = torch.cat((img_feat, grid_feat), dim=1) | ||
|
||
rel_embed = self.relation_embedding(bbox_feat) | ||
|
||
return img_feat, rel_embed, img_feat_mask | ||
|
||
|
||
def clevr_forward(self, feat_dict): | ||
grid_feat = feat_dict['GRID_FEAT'] | ||
|
||
img_feat_mask = make_mask(grid_feat) | ||
img_feat = self.grid_linear(grid_feat) | ||
|
||
rel_embed = self.relation_embedding(bbox_feat) | ||
|
||
return img_feat, rel_embed, img_feat_mask | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# -------------------------------------------------------- | ||
# OpenVQA | ||
# Written by Zhenwei Shao https://github.com/ParadoxZW | ||
# -------------------------------------------------------- | ||
|
||
from openvqa.core.base_cfgs import BaseCfgs | ||
|
||
|
||
class Cfgs(BaseCfgs): | ||
def __init__(self): | ||
super(Cfgs, self).__init__() | ||
|
||
self.ARCH = { | ||
'enc': ['SA', 'SA', 'SA', 'SA', 'FFN', 'FFN', 'FFN', 'FFN', 'SA', 'FFN', 'FFN', 'FFN'], | ||
'dec': ['GA', 'GA', 'FFN', 'FFN', 'GA', 'FFN', 'RSA', 'GA', 'FFN', 'GA', 'RSA', 'FFN', 'RSA', 'SA', 'FFN', 'RSA', 'GA', 'FFN'] | ||
} | ||
self.HIDDEN_SIZE = 512 | ||
self.BBOXFEAT_EMB_SIZE = 2048 | ||
self.FF_SIZE = 2048 | ||
self.MULTI_HEAD = 8 | ||
self.DROPOUT_R = 0.1 | ||
self.FLAT_MLP_SIZE = 512 | ||
self.FLAT_GLIMPSES = 1 | ||
self.FLAT_OUT_SIZE = 1024 | ||
self.USE_AUX_FEAT = False | ||
self.USE_BBOX_FEAT = False | ||
self.REL_HBASE = 64 | ||
self.REL_SIZE = 64 |
Oops, something went wrong.