forked from jiasenlu/vilbert_beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_cider.py
executable file
·320 lines (283 loc) · 11 KB
/
eval_cider.py
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import argparse
import json
import logging
import os
import random
from io import open
import numpy as np
from tensorboardX import SummaryWriter
from tqdm import tqdm
from bisect import bisect
import yaml
from easydict import EasyDict as edict
import pdb
import sys
import torch
import torch.nn.functional as F
import torch.nn as nn
from pytorch_pretrained_bert.optimization import WarmupLinearSchedule
# from parallel.parallel import DataParallelModel, DataParallelCriterion
from vilbert.task_utils import LoadDatasets, LoadLosses, ForwardModelsTrain, ForwardModelsVal
from vilbert.optimization import BertAdam, Adam, Adamax
from torch.optim.lr_scheduler import LambdaLR, ReduceLROnPlateau
import vilbert.utils as utils
import torch.distributed as dist
from vilbert.datasets.retreival_dataset import CiderDataset
from torch.utils.data import random_split
import numpy as np
from pytorch_pretrained_bert.tokenization import BertTokenizer
from torch.utils.data import DataLoader
# Reproducibility
np.random.seed(42)
torch.manual_seed(42)
torch.cuda.manual_seed_all(42)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
writer = SummaryWriter()
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--bert_model",
default="bert-base-uncased",
type=str,
help="Bert pre-trained model selected in the list: bert-base-uncased, "
"bert-large-uncased, bert-base-cased, bert-base-multilingual, bert-base-chinese.",
)
parser.add_argument(
"--from_pretrained",
default="bert-base-uncased",
type=str,
help="Bert pre-trained model selected in the list: bert-base-uncased, "
"bert-large-uncased, bert-base-cased, bert-base-multilingual, bert-base-chinese.",
)
parser.add_argument(
"--output_dir",
default="",
type=str,
help="The output directory where the model checkpoints will be written.",
)
parser.add_argument(
"--config_file",
default="config/bert_config.json",
type=str,
help="The config file which specified the model details.",
)
parser.add_argument(
"--learning_rate", default=2e-5, type=float, help="The initial learning rate for Adam."
)
parser.add_argument(
"--num_train_epochs",
default=20,
type=int,
help="Total number of training epochs to perform.",
)
parser.add_argument(
"--warmup_proportion",
default=0.1,
type=float,
help="Proportion of training to perform linear learning rate warmup for. "
"E.g., 0.1 = 10%% of training.",
)
parser.add_argument(
"--no_cuda", action="store_true", help="Whether not to use CUDA when available"
)
parser.add_argument(
"--do_lower_case",
default=True,
type=bool,
help="Whether to lower case the input text. True for uncased models, False for cased models.",
)
parser.add_argument(
"--local_rank", type=int, default=-1, help="local_rank for distributed training on gpus"
)
parser.add_argument("--seed", type=int, default=0, help="random seed for initialization")
parser.add_argument(
"--gradient_accumulation_steps",
type=int,
default=1,
help="Number of updates steps to accumualte before performing a backward/update pass.",
)
parser.add_argument(
"--fp16",
action="store_true",
help="Whether to use 16-bit float precision instead of 32-bit",
)
parser.add_argument(
"--loss_scale",
type=float,
default=0,
help="Loss scaling to improve fp16 numeric stability. Only used when fp16 set to True.\n"
"0 (default value): dynamic loss scaling.\n"
"Positive power of 2: static loss scaling value.\n",
)
parser.add_argument(
"--num_workers", type=int, default=16, help="Number of workers in the dataloader."
)
parser.add_argument(
"--save_name",
default='',
type=str,
help="save name for training.",
)
parser.add_argument(
"--use_chunk", default=0, type=float, help="whether use chunck for parallel training."
)
parser.add_argument(
"--in_memory", default=False, type=bool, help="whether use chunck for parallel training."
)
parser.add_argument(
"--optimizer", default='BertAdam', type=str, help="whether use chunck for parallel training."
)
parser.add_argument(
"--tasks", default='', type=str, help="1-2-3... training task separate by -"
)
parser.add_argument(
"--freeze", default = -1, type=int,
help="till which layer of textual stream of vilbert need to fixed."
)
parser.add_argument(
"--vision_scratch", action="store_true", help="whether pre-trained the image or not."
)
parser.add_argument(
"--evaluation_interval", default=1, type=int, help="evaluate very n epoch."
)
parser.add_argument(
"--lr_scheduler", default='mannul', type=str, help="whether use learning rate scheduler."
)
parser.add_argument(
"--baseline", action="store_true", help="whether use single stream baseline."
)
parser.add_argument(
"--compact", action="store_true", help="whether use compact vilbert model."
)
parser.add_argument(
"--captions_path", default='', type=str, help="1-2-3... training task separate by -"
)
parser.add_argument(
"--cider_path", default='', type=str, help="1-2-3... training task separate by -"
)
parser.add_argument(
"--tsv_path", default='', type=str, help="1-2-3... training task separate by -"
)
parser.add_argument(
"--out_path", default='', type=str, help="1-2-3... training task separate by -"
)
args = parser.parse_args()
assert len(args.output_dir) > 0
with open('vlbert_tasks.yml', 'r') as f:
task_cfg = edict(yaml.load(f))
if args.baseline:
from pytorch_pretrained_bert.modeling import BertConfig
from vilbert.basebert import BaseBertForVLTasks
elif args.compact:
from vilbert.vilbert_compact import BertConfig
from vilbert.vilbert_compact import VILBertForVLTasks
else:
from vilbert.vilbert import BertConfig
from vilbert.vilbert import VILBertForVLTasks
if args.save_name:
prefix = '-' + args.save_name
else:
prefix = ''
timeStamp = '_' + args.config_file.split('/')[1].split('.')[0] + prefix
savePath = os.path.join(args.output_dir, timeStamp)
bert_weight_name = json.load(open("config/" + args.bert_model + "_weight_name.json", "r"))
if args.local_rank == -1 or args.no_cuda:
device = torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu")
n_gpu = torch.cuda.device_count()
else:
torch.cuda.set_device(args.local_rank)
device = torch.device("cuda", args.local_rank)
n_gpu = 1
# Initializes the distributed backend which will take care of sychronizing nodes/GPUs
torch.distributed.init_process_group(backend="nccl")
logger.info(
"device: {} n_gpu: {}, distributed training: {}, 16-bits training: {}".format(
device, n_gpu, bool(args.local_rank != -1), args.fp16
)
)
default_gpu = False
if dist.is_available() and args.local_rank != -1:
rank = dist.get_rank()
if rank == 0:
default_gpu = True
else:
default_gpu = True
if default_gpu:
if not os.path.exists(savePath):
os.makedirs(savePath)
config = BertConfig.from_json_file(args.config_file)
if default_gpu:
# save all the hidden parameters.
with open(os.path.join(savePath, 'command.txt'), 'w') as f:
print(args, file=f) # Python 3.x
print('\n', file=f)
print(config, file=f)
tokenizer = BertTokenizer.from_pretrained(args.bert_model, do_lower_case=True)
dataset = CiderDataset(args.captions_path, args.tsv_path, args.cider_path, tokenizer, is_eval = True)
'''
length_of_data = len(dataset)
length_of_val = length_of_data // 10
train, val, test = random_split(dataset, [length_of_data - 2 * length_of_val, length_of_val, length_of_val])
train_dataloader = DataLoader(train, batch_size=10, shuffle=True)
val_dataloader = DataLoader(val, batch_size=10, shuffle=True)
test_dataloader = DataLoader(test, batch_size=10, shuffle=False)
'''
val_dataloader = DataLoader(dataset, batch_size=10,shuffle=False)
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
model_list = [1]
for model_name in model_list:
model = VILBertForVLTasks.from_pretrained(
args.from_pretrained, config, num_labels=1, default_gpu=default_gpu
)
model.to(device)
if args.local_rank != -1:
try:
from apex.parallel import DistributedDataParallel as DDP
except ImportError:
raise ImportError(
"Please install apex from https://www.github.com/nvidia/apex to use distributed and fp16 training."
)
elif n_gpu > 1:
model = torch.nn.DataParallel(model)
i = 0
# initialize the data iteration.
actual_values = []
predicted_values = []
image_ids_list = []
captions_list = []
model.eval()
for batch in val_dataloader:
i += 1
#if not args.no_cuda:
# batch = tuple(t.cuda(device=device, non_blocking=True) for t in batch)
features, spatials, image_mask, captions, _, input_mask, segment_ids, co_attention_mask, image_id, y, raw_captions = batch
#print(image_id)
#print(raw_captions)
#print(type(raw_captions))
captions_list.extend(raw_captions)
_, vil_logit, _, _, _, _, _ = \
model(captions.cuda(), features.cuda(), spatials.cuda(), segment_ids.cuda(), input_mask.cuda(), image_mask.cuda(), co_attention_mask.cuda())
actual_values += y.tolist()
predicted_values += vil_logit.squeeze(-1).tolist()
image_ids_list += image_id.tolist()
print("Model Name ", model_name)
print("Values ", np.corrcoef(np.array(actual_values), np.array(predicted_values)))
print("Actual mean", np.array(actual_values).mean())
print("Predicted mean", np.array(predicted_values).mean())
final_dict = {}
final_dict['actual_values'] = actual_values
final_dict['predicted_values'] = predicted_values
final_dict['image_ids'] = image_ids_list
final_dict['captions'] = captions_list
if len(args.out_path) > 0:
json.dump(final_dict, open(args.out_path, 'w'))
if __name__ == "__main__":
main()