-
Notifications
You must be signed in to change notification settings - Fork 20
/
infer.py
199 lines (156 loc) · 8.82 KB
/
infer.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
import pandas as pd
import argparse
import os
from tqdm import tqdm
import json
from PIL import Image
import h5py
from PIL import ImageFile
import torch
from transformers import AutoTokenizer, CLIPFeatureExtractor, AutoModel
from transformers.models.auto.configuration_auto import AutoConfig
from transformers.modeling_outputs import BaseModelOutput
from src.utils import load_data_for_inference, prep_strings, postprocess_preds
ImageFile.LOAD_TRUNCATED_IMAGES = True
PAD_TOKEN = '!'
EOS_TOKEN = '.'
CAPTION_LENGTH = 25
def evaluate_norag_model(args, feature_extractor, tokenizer, model, eval_df):
"""Models without retrival augmentation can be evaluated with a batch of length >1."""
out = []
bs = args.batch_size
for idx in tqdm(range(0, len(eval_df), bs)):
file_names = eval_df['file_name'][idx:idx+bs]
image_ids = eval_df['image_id'][idx:idx+bs]
decoder_input_ids = [prep_strings('', tokenizer, is_test=True) for _ in range(len(image_ids))]
# load image
images = [Image.open(args.images_dir + file_name).convert("RGB") for file_name in file_names]
pixel_values = feature_extractor(images, return_tensors="pt").pixel_values
with torch.no_grad():
preds = model.generate(pixel_values.to(args.device),
decoder_input_ids=torch.tensor(decoder_input_ids).to(args.device),
**args.generation_kwargs)
preds = tokenizer.batch_decode(preds)
for image_id, pred in zip(image_ids, preds):
pred = postprocess_preds(pred, tokenizer)
out.append({"image_id": int(image_id), "caption": pred})
return out
def evaluate_rag_model(args, feature_extractor, tokenizer, model, eval_df):
"""RAG models can only be evaluated with a batch of length 1."""
template = open(args.template_path).read().strip() + ' '
if args.features_path is not None:
features = h5py.File(args.features_path, 'r')
out = []
for idx in tqdm(range(len(eval_df))):
file_name = eval_df['file_name'][idx]
image_id = eval_df['image_id'][idx]
caps = eval_df['caps'][idx]
decoder_input_ids = prep_strings('', tokenizer, template=template, retrieved_caps=caps,
k=int(args.k), is_test=True)
# load image
if args.features_path is not None:
encoder_last_hidden_state = torch.FloatTensor([features[image_id][()]])
encoder_outputs = BaseModelOutput(last_hidden_state=encoder_last_hidden_state.to(args.device))
with torch.no_grad():
pred = model.generate(encoder_outputs=encoder_outputs,
decoder_input_ids=torch.tensor([decoder_input_ids]).to(args.device),
**args.generation_kwargs)
else:
image = Image.open(args.images_dir + file_name).convert("RGB")
pixel_values = feature_extractor(image, return_tensors="pt").pixel_values
with torch.no_grad():
pred = model.generate(pixel_values.to(args.device),
decoder_input_ids=torch.tensor([decoder_input_ids]).to(args.device),
**args.generation_kwargs)
pred = tokenizer.decode(pred[0])
pred = postprocess_preds(pred, tokenizer)
out.append({"image_id": int(image_id), "caption": pred})
return out
def load_model(args, checkpoint_path):
config = AutoConfig.from_pretrained(checkpoint_path + '/config.json')
model = AutoModel.from_pretrained(checkpoint_path)
model.config = config
model.eval()
model.to(args.device)
return model
def infer_one_checkpoint(args, feature_extractor, tokenizer, checkpoint_path, eval_df, infer_fn):
model = load_model(args, checkpoint_path)
preds = infer_fn(args, feature_extractor, tokenizer, model, eval_df)
with open(os.path.join(checkpoint_path, args.outfile_name), 'w') as outfile:
json.dump(preds, outfile)
def register_model_and_config():
from transformers import AutoModelForCausalLM
from src.vision_encoder_decoder import SmallCap, SmallCapConfig
from src.gpt2 import ThisGPT2Config, ThisGPT2LMHeadModel
from src.opt import ThisOPTConfig, ThisOPTForCausalLM
from src.xglm import ThisXGLMConfig, ThisXGLMForCausalLM
AutoConfig.register("this_xglm", ThisXGLMConfig)
AutoModel.register(ThisXGLMConfig, ThisXGLMForCausalLM)
AutoModelForCausalLM.register(ThisXGLMConfig, ThisXGLMForCausalLM)
AutoConfig.register("this_opt", ThisOPTConfig)
AutoModel.register(ThisOPTConfig, ThisOPTForCausalLM)
AutoModelForCausalLM.register(ThisOPTConfig, ThisOPTForCausalLM)
AutoConfig.register("this_gpt2", ThisGPT2Config)
AutoModel.register(ThisGPT2Config, ThisGPT2LMHeadModel)
AutoModelForCausalLM.register(ThisGPT2Config, ThisGPT2LMHeadModel)
AutoConfig.register("smallcap", SmallCapConfig)
AutoModel.register(SmallCapConfig, SmallCap)
def main(args):
register_model_and_config()
args.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if args.infer_test or args.disable_rag:
args.features_path = None
if args.features_path is not None:
feature_extractor = None
else:
feature_extractor = CLIPFeatureExtractor.from_pretrained(args.encoder_name)
if args.disable_rag:
args.k=0
infer_fn = evaluate_norag_model
else:
infer_fn = evaluate_rag_model
if args.infer_test:
split = 'test'
else:
split = 'val'
data = load_data_for_inference(args.annotations_path, args.captions_path)
eval_df = pd.DataFrame(data[split])
args.outfile_name = '{}_preds.json'.format(split)
# load and configure tokenizer
tokenizer = AutoTokenizer.from_pretrained(args.decoder_name)
tokenizer.pad_token = PAD_TOKEN
tokenizer.eos_token = EOS_TOKEN
# configure generation
args.generation_kwargs = {'max_new_tokens': CAPTION_LENGTH, 'no_repeat_ngram_size': 0, 'length_penalty': 0.,
'num_beams': 3, 'early_stopping': True, 'eos_token_id': tokenizer.eos_token_id}
# run inference once if checkpoint specified else run for all checkpoints
if args.checkpoint_path is not None:
checkpoint_path = os.path.join(args.model_path, args.checkpoint_path)
infer_one_checkpoint(args, feature_extractor, tokenizer, checkpoint_path, eval_df, infer_fn)
else:
for checkpoint_path in os.listdir(args.model_path):
if 'runs' in checkpoint_path:
continue
checkpoint_path = os.path.join(args.model_path, checkpoint_path)
if os.path.exists(os.path.join(checkpoint_path, args.outfile_name)):
print('Found existing file for', checkpoint_path)
else:
infer_one_checkpoint(args, feature_extractor, tokenizer, checkpoint_path, eval_df, infer_fn)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Model Training')
parser.add_argument("--images_dir", type=str, default="data/images/", help="Directory where input image features are stored")
parser.add_argument("--features_path", type=str, default='features/val.hdf5', help="H5 file with cached input image features")
parser.add_argument("--annotations_path", type=str, default="data/dataset_coco.json", help="JSON file with annotations in Karpathy splits")
parser.add_argument("--model_path", type=str, default=None, help="Path to model to use for inference")
parser.add_argument("--checkpoint_path", type=str, default=None, help="Path to checkpoint to use for inference; If not specified, will infer with all checkpoints")
parser.add_argument("--infer_test", action="store_true", default=False, help="Use test data instead of val data")
parser.add_argument("--encoder_name", type=str, default="openai/clip-vit-base-patch32", help="Encoder name as found of HuggingFace or stored locally")
parser.add_argument("--decoder_name", type=str, default="gpt2", help="Decoder name as found of HuggingFace or stored locally")
parser.add_argument("--disable_rag", action="store_true", default=False, help="Disable retrieval augmentation or not")
parser.add_argument("--k", type=int, default=4, help="Number of retrieved captions to use in prefix")
parser.add_argument("--retrieval_encoder", type=str, default="RN50x64", help="Visual encoder used for retieving captions")
parser.add_argument("--captions_path", type=str, default="data/retrieved_caps_resnet50x64.json", help="JSON file with retrieved captions")
parser.add_argument("--template_path", type=str, default="src/template.txt", help="TXT file with template")
parser.add_argument("--batch_size", type=int, default=64, help="Batch size; only matter if evaluating a norag model")
args = parser.parse_args()
main(args)