forked from rmokady/CLIP_prefix_caption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
474 lines (410 loc) · 19.8 KB
/
train.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import torch
import torch.nn as nn
from torch.nn import functional as nnf
from torch.utils.data import Dataset, DataLoader
from enum import Enum
from transformers import GPT2Tokenizer, GPT2LMHeadModel, AdamW, get_linear_schedule_with_warmup
from tqdm import tqdm
import os
import pickle
import sys
import argparse
import json
from typing import Tuple, Optional, Union
CONST_PREFIX = 1
class MappingType(Enum):
MLP = 'mlp'
Transformer = 'transformer'
class ClipCocoDataset(Dataset):
def __len__(self) -> int:
return len(self.captions_tokens)
def pad_tokens(self, item: int):
tokens = self.captions_tokens[item]
padding = self.max_seq_len - tokens.shape[0]
if padding > 0:
tokens = torch.cat((tokens, torch.zeros(padding, dtype=torch.int64) - 1))
self.captions_tokens[item] = tokens
elif padding < 0:
tokens = tokens[:self.max_seq_len]
self.captions_tokens[item] = tokens
mask = tokens.ge(0) # mask is zero where we out of sequence
tokens[~mask] = 0
mask = mask.float()
mask = torch.cat((torch.ones(self.prefix_length), mask), dim=0) # adding prefix mask
return tokens, mask
def __getitem__(self, item: int) -> Tuple[torch.Tensor, ...]:
tokens, mask = self.pad_tokens(item)
prefix = self.prefixes[self.caption2embedding[item]]
if self.normalize_prefix:
prefix = prefix.float()
prefix = prefix / prefix.norm(2, -1)
return tokens, mask, prefix
def __init__(self, data_path: str, prefix_length: int, gpt2_type: str = "gpt2",
normalize_prefix=False):
self.tokenizer = GPT2Tokenizer.from_pretrained(gpt2_type)
self.prefix_length = prefix_length
self.normalize_prefix = normalize_prefix
with open(data_path, 'rb') as f:
all_data = pickle.load(f)
print("Data size is %0d" % len(all_data["clip_embedding"]))
sys.stdout.flush()
self.prefixes = all_data["clip_embedding"]
captions_raw = all_data["captions"]
self.image_ids = [caption["image_id"] for caption in captions_raw]
self.captions = [caption['caption'] for caption in captions_raw]
if os.path.isfile(f"{data_path[:-4]}_tokens.pkl"):
with open(f"{data_path[:-4]}_tokens.pkl", 'rb') as f:
self.captions_tokens, self.caption2embedding, self.max_seq_len = pickle.load(f)
else:
self.captions_tokens = []
self.caption2embedding = []
max_seq_len = 0
for caption in captions_raw:
self.captions_tokens.append(torch.tensor(self.tokenizer.encode(caption['caption']), dtype=torch.int64))
self.caption2embedding.append(caption["clip_embedding"])
max_seq_len = max(max_seq_len, self.captions_tokens[-1].shape[0])
# self.max_seq_len = max_seq_len
with open(f"{data_path[:-4]}_tokens.pkl", 'wb') as f:
pickle.dump([self.captions_tokens, self.caption2embedding, max_seq_len], f)
all_len = torch.tensor([len(self.captions_tokens[i]) for i in range(len(self))]).float()
self.max_seq_len = min(int(all_len.mean() + all_len.std() * 10), int(all_len.max()))
class MLP(nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.model(x)
def __init__(self, sizes: Tuple[int, ...], bias=True, act=nn.Tanh):
super(MLP, self).__init__()
layers = []
for i in range(len(sizes) - 1):
layers.append(nn.Linear(sizes[i], sizes[i + 1], bias=bias))
if i < len(sizes) - 2:
layers.append(act())
self.model = nn.Sequential(*layers)
class MlpTransformer(nn.Module):
def __init__(self, in_dim, h_dim, out_d: Optional[int] = None, act=nnf.relu, dropout=0.):
super().__init__()
out_d = out_d if out_d is not None else in_dim
self.fc1 = nn.Linear(in_dim, h_dim)
self.act = act
self.fc2 = nn.Linear(h_dim, out_d)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.dropout(x)
x = self.fc2(x)
x = self.dropout(x)
return x
class MultiHeadAttention(nn.Module):
def __init__(self, dim_self, dim_ref, num_heads, bias=True, dropout=0.):
super().__init__()
self.num_heads = num_heads
head_dim = dim_self // num_heads
self.scale = head_dim ** -0.5
self.to_queries = nn.Linear(dim_self, dim_self, bias=bias)
self.to_keys_values = nn.Linear(dim_ref, dim_self * 2, bias=bias)
self.project = nn.Linear(dim_self, dim_self)
self.dropout = nn.Dropout(dropout)
def forward(self, x, y=None, mask=None):
y = y if y is not None else x
b, n, c = x.shape
_, m, d = y.shape
# b n h dh
queries = self.to_queries(x).reshape(b, n, self.num_heads, c // self.num_heads)
# b m 2 h dh
keys_values = self.to_keys_values(y).reshape(b, m, 2, self.num_heads, c // self.num_heads)
keys, values = keys_values[:, :, 0], keys_values[:, :, 1]
attention = torch.einsum('bnhd,bmhd->bnmh', queries, keys) * self.scale
if mask is not None:
if mask.dim() == 2:
mask = mask.unsqueeze(1)
attention = attention.masked_fill(mask.unsqueeze(3), float("-inf"))
attention = attention.softmax(dim=2)
out = torch.einsum('bnmh,bmhd->bnhd', attention, values).reshape(b, n, c)
out = self.project(out)
return out, attention
class TransformerLayer(nn.Module):
def forward_with_attention(self, x, y=None, mask=None):
x_, attention = self.attn(self.norm1(x), y, mask)
x = x + x_
x = x + self.mlp(self.norm2(x))
return x, attention
def forward(self, x, y=None, mask=None):
x = x + self.attn(self.norm1(x), y, mask)[0]
x = x + self.mlp(self.norm2(x))
return x
def __init__(self, dim_self, dim_ref, num_heads, mlp_ratio=4., bias=False, dropout=0., act=nnf.relu,
norm_layer: nn.Module = nn.LayerNorm):
super().__init__()
self.norm1 = norm_layer(dim_self)
self.attn = MultiHeadAttention(dim_self, dim_ref, num_heads, bias=bias, dropout=dropout)
self.norm2 = norm_layer(dim_self)
self.mlp = MlpTransformer(dim_self, int(dim_self * mlp_ratio), act=act, dropout=dropout)
class Transformer(nn.Module):
def forward_with_attention(self, x, y=None, mask=None):
attentions = []
for layer in self.layers:
x, att = layer.forward_with_attention(x, y, mask)
attentions.append(att)
return x, attentions
def forward(self, x, y=None, mask=None):
for i, layer in enumerate(self.layers):
if i % 2 == 0 and self.enc_dec: # cross
x = layer(x, y)
elif self.enc_dec: # self
x = layer(x, x, mask)
else: # self or cross
x = layer(x, y, mask)
return x
def __init__(self, dim_self: int, num_heads: int, num_layers: int, dim_ref: Optional[int] = None,
mlp_ratio: float = 2., act=nnf.relu, norm_layer: nn.Module = nn.LayerNorm, enc_dec: bool = False):
super(Transformer, self).__init__()
dim_ref = dim_ref if dim_ref is not None else dim_self
self.enc_dec = enc_dec
if enc_dec:
num_layers = num_layers * 2
layers = []
for i in range(num_layers):
if i % 2 == 0 and enc_dec: # cross
layers.append(TransformerLayer(dim_self, dim_ref, num_heads, mlp_ratio, act=act, norm_layer=norm_layer))
elif enc_dec: # self
layers.append(TransformerLayer(dim_self, dim_self, num_heads, mlp_ratio, act=act, norm_layer=norm_layer))
else: # self or cross
layers.append(TransformerLayer(dim_self, dim_ref, num_heads, mlp_ratio, act=act, norm_layer=norm_layer))
self.layers = nn.ModuleList(layers)
class TransformerMapper(nn.Module):
def forward(self, x):
x = self.linear(x).view(x.shape[0], self.clip_length, -1)
if CONST_PREFIX: # With learned prefix constant
prefix = self.prefix_const.unsqueeze(0).expand(x.shape[0], *self.prefix_const.shape)
prefix = torch.cat((x, prefix), dim=1)
out = self.transformer(prefix)[:, self.clip_length:]
else: # Without learned prefix constant
prefix = x
out = self.transformer(prefix)
return out
def __init__(self, dim_clip: int, dim_embedding: int, prefix_length: int, clip_length: int, num_layers: int = 8):
super(TransformerMapper, self).__init__()
self.clip_length = clip_length
self.transformer = Transformer(dim_embedding, 8, num_layers)
self.linear = nn.Linear(dim_clip, clip_length * dim_embedding)
if CONST_PREFIX:
self.prefix_const = nn.Parameter(torch.randn(prefix_length, dim_embedding), requires_grad=True)
class ClipCaptionModel(nn.Module):
def get_dummy_token(self, batch_size: int, device: torch.device) -> torch.Tensor:
return torch.zeros(batch_size, self.prefix_length, dtype=torch.int64, device=device)
def forward(self, tokens: torch.Tensor, prefix: torch.Tensor, mask: Optional[torch.Tensor] = None,
labels: Optional[torch.Tensor] = None):
embedding_text = self.gpt.transformer.wte(tokens)
prefix_projections = self.clip_project(prefix).view(-1, self.prefix_length, self.gpt_embedding_size)
embedding_cat = torch.cat((prefix_projections, embedding_text), dim=1)
if labels is not None:
dummy_token = self.get_dummy_token(tokens.shape[0], tokens.device)
labels = torch.cat((dummy_token, tokens), dim=1)
out = self.gpt(inputs_embeds=embedding_cat, labels=labels, attention_mask=mask)
return out
def __init__(self, prefix_length: int, clip_length: Optional[int] = None, prefix_size: int = 512,
num_layers: int = 8, mapping_type: MappingType = MappingType.MLP):
super(ClipCaptionModel, self).__init__()
self.prefix_length = prefix_length
self.gpt = GPT2LMHeadModel.from_pretrained('gpt2')
self.gpt_embedding_size = self.gpt.transformer.wte.weight.shape[1]
if mapping_type == MappingType.MLP:
self.clip_project = MLP((prefix_size, (self.gpt_embedding_size * prefix_length) // 2,
self.gpt_embedding_size * prefix_length))
else:
self.clip_project = TransformerMapper(prefix_size, self.gpt_embedding_size, prefix_length,
clip_length, num_layers)
class ClipCaptionPrefix(ClipCaptionModel):
def parameters(self, recurse: bool = True):
return self.clip_project.parameters()
def train(self, mode: bool = True):
super(ClipCaptionPrefix, self).train(mode)
self.gpt.eval()
return self
def save_config(args: argparse.Namespace):
config = {}
for key, item in args._get_kwargs():
config[key] = item
out_path = os.path.join(args.out_dir, f"{args.prefix}.json")
with open(out_path, 'w') as outfile:
json.dump(config, outfile)
def load_model(config_path: str, epoch_or_latest: Union[str, int] = '_latest'):
with open(config_path) as f:
config = json.load(f)
parser = argparse.ArgumentParser()
parser.set_defaults(**config)
args = parser.parse_args()
if type(epoch_or_latest) is int:
epoch_or_latest = f"-{epoch_or_latest:03d}"
model_path = os.path.join(args.out_dir, f"{args.prefix}{epoch_or_latest}.pt")
if args.only_prefix:
model = ClipCaptionPrefix(args.prefix_length)
else:
model = ClipCaptionModel(args.prefix_length)
if os.path.isfile(model_path):
print(f"loading model from {model_path}")
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
else:
print(f"{model_path} is not exist")
return model, parser
def train(dataset: ClipCocoDataset, model: ClipCaptionModel, args,
lr: float = 2e-5, warmup_steps: int = 5000, output_dir: str = ".", output_prefix: str = ""):
device = torch.device('cuda:0')
batch_size = args.bs
epochs = args.epochs
if not os.path.exists(output_dir):
os.makedirs(output_dir)
model = model.to(device)
model.train()
optimizer = AdamW(model.parameters(), lr=lr)
train_dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True, drop_last=True)
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=warmup_steps, num_training_steps=epochs * len(train_dataloader)
)
# save_config(args)
for epoch in range(epochs):
print(f">>> Training epoch {epoch}")
sys.stdout.flush()
progress = tqdm(total=len(train_dataloader), desc=output_prefix)
for idx, (tokens, mask, prefix) in enumerate(train_dataloader):
model.zero_grad()
tokens, mask, prefix = tokens.to(device), mask.to(device), prefix.to(device, dtype=torch.float32)
outputs = model(tokens, prefix, mask)
logits = outputs.logits[:, dataset.prefix_length - 1: -1]
loss = nnf.cross_entropy(logits.reshape(-1, logits.shape[-1]), tokens.flatten(), ignore_index=0)
loss.backward()
optimizer.step()
scheduler.step()
optimizer.zero_grad()
progress.set_postfix({"loss": loss.item()})
progress.update()
if (idx + 1) % 10000 == 0:
torch.save(
model.state_dict(),
os.path.join(output_dir, f"{output_prefix}_latest.pt"),
)
progress.close()
if epoch % args.save_every == 0 or epoch == epochs - 1:
torch.save(
model.state_dict(),
os.path.join(output_dir, f"{output_prefix}-{epoch:03d}.pt"),
)
return model
def evaluate(dataset: ClipCocoDataset, model: ClipCaptionModel, args, output_dir: str = ".", output_prefix: str = ""):
device = torch.device('cuda:0')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
print(f">>> Loading dataset...")
eval_dataloader = DataLoader(dataset)
print(f">>> Evaluating...")
sys.stdout.flush()
output = {}
progress = tqdm(total=len(eval_dataloader), desc=output_prefix)
for idx, (tokens, mask, prefix) in enumerate(eval_dataloader):
# Since there are like 4 captions per image, I skip all but the first for inference.
image_id = dataset.image_ids[idx]
if image_id not in output:
prefix = prefix.to(device, dtype=torch.float32)
prefix_embed = model.clip_project(prefix).reshape(1, model.prefix_length, -1)
p = generate2(model, dataset.tokenizer, embed=prefix_embed)
output[image_id] = [p]
progress.update()
with open(os.path.join(output_dir, 'evaluate.json'), 'w') as outfile:
json.dump(output, outfile)
progress.close()
def generate2(
model,
tokenizer,
tokens=None,
prompt=None,
embed=None,
entry_count=1,
entry_length=67, # maximum number of words
top_p=0.8,
temperature=1.0,
stop_token: str = ".",
):
model.eval()
generated_num = 0
generated_list = []
stop_token_index = tokenizer.encode(stop_token)[0]
filter_value = -float("Inf")
device = next(model.parameters()).device
with torch.no_grad():
for entry_idx in range(entry_count):
if embed is not None:
generated = embed
else:
if tokens is None:
tokens = torch.tensor(tokenizer.encode(prompt))
tokens = tokens.unsqueeze(0).to(device)
generated = model.gpt.transformer.wte(tokens)
for i in range(entry_length):
outputs = model.gpt(inputs_embeds=generated)
logits = outputs.logits
logits = logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
sorted_logits, sorted_indices = torch.sort(logits, descending=True)
cumulative_probs = torch.cumsum(
nnf.softmax(sorted_logits, dim=-1), dim=-1
)
sorted_indices_to_remove = cumulative_probs > top_p
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[
..., :-1
].clone()
sorted_indices_to_remove[..., 0] = 0
indices_to_remove = sorted_indices[sorted_indices_to_remove]
logits[:, indices_to_remove] = filter_value
next_token = torch.argmax(logits, -1).unsqueeze(0)
next_token_embed = model.gpt.transformer.wte(next_token)
if tokens is None:
tokens = next_token
else:
tokens = torch.cat((tokens, next_token), dim=1)
generated = torch.cat((generated, next_token_embed), dim=1)
if stop_token_index == next_token.item():
break
output_list = list(tokens.squeeze().cpu().numpy())
output_text = tokenizer.decode(output_list)
generated_list.append(output_text)
return generated_list[0]
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--evaluate', action='store_true')
parser.add_argument('--model_path', default='')
parser.add_argument('--data', default='./data/coco/oscar_split_train.pkl')
parser.add_argument('--out_dir', default='./checkpoints')
parser.add_argument('--prefix', default='coco_prefix', help='prefix for saved filenames')
parser.add_argument('--epochs', type=int, default=10)
parser.add_argument('--save_every', type=int, default=1)
parser.add_argument('--prefix_length', type=int, default=10)
parser.add_argument('--prefix_length_clip', type=int, default=10)
parser.add_argument('--bs', type=int, default=40)
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')
args = parser.parse_args()
prefix_length = args.prefix_length
dataset = ClipCocoDataset(args.data, prefix_length, normalize_prefix=args.normalize_prefix)
prefix_dim = 640 if args.is_rn else 512
args.mapping_type = {'mlp': MappingType.MLP, 'transformer': MappingType.Transformer}[args.mapping_type]
if args.only_prefix:
model = ClipCaptionPrefix(prefix_length, clip_length=args.prefix_length_clip, prefix_size=prefix_dim,
num_layers=args.num_layers, mapping_type=args.mapping_type)
print("Model: only prefix")
else:
model = ClipCaptionModel(prefix_length, clip_length=args.prefix_length_clip, prefix_size=prefix_dim,
num_layers=args.num_layers, mapping_type=args.mapping_type)
print("Model: both prefix and GPT")
sys.stdout.flush()
if (args.evaluate):
print(f"Model: loading from {args.model_path}")
model.load_state_dict(torch.load(args.model_path, map_location=torch.device("cuda:0")))
model.to("cuda:0") # not sure why the map_location in the line above doesn't work...
evaluate(dataset, model, args, output_dir=args.out_dir, output_prefix=args.prefix)
else:
print('Model: number of trainable parameters is', sum(p.numel() for p in model.parameters() if p.requires_grad))
train(dataset, model, args, output_dir=args.out_dir, output_prefix=args.prefix)
if __name__ == '__main__':
main()