-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_captions.py
177 lines (162 loc) · 7.51 KB
/
save_captions.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
import clip
import os
from torch import nn
import numpy as np
import torch
import torch.nn.functional as nnf
import sys
from typing import Tuple, List, Union, Optional
from transformers import (
GPT2Tokenizer,
GPT2LMHeadModel,
AdamW,
get_linear_schedule_with_warmup,
)
import skimage.io as io
import PIL.Image
from train import TransformerMapper
from tqdm import tqdm
from os import listdir
from os.path import isfile, join
import json
from train import *
from utils import *
# Used to save text feature embedding from first iteration of the clipcap model.
DEVICE = torch.device("cuda:0")
class Predictor:
def setup(self, args):
"""Load the model into memory to make running multiple predictions efficient"""
self.device = torch.device("cuda")
self.clip_model, self.preprocess = clip.load(
"ViT-B/32", device=self.device, jit=False
)
self.tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
self.prefix_length = args.prefix_length
weights_path = args.weights
prefix_size= 640 if args.is_rn else 512
if args.text_data is not None:
prefix_size *= 2
if args.only_prefix:
model = ClipCaptionPrefix(
self.prefix_length,
clip_length=args.prefix_length_clip,
prefix_size=prefix_size,
num_layers=args.num_layers,
mapping_type=args.mapping_type,
)
else:
model = ClipCaptionModel(
self.prefix_length,
clip_length = args.prefix_length_clip,
prefix_size=prefix_size,
num_layers = args.num_layers,
mapping_type=args.mapping_type,
)
model.load_state_dict(torch.load(weights_path, map_location=DEVICE))
model = model.eval()
model = model.to(self.device)
self.model = model
def predict(self, image, model, use_beam_search):
"""Run a single prediction on the model"""
image = io.imread(image)
model = self.models[model]
pil_image = PIL.Image.fromarray(image)
image = self.preprocess(pil_image).unsqueeze(0).to(self.device)
with torch.no_grad():
prefix = self.clip_model.encode_image(image).to(
self.device, dtype=torch.float32
)
prefix_embed = model.clip_project(prefix).reshape(1, self.prefix_length, -1)
if use_beam_search:
return generate_beam(model, self.tokenizer, embed=prefix_embed)[0]
else:
return generate2(model, self.tokenizer, embed=prefix_embed)
def predict2(self, sample, model, use_beam_search):
"""Run a single prediction on the model"""
(tokens, mask, prefix) = sample
with torch.no_grad():
prefix_embed = self.model(tokens, prefix, mask)["prefix"]
# prefix_embed = self.model.clip_project(prefix).reshape(1, self.prefix_length, -1)
if use_beam_search:
return generate_beam(self.model, self.tokenizer, embed=prefix_embed)[0]
else:
return generate2(self.model, self.tokenizer, embed=prefix_embed)
def main(args):
model = "coco"
use_beam_search = True
run_type = args.run_type
tag = args.tag
out_path = f"./data/coco/oscar_split_{tag}_{run_type}.pkl"
device = torch.device("cuda:0")
clip_model, preprocess = clip.load(args.clip_model_type, device=device, jit=False)
clip_model.eval()
predictor = Predictor()
predictor.setup(args)
dataset = ClipCocoDataset(args.data, args.prefix_length,
normalize_prefix=args.normalize_prefix,
text_data_path=args.text_data,
)
dataloader = DataLoader(dataset, batch_size=40, shuffle=False, drop_last=False)
print(f"Validation dataset size is {len(dataloader)}")
all_embeddings = []
all_captions = []
for i, (tokens, mask, prefix) in enumerate(tqdm(dataloader)):
tokens, mask, prefix = tokens.to(DEVICE), mask.to(DEVICE), prefix.to(DEVICE, dtype=torch.float32)
if tokens.shape[0] > 1:
for j in range(tokens.shape[0]):
idx = i + j
if args.oracle:
sentence = tokens[j]
for k in range(tokens[j].shape[0]):
if tokens[j,k] == 0:
sentence = tokens[j,:k]
break
result = predictor.tokenizer.decode(sentence)
else:
sample = (tokens[j].unsqueeze(0), mask[j].unsqueeze(0), prefix[j].unsqueeze(0))
result = predictor.predict2(sample, model, use_beam_search)
if len(result) > 77:
result = result[:77]
generated_caption = clip.tokenize(result).to(device)
text_prefix = clip_model.encode_text(generated_caption).detach().cpu()
all_embeddings.append(text_prefix)
all_captions.append(dataset.captions[idx])
if idx % 100 == 0:
with open(out_path, 'wb') as f:
pickle.dump({"clip_embedding": torch.cat(all_embeddings, dim=0), "captions": all_captions}, f)
print("Step", idx, "-- Image", dataset.image_ids[idx], "-- Caption:", result, "-- feature shape:", torch.cat(all_embeddings, dim=0).shape)
else:
raise IOError("batch size 1 not tested recently! e.g. missing oracle access.")
sample = (tokens, mask, prefix)
idx = i
result = predictor.predict2(sample, model, use_beam_search)
if len(result) > 77:
result = result[:77]
generated_caption = clip.tokenize(result).to(device)
text_prefix = clip_model.encode_text(generated_caption).detach().cpu()
all_embeddings.append(text_prefix)
all_captions.append(dataset.captions[idx])
if idx % 100 == 0:
with open(out_path, 'wb') as f:
pickle.dump({"clip_embedding": torch.cat(all_embeddings, dim=0), "captions": all_captions}, f)
print("Step", idx, "-- Image", dataset.image_ids[idx], "-- Caption:", result, "-- feature shape:", torch.cat(all_embeddings, dim=0).shape)
with open(out_path, 'wb') as f:
pickle.dump({"clip_embedding": torch.cat(all_embeddings, dim=0), "captions": all_captions}, f)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--data', default='./data/coco/oscar_split_train.pkl')
parser.add_argument('--text_data', default=None)
parser.add_argument('--prefix_length', type=int, default=10)
parser.add_argument('--prefix_length_clip', type=int, default=10)
parser.add_argument('--only_prefix', dest='only_prefix', action='store_true')
parser.add_argument('--mapping_type', type=str, default='mlp', help='mlp/transformer')
parser.add_argument('--num_layers', type=int, default=8)
parser.add_argument('--is_rn', dest='is_rn', action='store_true')
parser.add_argument('--normalize_prefix', dest='normalize_prefix', action='store_true')
parser.add_argument('--tag', type=str, required=True)
parser.add_argument('--clip_model_type', default="ViT-B/32", choices=('RN50', 'RN101', 'RN50x4', 'ViT-B/32'))
parser.add_argument('--run_type', default="train", choices=("train", "val"))
parser.add_argument('--weights', type=str, required=True)
parser.add_argument('--oracle', action='store_true', default=False)
args = parser.parse_args()
main(args)