-
Notifications
You must be signed in to change notification settings - Fork 2
/
gpt.py
743 lines (659 loc) · 22.7 KB
/
gpt.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
import logging
from typing import Any, Dict, Literal, Optional, Tuple
import numpy as np
import timm
import torch
import torch.nn.functional as F
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from timm.data import resolve_model_data_config, str_to_interp_mode
from torch import nn
from boldgpt.data import DataConfig, load_nsd_flat_mask
from boldgpt.patching import MaskedPatchify
from boldgpt.shuffle import permute, random_order
from boldgpt.tokenizer import KMeansTokenizer
from . import constants as C
from .generate import generate
from .registry import register_configs, register_model
from .transformer import Transformer
from .utils import imshow, infer_embed_dim, r2_score, to_numpy
class IGPT(nn.Module):
def __init__(
self,
patchify: MaskedPatchify,
tokenizer: Optional[KMeansTokenizer],
decoder: Transformer,
encoder: Optional[nn.Module] = None,
shuffle: bool = True,
modality: Literal["image", "bold"] = "bold",
):
super().__init__()
self.shuffle = shuffle
self.modality = modality
self.is_categorical = tokenizer is not None
self.is_seq2seq = encoder is not None
self.with_sub_embed = decoder.with_sub_embed
self.patchify = patchify
if tokenizer is None:
self.register_module("tokenizer", None)
else:
self.tokenizer = tokenizer
self.decoder = decoder
if encoder is None:
self.register_module("encoder", None)
else:
# TODO: May want to use a `FeatureExtractor` to extract intermediate rather
# than final features. But currently the forward hooks interfere with model
# compilation.
self.encoder = encoder
def forward(
self,
batch: Dict[str, torch.Tensor],
) -> Tuple[torch.Tensor, Dict[str, Optional[torch.Tensor]]]:
# Unpack data
images = batch["activity"] if self.modality == "bold" else batch["image"]
if self.is_seq2seq:
inputs = batch["image"] if self.modality == "bold" else batch["activity"]
sub_indices = batch["subject_id"] if self.with_sub_embed else None
# Get patches and optionally tokens
patches = self.patchify(images)
B, N = patches.shape[:2]
device = patches.device
if self.shuffle and self.training:
order, ranking = random_order(B, N, device=device)
patches = permute(patches, order)
else:
order = ranking = None
tokens = self.tokenizer(patches) if self.is_categorical else None
# Get encoder context
if self.is_seq2seq:
context = self.encoder.forward_features(inputs)
else:
context = None
# Forward pass
output = self.decoder(
patches, sub_indices=sub_indices, context=context, order=order
)
# Drop the trailing EOS token (and any registers)
output = output[:, :N]
state = dict(
patches=patches,
order=order,
ranking=ranking,
tokens=tokens,
context=context,
output=output,
)
return output, state
def loss_fn(
self,
batch: Dict[str, torch.Tensor],
output: torch.Tensor,
state: Dict[str, Optional[torch.Tensor]],
) -> torch.Tensor:
if self.is_categorical:
tokens = state["tokens"]
loss = F.cross_entropy(output.flatten(0, 1), tokens.flatten())
else:
patches = state["patches"]
order = state["order"]
mask = self.patchify.patch_mask.expand_as(patches)
# If patches are shuffled, the mask must be shuffled as well.
if order is not None:
mask = permute(mask, order)
loss = torch.sum(mask * (output - patches) ** 2) / mask.sum()
return loss
@torch.no_grad()
def generate(
self,
batch: Dict[str, torch.Tensor],
prompt_fraction: float = 0.25,
shuffle: bool = False,
order: Optional[torch.Tensor] = None,
context: Optional[torch.Tensor] = None,
temperature: float = 1.0,
top_k: Optional[int] = None,
use_cache: bool = True,
) -> Tuple[torch.Tensor, Dict[str, Optional[torch.Tensor]]]:
if not self.shuffle and (shuffle or order is not None):
raise ValueError(
"Must train with shuffled patches to generate with "
"shuffled/custom order"
)
images = batch["activity"] if self.modality == "bold" else batch["image"]
if self.is_seq2seq:
inputs = batch["image"] if self.modality == "bold" else batch["activity"]
sub_indices = batch["subject_id"] if self.with_sub_embed else None
patches = self.patchify(images)
B, N = patches.shape[:2]
device = patches.device
if shuffle:
order, ranking = random_order(B, N, device=device)
elif order is not None:
ranking = torch.argsort(order, dim=1)
else:
ranking = None
if order is not None:
patches = permute(patches, order)
prompt_length = int(prompt_fraction * N)
prompt = patches[:, :prompt_length]
sample_mask = torch.ones(B, N, device=prompt.device, dtype=torch.bool)
sample_mask[:, :prompt_length] = False
# Get encoder context
if self.is_seq2seq and context is None:
context = self.encoder.forward_features(inputs)
sample = generate(
model=self.decoder,
prompt=prompt,
sub_indices=sub_indices,
context=context,
order=order,
tokenizer=self.tokenizer,
patch_mask=self.patchify.patch_mask,
temperature=temperature,
top_k=top_k,
use_cache=use_cache,
)
if order is not None:
sample_mask = permute(sample_mask, ranking)
sample = permute(sample, ranking)
sample_mask = self.patchify.inverse_vector(sample_mask)
sample = self.patchify.inverse(sample)
state = {
"patches": patches,
"order": order,
"ranking": ranking,
"prompt_length": prompt_length,
"context": context,
"sample": sample,
"sample_mask": sample_mask,
}
return sample, state
@torch.no_grad()
def prepare_examples(
self,
batch: Dict[str, torch.Tensor],
state: Dict[str, torch.Tensor],
) -> Dict[str, np.ndarray]:
"""
Prepare a batch of examples for figure generation.
"""
subid = batch["subject_id"] if "subject_id" in batch else None
if "nsd_id" in batch:
imgid = batch["nsd_id"]
elif "image_id" in batch:
imgid = batch["image_id"]
else:
imgid = None
images = batch["activity"] if self.modality == "bold" else batch["image"]
if self.is_seq2seq:
inputs = batch["image"] if self.modality == "bold" else batch["activity"]
else:
inputs = None
patches = state["patches"]
order = state["order"]
ranking = state["ranking"]
tokens = state["tokens"]
context = state["context"]
output = state["output"]
if context is not None:
context = context.detach()
output = output.detach()
B, N = patches.shape[:2]
device = patches.device
# Ensure images are masked
mask = self.patchify.expanded_mask
images = mask * images
# Unshuffle if necessary
if order is not None:
patches = permute(patches, ranking)
if tokens is not None:
tokens = permute(tokens, ranking)
output = permute(output, ranking)
else:
ranking = torch.arange(N, device=device).expand(B, -1)
# Invert order, target, reconstruction and compute R^2
order_map = self.patchify.inverse_vector(ranking.float() + 1.0)
# Remove channels dimension
if self.modality == "image":
order_map = order_map[:, 0]
if self.is_categorical:
target = self.tokenizer.inverse(tokens)
pred = torch.argmax(output, dim=-1)
recon = self.tokenizer.inverse(pred)
else:
target = patches
recon = output
target = self.patchify.inverse(target)
recon = self.patchify.inverse(recon)
target_r2 = r2_score(target[:, mask], images[:, mask], reduction="none")
recon_r2 = r2_score(recon[:, mask], images[:, mask], reduction="none")
sample, sample_state = self.generate(
batch=batch,
prompt_fraction=0.0 if self.is_seq2seq else 0.25,
order=order,
context=context,
)
sample_mask = sample_state["sample_mask"]
sample_r2 = r2_score(
(sample_mask * sample)[:, mask],
(sample_mask * images)[:, mask],
reduction="none",
)
examples = {
"subject_id": subid,
"image_id": imgid,
"images": images,
"inputs": inputs,
"order_map": order_map,
"target": target,
"recon": recon,
"sample": sample,
"sample_mask": sample_mask,
"target_r2": target_r2,
"recon_r2": recon_r2,
"sample_r2": sample_r2,
}
examples = {k: to_numpy(v) for k, v in examples.items()}
return examples
def plot_examples(
self,
examples: Dict[str, np.ndarray],
num_examples: int = 10,
fname: Optional[str] = None,
) -> Figure:
"""
Plot a grid of samples and predictions.
"""
subid = examples["subject_id"]
imgid = examples["image_id"]
images = examples["images"]
inputs = examples["inputs"]
order_map = examples["order_map"]
target = examples["target"]
recon = examples["recon"]
sample = examples["sample"]
sample_mask = examples["sample_mask"]
target_r2 = examples["target_r2"]
recon_r2 = examples["recon_r2"]
sample_r2 = examples["sample_r2"]
# Remove channels dimension
if self.modality == "image":
sample_mask = sample_mask[:, 0]
sample_mask_rgba = np.where(
sample_mask[:, None],
np.zeros((1, 4, 1, 1)),
np.array([1.0, 1.0, 1.0, 0.5]).reshape(1, 4, 1, 1),
)
sample_mask_rgba = self.patchify.mask.cpu().numpy() * sample_mask_rgba
img_shape = images.shape[-2:]
plotw = 3.0
ploth = 3.5
nr = num_examples
# Extra plots depending on model
nc = 4 + self.is_seq2seq + self.is_categorical
f, axs = plt.subplots(nr, nc, figsize=(nc * plotw, nr * ploth), squeeze=False)
textdict = {
"fontsize": 10,
"color": "w",
"bbox": {
"boxstyle": "square",
"fc": (0.5, 0.5, 0.5),
"ec": "none",
"pad": 0,
},
}
for ii in range(num_examples):
label = []
if subid is not None:
label.append(f"s{subid[ii]+1:02d}")
if imgid is not None:
label.append(f"{imgid[ii]:05d}")
label = " ".join(label)
col = 0
if self.is_seq2seq:
plt.sca(axs[ii, col])
tform = axs[ii, col].transAxes
imshow(inputs[ii], img_shape=img_shape)
plt.text(
0.5,
0.98,
"Image" if self.modality == "bold" else "Activity",
ha="center",
va="top",
transform=tform,
**textdict,
)
col += 1
plt.sca(axs[ii, col])
tform = axs[ii, col].transAxes
imshow(images[ii])
plt.text(
0.5,
0.98,
"Activity" if self.modality == "bold" else "Image",
ha="center",
va="top",
transform=tform,
**textdict,
)
plt.text(
0.04, 0.0, label, ha="left", va="bottom", transform=tform, **textdict
)
col += 1
if self.is_categorical:
plt.sca(axs[ii, col])
tform = axs[ii, col].transAxes
imshow(target[ii])
plt.text(
0.5,
0.98,
"Target",
ha="center",
va="top",
transform=tform,
**textdict,
)
plt.text(
0.98,
0.0,
f"R2={target_r2[ii]:.3f}",
ha="right",
va="bottom",
transform=tform,
**textdict,
)
col += 1
plt.sca(axs[ii, col])
tform = axs[ii, col].transAxes
imshow(recon[ii])
plt.text(
0.5, 0.98, "Pred", ha="center", va="top", transform=tform, **textdict
)
plt.text(
0.98,
0.0,
f"R2={recon_r2[ii]:.3f}",
ha="right",
va="bottom",
transform=tform,
**textdict,
)
col += 1
plt.sca(axs[ii, col])
tform = axs[ii, col].transAxes
imshow(sample[ii])
imshow(sample_mask_rgba[ii])
plt.text(
0.5, 0.98, "Sample", ha="center", va="top", transform=tform, **textdict
)
plt.text(
0.98,
0.0,
f"R2={sample_r2[ii]:.3f}",
ha="right",
va="bottom",
transform=tform,
**textdict,
)
col += 1
plt.sca(axs[ii, col])
tform = axs[ii, col].transAxes
imshow(order_map[ii])
plt.text(
0.5, 0.98, "Order", ha="center", va="top", transform=tform, **textdict
)
col += 1
plt.tight_layout(pad=0.2, h_pad=0.05)
if fname is not None:
plt.savefig(fname, bbox_inches="tight")
return f
def get_data_config(self) -> DataConfig:
cfg = {}
if self.modality == "bold" and not self.is_seq2seq:
# boldgpt
cfg["dataset"] = "NSD-Flat"
cfg["columns"] = ["subject_id", "nsd_id", "activity"]
elif self.modality == "bold" and self.is_seq2seq:
# image2bold
cfg["dataset"] = "NSD-Flat"
cfg["columns"] = ["subject_id", "nsd_id", "image", "activity"]
timm_data_config = resolve_model_data_config(self.encoder)
cfg["img_size"] = timm_data_config["input_size"][1]
cfg["img_mean"] = timm_data_config["mean"]
cfg["img_std"] = timm_data_config["std"]
cfg["interp_mode"] = str_to_interp_mode(timm_data_config["interpolation"])
elif self.modality == "image" and not self.is_seq2seq:
# imagegpt
cfg["dataset"] = "COCO"
cfg["columns"] = ["image_id", "image"]
cfg["img_size"] = self.patchify.mask.shape[1]
elif self.modality == "image" and self.is_seq2seq:
# bold2image
cfg["dataset"] = "NSD-Flat"
cfg["columns"] = ["subject_id", "nsd_id", "image", "activity"]
cfg["img_size"] = self.patchify.mask.shape[1]
return DataConfig(**cfg)
def extra_repr(self) -> str:
return (
f"is_categorical={self.is_categorical}, "
f"is_seq2seq={self.is_seq2seq}, "
f"shuffle={self.shuffle}, "
f"modality={self.modality}"
)
def _create_boldgpt(
*,
mask: Optional[np.ndarray] = None,
patch_size: int = 10,
categorical: bool = False,
ordering: str = "radial",
with_sub_embed: bool = True,
vocab_size: int = 1024,
shuffle: bool = True,
num_subs: int = 1024,
embed_dim: int = 768,
depth: int = 12,
num_heads: int = 12,
mlp_ratio: float = 4.0,
drop_rate: float = 0.0,
sub_drop_rate: float = 0.0,
proj_drop_rate: float = 0.0,
attn_drop_rate: float = 0.0,
drop_path_rate: float = 0.0,
**kwargs,
):
if kwargs:
logging.warning("Extra unused kwargs: %s", kwargs)
if mask is None:
mask = load_nsd_flat_mask()
patchify = MaskedPatchify(mask, patch_size=patch_size, ordering=ordering)
if categorical:
tokenizer = KMeansTokenizer(vocab_size=vocab_size, dim=patchify.dim)
else:
tokenizer = None
decoder = Transformer(
num_patches=patchify.num_patches,
in_features=patchify.dim,
num_subs=num_subs,
num_classes=(vocab_size if categorical else patchify.dim),
embed_dim=embed_dim,
depth=depth,
num_heads=num_heads,
mlp_ratio=mlp_ratio,
with_sub_embed=with_sub_embed,
with_next_pos=shuffle,
with_cross=False,
is_causal=True,
is_masked=False,
drop_rate=drop_rate,
sub_drop_rate=sub_drop_rate,
proj_drop_rate=proj_drop_rate,
attn_drop_rate=attn_drop_rate,
drop_path_rate=drop_path_rate,
)
model = IGPT(
patchify=patchify,
tokenizer=tokenizer,
decoder=decoder,
shuffle=shuffle,
modality="bold",
)
return model
def _create_imagegpt(
*,
img_size: int = 224,
patch_size: int = 10,
ordering: str = "reverse_radial",
categorical: bool = False,
vocab_size: int = 1024,
shuffle: bool = True,
embed_dim: int = 768,
depth: int = 12,
num_heads: int = 12,
mlp_ratio: float = 4.0,
drop_rate: float = 0.0,
proj_drop_rate: float = 0.0,
attn_drop_rate: float = 0.0,
drop_path_rate: float = 0.0,
**kwargs,
):
if kwargs:
logging.warning("Extra unused kwargs: %s", kwargs)
mask = torch.ones(img_size, img_size, dtype=torch.bool)
patchify = MaskedPatchify(
mask, num_channels=3, patch_size=patch_size, ordering=ordering
)
if categorical:
tokenizer = KMeansTokenizer(vocab_size=vocab_size, dim=patchify.dim)
else:
tokenizer = None
decoder = Transformer(
num_patches=patchify.num_patches,
in_features=patchify.dim,
num_classes=(vocab_size if categorical else patchify.dim),
embed_dim=embed_dim,
depth=depth,
num_heads=num_heads,
mlp_ratio=mlp_ratio,
with_sub_embed=False,
with_next_pos=shuffle,
with_cross=False,
is_causal=True,
is_masked=False,
drop_rate=drop_rate,
proj_drop_rate=proj_drop_rate,
attn_drop_rate=attn_drop_rate,
drop_path_rate=drop_path_rate,
)
model = IGPT(
patchify=patchify,
tokenizer=tokenizer,
decoder=decoder,
shuffle=shuffle,
modality="image",
)
return model
def _create_image2bold(
*,
mask: Optional[np.ndarray] = None,
patch_size: int = 10,
ordering: str = "radial",
categorical: bool = False,
with_sub_embed: bool = True,
vocab_size: int = 1024,
shuffle: bool = True,
encoder_name: str = "eva02_base_patch14_224.mim_in22k",
encoder_kwargs: Optional[Dict[str, Any]] = None,
num_subs: int = 1024,
embed_dim: int = 768,
depth: int = 12,
num_heads: int = 12,
mlp_ratio: float = 4.0,
drop_rate: float = 0.0,
sub_drop_rate: float = 0.0,
proj_drop_rate: float = 0.0,
attn_drop_rate: float = 0.0,
drop_path_rate: float = 0.0,
**kwargs,
):
if kwargs:
logging.warning("Extra unused kwargs: %s", kwargs)
if mask is None:
mask = load_nsd_flat_mask()
patchify = MaskedPatchify(mask, patch_size=patch_size, ordering=ordering)
if categorical:
tokenizer = KMeansTokenizer(vocab_size=vocab_size, dim=patchify.dim)
else:
tokenizer = None
encoder_kwargs = encoder_kwargs or {}
encoder_kwargs = {"pretrained": True, **encoder_kwargs}
encoder = timm.create_model(encoder_name, **encoder_kwargs)
encoder_dim = infer_embed_dim(encoder_name)
decoder = Transformer(
num_patches=patchify.num_patches,
in_features=patchify.dim,
num_subs=num_subs,
num_classes=(vocab_size if categorical else patchify.dim),
embed_dim=embed_dim,
context_dim=encoder_dim,
depth=depth,
num_heads=num_heads,
mlp_ratio=mlp_ratio,
with_sub_embed=with_sub_embed,
with_next_pos=shuffle,
with_cross=True,
is_causal=True,
is_masked=False,
drop_rate=drop_rate,
sub_drop_rate=sub_drop_rate,
proj_drop_rate=proj_drop_rate,
attn_drop_rate=attn_drop_rate,
drop_path_rate=drop_path_rate,
)
model = IGPT(
patchify=patchify,
tokenizer=tokenizer,
decoder=decoder,
encoder=encoder,
shuffle=shuffle,
modality="bold",
)
return model
@register_model
def boldgpt_tiny_patch10(**kwargs):
return _create_boldgpt(patch_size=10, **C.TINY_ARCH_KWARGS, **kwargs)
@register_model
def boldgpt_small_patch10(**kwargs):
return _create_boldgpt(patch_size=10, **C.SMALL_ARCH_KWARGS, **kwargs)
@register_model
def boldgpt_base_patch10(**kwargs):
return _create_boldgpt(patch_size=10, **C.BASE_ARCH_KWARGS, **kwargs)
@register_model
def imagegpt_tiny_patch16(**kwargs):
return _create_imagegpt(patch_size=16, **C.TINY_ARCH_KWARGS, **kwargs)
@register_model
def imagegpt_small_patch16(**kwargs):
return _create_imagegpt(patch_size=16, **C.SMALL_ARCH_KWARGS, **kwargs)
@register_model
def imagegpt_base_patch16(**kwargs):
return _create_imagegpt(patch_size=16, **C.BASE_ARCH_KWARGS, **kwargs)
@register_model
def image2bold_tiny_patch10(**kwargs):
return _create_image2bold(patch_size=10, **C.TINY_ARCH_KWARGS, **kwargs)
@register_model
def image2bold_small_patch10(**kwargs):
return _create_image2bold(patch_size=10, **C.SMALL_ARCH_KWARGS, **kwargs)
@register_model
def image2bold_base_patch10(**kwargs):
return _create_image2bold(patch_size=10, **C.BASE_ARCH_KWARGS, **kwargs)
CONFIGS = {
"boldgpt_small_patch10.kmq": {
"has_weights": True,
"kwargs": {"categorical": True},
},
"boldgpt_small_patch10.cont": {
"has_weights": True,
"kwargs": {"categorical": False},
},
}
register_configs(CONFIGS)