File size: 42,536 Bytes
1b78df8 | 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 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 | #!/usr/bin/env python
# coding: utf-8
# # Import packages & functions
# In[1]:
import os
import sys
import json
import argparse
import numpy as np
import math
from einops import rearrange
import time
import random
import string
import h5py
from tqdm import tqdm
import webdataset as wds
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torchvision import transforms
from accelerate import Accelerator
# SDXL unCLIP requires code from https://github.com/Stability-AI/generative-models/tree/main
sys.path.append('generative_models/')
import sgm
from generative_models.sgm.modules.encoders.modules import FrozenOpenCLIPImageEmbedder # bigG embedder
# tf32 data type is faster than standard float32
torch.backends.cuda.matmul.allow_tf32 = True
# custom functions #
import utils
# In[34]:
import torch
import torch.nn.functional as F
def classPrecision(logits, y_true, top=1):
"""
Calculate the precision of the top-n predictions.
Parameters:
logits (torch.Tensor): The output logits from the model (shape: [batch_size, num_classes]).
y_true (torch.Tensor): The ground truth labels (shape: [batch_size]).
top (int): The number of top predictions to consider.
Returns:
float: The precision percentage of the top-n predictions.
"""
# Apply softmax to get probabilities
probs = F.softmax(logits, dim=1)
# Get the top-n predictions
top_n_preds = torch.topk(probs, top, dim=1).indices
# Check if y_true is in top-n predictions
correct = top_n_preds.eq(y_true.view(-1, 1).expand_as(top_n_preds))
# Calculate precision
precision = correct.sum().item() / y_true.size(0)
return precision * 100
# Example usage:
logits = torch.randn(8, 41) # Example logits tensor
y_true = torch.randint(0, 41, (8,)) # Example ground truth labels
top_n_precision = classPrecision(logits, y_true, top=1)
print(f"Top-1 Precision: {top_n_precision:.2f}%")
# In[2]:
### Multi-GPU config ###
local_rank = os.getenv('RANK')
if local_rank is None:
local_rank = 0
else:
local_rank = int(local_rank)
print("LOCAL RANK ", local_rank)
data_type = torch.float16 # change depending on your mixed_precision
num_devices = torch.cuda.device_count()
if num_devices==0: num_devices = 1
# First use "accelerate config" in terminal and setup using deepspeed stage 2 with CPU offloading!
accelerator = Accelerator(split_batches=False, mixed_precision="fp16")
if utils.is_interactive(): # set batch size here if using interactive notebook instead of submitting job
global_batch_size = batch_size = 8
else:
global_batch_size = os.environ["GLOBAL_BATCH_SIZE"]
batch_size = int(os.environ["GLOBAL_BATCH_SIZE"]) // num_devices
# In[3]:
print("PID of this process =",os.getpid())
device = accelerator.device
print("device:",device)
world_size = accelerator.state.num_processes
distributed = not accelerator.state.distributed_type == 'NO'
num_devices = torch.cuda.device_count()
if num_devices==0 or not distributed: num_devices = 1
num_workers = num_devices
print(accelerator.state)
print("distributed =",distributed, "num_devices =", num_devices, "local rank =", local_rank, "world size =", world_size, "data_type =", data_type)
print = accelerator.print # only print if local_rank=0
# # Configurations
# In[4]:
# if running this interactively, can specify jupyter_args here for argparser to use
if utils.is_interactive():
model_name = "testing2"
print("model_name:", model_name)
# global_batch_size and batch_size should already be defined in the 2nd cell block
jupyter_args = f"--data_path=/weka/proj-medarc/shared/mindeyev2_dataset \
--cache_dir=/weka/proj-medarc/shared/cache \
--model_name={model_name} \
--no-multi_subject --subj=1 --batch_size={batch_size} --num_sessions=40 \
--hidden_dim=1024 --clip_scale=1. \
--no-blurry_recon --blur_scale=.5 \
--use_prior --prior_scale=30 \
--n_blocks=4 --max_lr=3e-5 --mixup_pct=.33 --num_epochs=150 --no-use_image_aug \
--ckpt_interval=999 --no-ckpt_saving --wandb_log"
# --multisubject_ckpt=../train_logs/multisubject_subj01_1024_24bs_nolow
print(jupyter_args)
jupyter_args = jupyter_args.split()
from IPython.display import clear_output # function to clear print outputs in cell
get_ipython().run_line_magic('load_ext', 'autoreload')
# this allows you to change functions in models.py or utils.py and have this notebook automatically update with your revisions
get_ipython().run_line_magic('autoreload', '2')
# In[5]:
parser = argparse.ArgumentParser(description="Model Training Configuration")
parser.add_argument(
"--model_name", type=str, default="testing2",
help="name of model, used for ckpt saving and wandb logging (if enabled)",
)
parser.add_argument(
"--data_path", type=str, default=os.getcwd(),
help="Path to where NSD data is stored / where to download it to",
)
parser.add_argument(
"--cache_dir", type=str, default=os.getcwd(),
help="Path to where misc. files downloaded from huggingface are stored. Defaults to current src directory.",
)
parser.add_argument(
"--subj",type=int, default=1, choices=[1,2,3,4,5,6,7,8],
help="Validate on which subject?",
)
parser.add_argument(
"--multisubject_ckpt", type=str, default=None,
help="Path to pre-trained multisubject model to finetune a single subject from. multisubject must be False.",
)
parser.add_argument(
"--num_sessions", type=int, default=1,
help="Number of training sessions to include",
)
parser.add_argument(
"--use_prior",action=argparse.BooleanOptionalAction,default=True,
help="whether to train diffusion prior (True) or just rely on retrieval part of the pipeline (False)",
)
parser.add_argument(
"--batch_size", type=int, default=16,
help="Batch size can be increased by 10x if only training retreival submodule and not diffusion prior",
)
parser.add_argument(
"--wandb_log",action=argparse.BooleanOptionalAction,default=False,
help="whether to log to wandb",
)
parser.add_argument(
"--wandb_project",type=str,default="stability",
help="wandb project name",
)
parser.add_argument(
"--mixup_pct",type=float,default=.33,
help="proportion of way through training when to switch from BiMixCo to SoftCLIP",
)
parser.add_argument(
"--blurry_recon",action=argparse.BooleanOptionalAction,default=True,
help="whether to output blurry reconstructions",
)
parser.add_argument(
"--blur_scale",type=float,default=.5,
help="multiply loss from blurry recons by this number",
)
parser.add_argument(
"--clip_scale",type=float,default=1.,
help="multiply contrastive loss by this number",
)
parser.add_argument(
"--prior_scale",type=float,default=30,
help="multiply diffusion prior loss by this",
)
parser.add_argument(
"--use_image_aug",action=argparse.BooleanOptionalAction,default=False,
help="whether to use image augmentation",
)
parser.add_argument(
"--num_epochs",type=int,default=150,
help="number of epochs of training",
)
parser.add_argument(
"--multi_subject",action=argparse.BooleanOptionalAction,default=False,
)
parser.add_argument(
"--new_test",action=argparse.BooleanOptionalAction,default=True,
)
parser.add_argument(
"--n_blocks",type=int,default=4,
)
parser.add_argument(
"--hidden_dim",type=int,default=1024,
)
parser.add_argument(
"--lr_scheduler_type",type=str,default='cycle',choices=['cycle','linear'],
)
parser.add_argument(
"--ckpt_saving",action=argparse.BooleanOptionalAction,default=True,
)
parser.add_argument(
"--ckpt_interval",type=int,default=5,
help="save backup ckpt and reconstruct every x epochs",
)
parser.add_argument(
"--seed",type=int,default=42,
)
parser.add_argument(
"--max_lr",type=float,default=3e-5,
)
if utils.is_interactive():
args = parser.parse_args(jupyter_args)
else:
args = parser.parse_args()
# create global variables without the args prefix
for attribute_name in vars(args).keys():
globals()[attribute_name] = getattr(args, attribute_name)
# seed all random functions
utils.seed_everything(seed)
outdir = os.path.abspath(f'../train_logs/{model_name}')
if not os.path.exists(outdir) and ckpt_saving:
os.makedirs(outdir,exist_ok=True)
if use_image_aug or blurry_recon:
import kornia
from kornia.augmentation.container import AugmentationSequential
if use_image_aug:
img_augment = AugmentationSequential(
kornia.augmentation.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0.1, p=0.3),
same_on_batch=False,
data_keys=["input"],
)
if multi_subject:
subj_list = np.arange(1,9)
subj_list = subj_list[subj_list != subj]
else:
subj_list = [subj]
print("subj_list", subj_list, "num_sessions", num_sessions)
# In[6]:
max_lr
# # Prep data, models, and dataloaders
# ### Creating wds dataloader, preload betas and all 73k possible images
# In[7]:
def my_split_by_node(urls): return urls
num_voxels_list = []
if multi_subject:
nsessions_allsubj=np.array([40, 40, 32, 30, 40, 32, 40, 30])
num_samples_per_epoch = (750*40) // num_devices
else:
num_samples_per_epoch = (750*num_sessions) // num_devices
print("dividing batch size by subj_list, which will then be concatenated across subj during training...")
batch_size = batch_size // len(subj_list)
num_iterations_per_epoch = num_samples_per_epoch // (batch_size*len(subj_list))
print("batch_size =", batch_size, "num_iterations_per_epoch =",num_iterations_per_epoch, "num_samples_per_epoch =",num_samples_per_epoch)
# In[8]:
train_data = {}
train_dl = {}
num_voxels = {}
voxels = {}
for s in subj_list:
print(f"Training with {num_sessions} sessions")
if multi_subject:
train_url = f"{data_path}/wds/subj0{s}/train/" + "{0.." + f"{nsessions_allsubj[s-1]-1}" + "}.tar"
else:
train_url = f"{data_path}/wds/subj0{s}/train/" + "{0.." + f"{num_sessions-1}" + "}.tar"
print(train_url)
train_data[f'subj0{s}'] = wds.WebDataset(train_url,resampled=True,nodesplitter=my_split_by_node)\
.shuffle(750, initial=1500, rng=random.Random(42))\
.decode("torch")\
.rename(behav="behav.npy", past_behav="past_behav.npy", future_behav="future_behav.npy", olds_behav="olds_behav.npy")\
.to_tuple(*["behav", "past_behav", "future_behav", "olds_behav"])
train_dl[f'subj0{s}'] = torch.utils.data.DataLoader(train_data[f'subj0{s}'], batch_size=batch_size, shuffle=False, drop_last=False, pin_memory=True)
f = h5py.File(f'{data_path}/betas_all_subj0{s}_fp32_renorm.hdf5', 'r')
betas = f['betas'][:]
betas = torch.Tensor(betas).to("cpu").to(data_type)
num_voxels_list.append(betas[0].shape[-1])
num_voxels[f'subj0{s}'] = betas[0].shape[-1]
voxels[f'subj0{s}'] = betas
print(f"num_voxels for subj0{s}: {num_voxels[f'subj0{s}']}")
print("Loaded all subj train dls and betas!\n")
# Validate only on one subject
if multi_subject:
subj = subj_list[0] # cant validate on the actual held out person so picking first in subj_list
if not new_test: # using old test set from before full dataset released (used in original MindEye paper)
if subj==3:
num_test=2113
elif subj==4:
num_test=1985
elif subj==6:
num_test=2113
elif subj==8:
num_test=1985
else:
num_test=2770
test_url = f"{data_path}/wds/subj0{subj}/test/" + "0.tar"
elif new_test: # using larger test set from after full dataset released
if subj==3:
num_test=2371
elif subj==4:
num_test=2188
elif subj==6:
num_test=2371
elif subj==8:
num_test=2188
else:
num_test=3000
test_url = f"{data_path}/wds/subj0{subj}/new_test/" + "0.tar"
print(test_url)
test_data = wds.WebDataset(test_url,resampled=False,nodesplitter=my_split_by_node)\
.shuffle(750, initial=1500, rng=random.Random(42))\
.decode("torch")\
.rename(behav="behav.npy", past_behav="past_behav.npy", future_behav="future_behav.npy", olds_behav="olds_behav.npy")\
.to_tuple(*["behav", "past_behav", "future_behav", "olds_behav"])
test_dl = torch.utils.data.DataLoader(test_data, batch_size=num_test, shuffle=False, drop_last=True, pin_memory=True)
print(f"Loaded test dl for subj{subj}!\n")
# In[9]:
# Load 73k NSD images
f = h5py.File(f'{data_path}/coco_images_224_float16.hdf5', 'r')
images = f['images']
print("Loaded all 73k possible NSD images to cpu!", images.shape)
# ## Load models
# ### CLIP image embeddings model
# In[10]:
clip_img_embedder = FrozenOpenCLIPImageEmbedder(
arch="ViT-bigG-14",
version="laion2b_s39b_b160k",
output_tokens=True,
only_tokens=True,
)
clip_img_embedder.to(device)
clip_seq_dim = 256
clip_emb_dim = 1664
# ### SD VAE
# In[11]:
if blurry_recon:
from diffusers import AutoencoderKL
autoenc = AutoencoderKL(
down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D', 'DownEncoderBlock2D'],
up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D', 'UpDecoderBlock2D'],
block_out_channels=[128, 256, 512, 512],
layers_per_block=2,
sample_size=256,
)
ckpt = torch.load(f'{cache_dir}/sd_image_var_autoenc.pth')
autoenc.load_state_dict(ckpt)
autoenc.eval()
autoenc.requires_grad_(False)
autoenc.to(device)
utils.count_params(autoenc)
from autoencoder.convnext import ConvnextXL
cnx = ConvnextXL(f'{cache_dir}/convnext_xlarge_alpha0.75_fullckpt.pth')
cnx.requires_grad_(False)
cnx.eval()
cnx.to(device)
mean = torch.tensor([0.485, 0.456, 0.406]).to(device).reshape(1,3,1,1)
std = torch.tensor([0.228, 0.224, 0.225]).to(device).reshape(1,3,1,1)
blur_augs = AugmentationSequential(
kornia.augmentation.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.2, hue=0.1, p=0.8),
kornia.augmentation.RandomGrayscale(p=0.1),
kornia.augmentation.RandomSolarize(p=0.1),
kornia.augmentation.RandomResizedCrop((224,224), scale=(.9,.9), ratio=(1,1), p=1.0),
data_keys=["input"],
)
# ### MindEye modules
# In[12]:
class MindEyeModule(nn.Module):
def __init__(self):
super(MindEyeModule, self).__init__()
def forward(self, x):
return x
model = MindEyeModule()
model
# In[13]:
class RidgeRegression(torch.nn.Module):
# make sure to add weight_decay when initializing optimizer to enable regularization
def __init__(self, input_sizes, out_features):
super(RidgeRegression, self).__init__()
self.out_features = out_features
self.linears = torch.nn.ModuleList([
torch.nn.Linear(input_size, out_features) for input_size in input_sizes
])
def forward(self, x, subj_idx):
out = self.linears[subj_idx](x[:,0]).unsqueeze(1)
return out
class IndividRidgeRegression(torch.nn.Module):
def __init__(self, input_size, out_features):
super(IndividRidgeRegression, self).__init__()
self.out_features = out_features
self.linear = torch.nn.Linear(input_size, out_features)
def forward(self, x):
out = self.linear(x)
return out
model.ridge = RidgeRegression(num_voxels_list, out_features=hidden_dim)
utils.count_params(model.ridge)
utils.count_params(model)
# test on subject 1 with fake data
b = torch.randn((2,1,num_voxels_list[0]))
print(b.shape, model.ridge(b,0).shape)
# In[14]:
from models import BrainNetwork
model.backbone = BrainNetwork(h=hidden_dim, in_dim=hidden_dim, seq_len=1, n_blocks=n_blocks,
clip_size=clip_emb_dim, out_dim=clip_emb_dim*clip_seq_dim,
blurry_recon=blurry_recon, clip_scale=clip_scale)
utils.count_params(model.backbone)
utils.count_params(model)
# test that the model works on some fake data
b = torch.randn((2,1,hidden_dim))
print("b.shape",b.shape)
backbone_, clip_, blur_ = model.backbone(b)
print(backbone_.shape, clip_.shape, blur_[0].shape, blur_[1].shape)
# ### Load semantic clusters
# In[15]:
path_semantic_names = "/weka/proj-medarc/shared/mindeyev2_dataset/semantic_cluster_names.npy"
path_semantic_cluster = "/weka/proj-medarc/shared/mindeyev2_dataset/COCO_73k_semantic_cluster.npy"
semantic_cluster_names = np.load(path_semantic_names)
semantic_cluster = np.load(path_semantic_cluster)
possible_semantic_clusters = np.unique(semantic_cluster)
# one-hot encode semantic clusters
# move possible_semantic_clusters to numbers and create a dictionary
semantic_cluster_dict = {cluster: i for i, cluster in enumerate(possible_semantic_clusters)}
semantic_cluster_onehot = torch.zeros((len(semantic_cluster), len(possible_semantic_clusters)))
for i, cluster in enumerate(semantic_cluster):
semantic_cluster_onehot[i, semantic_cluster_dict[cluster]] = 1
print("semantic_cluster_onehot.shape", semantic_cluster_onehot.shape)
num_seman_clusters = len(np.unique(semantic_cluster))
print("num_seman_clusters", num_seman_clusters)
# ### Adding the ridge regression to the class
# In[16]:
# if use_prior:
# from models import *
# # setup diffusion prior network
# out_dim = clip_emb_dim
# depth = 6
# dim_head = 52
# heads = clip_emb_dim//52 # heads * dim_head = clip_emb_dim
# timesteps = 100
# prior_network = PriorNetwork(
# dim=out_dim,
# depth=depth,
# dim_head=dim_head,
# heads=heads,
# causal=False,
# num_tokens = clip_seq_dim,
# learned_query_mode="pos_emb"
# )
# model.diffusion_prior = BrainDiffusionPrior(
# net=prior_network,
# image_embed_dim=out_dim,
# condition_on_text_encodings=False,
# timesteps=timesteps,
# cond_drop_prob=0.2,
# image_embed_scale=None,
# )
# utils.count_params(model.diffusion_prior)
# utils.count_params(model)
model.RRClassifier = IndividRidgeRegression(clip_emb_dim*clip_seq_dim, out_features=num_seman_clusters)
utils.count_params(model.RRClassifier)
utils.count_params(model)
# ### Setup optimizer / lr / ckpt saving
# In[17]:
no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
opt_grouped_parameters = [
{'params': [p for n, p in model.ridge.named_parameters()], 'weight_decay': 1e-2},
{'params': [p for n, p in model.backbone.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 1e-2},
{'params': [p for n, p in model.backbone.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0},
]
# if use_prior:
# opt_grouped_parameters.extend([
# {'params': [p for n, p in model.diffusion_prior.named_parameters() if not any(nd in n for nd in no_decay)], 'weight_decay': 1e-2},
# {'params': [p for n, p in model.diffusion_prior.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
# ])
opt_grouped_parameters.extend([
{'params': [p for n, p in model.RRClassifier.named_parameters()], 'weight_decay': 1e-2},
])
optimizer = torch.optim.AdamW(opt_grouped_parameters, lr=max_lr)
if lr_scheduler_type == 'linear':
lr_scheduler = torch.optim.lr_scheduler.LinearLR(
optimizer,
total_iters=int(np.floor(num_epochs*num_iterations_per_epoch)),
last_epoch=-1
)
elif lr_scheduler_type == 'cycle':
total_steps=int(np.floor(num_epochs*num_iterations_per_epoch))
print("total_steps", total_steps)
lr_scheduler = torch.optim.lr_scheduler.OneCycleLR(
optimizer,
max_lr=max_lr,
total_steps=total_steps,
final_div_factor=1000,
last_epoch=-1, pct_start=2/num_epochs
)
def save_ckpt(tag):
ckpt_path = outdir+f'/{tag}.pth'
if accelerator.is_main_process:
unwrapped_model = accelerator.unwrap_model(model)
torch.save({
'epoch': epoch,
'model_state_dict': unwrapped_model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'lr_scheduler': lr_scheduler.state_dict(),
'train_losses': losses,
'test_losses': test_losses,
'lrs': lrs,
}, ckpt_path)
print(f"\n---saved {outdir}/{tag} ckpt!---\n")
def load_ckpt(tag,load_lr=True,load_optimizer=True,load_epoch=True,strict=True,outdir=outdir,multisubj_loading=False):
print(f"\n---loading {outdir}/{tag}.pth ckpt---\n")
checkpoint = torch.load(outdir+'/last.pth', map_location='cpu')
state_dict = checkpoint['model_state_dict']
if multisubj_loading: # remove incompatible ridge layer that will otherwise error
state_dict.pop('ridge.linears.0.weight',None)
model.load_state_dict(state_dict, strict=strict)
if load_epoch:
globals()["epoch"] = checkpoint['epoch']
print("Epoch",epoch)
if load_optimizer:
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
if load_lr:
lr_scheduler.load_state_dict(checkpoint['lr_scheduler'])
del checkpoint
print("\nDone with model preparations!")
num_params = utils.count_params(model)
# # Weights and Biases
# In[18]:
if local_rank==0 and wandb_log: # only use main process for wandb logging
import wandb
wandb_project = 'mindeye_semantic_cluster'
print(f"wandb {wandb_project} run {model_name}")
# need to configure wandb beforehand in terminal with "wandb init"!
wandb_config = {
"model_name": model_name,
"global_batch_size": global_batch_size,
"batch_size": batch_size,
"num_epochs": num_epochs,
"num_sessions": num_sessions,
"num_params": num_params,
"clip_scale": clip_scale,
"prior_scale": prior_scale,
"blur_scale": blur_scale,
"use_image_aug": use_image_aug,
"max_lr": max_lr,
"mixup_pct": mixup_pct,
"num_samples_per_epoch": num_samples_per_epoch,
"num_test": num_test,
"ckpt_interval": ckpt_interval,
"ckpt_saving": ckpt_saving,
"seed": seed,
"distributed": distributed,
"num_devices": num_devices,
"world_size": world_size,
"train_url": train_url,
"test_url": test_url,
}
print("wandb_config:\n",wandb_config)
print("wandb_id:",model_name)
wandb.login(host='https://stability.wandb.io')
wandb.init(
id=model_name,
project=wandb_project,
name=model_name,
config=wandb_config,
resume="allow",
)
else:
wandb_log = False
# # Main
# In[19]:
epoch = 0
losses, test_losses, lrs = [], [], []
best_test_loss = 1e9
torch.cuda.empty_cache()
# In[20]:
# load multisubject stage1 ckpt if set
if multisubject_ckpt is not None:
load_ckpt("last",outdir=multisubject_ckpt,load_lr=False,load_optimizer=False,load_epoch=False,strict=False,multisubj_loading=True)
# In[21]:
train_dls = [train_dl[f'subj0{s}'] for s in subj_list]
model, optimizer, *train_dls, lr_scheduler, semantic_cluster_onehot = accelerator.prepare(model, optimizer, *train_dls, lr_scheduler, semantic_cluster_onehot)
# leaving out test_dl since we will only have local_rank 0 device do evals
# In[22]:
print(num_iterations_per_epoch)
# In[ ]:
print(f"{model_name} starting with epoch {epoch} / {num_epochs}")
progress_bar = tqdm(range(epoch,num_epochs), ncols=1200, disable=(local_rank!=0))
test_image, test_voxel = None, None
mse = nn.MSELoss()
l1 = nn.L1Loss()
soft_loss_temps = utils.cosine_anneal(0.004, 0.0075, num_epochs - int(mixup_pct * num_epochs))
for epoch in progress_bar:
model.train()
fwd_percent_correct = 0.
bwd_percent_correct = 0.
test_fwd_percent_correct = 0.
test_bwd_percent_correct = 0.
recon_cossim = 0.
test_recon_cossim = 0.
recon_mse = 0.
test_recon_mse = 0.
loss_clip_total = 0.
loss_blurry_total = 0.
loss_blurry_cont_total = 0.
test_loss_clip_total = 0.
loss_prior_total = 0.
test_loss_prior_total = 0.
loss_RR_total = 0.
test_loss_RR_total = 0.
blurry_pixcorr = 0.
test_blurry_pixcorr = 0. # needs >.456 to beat low-level subj01 results in mindeye v1
class_precisions_1 = 0
test_class_precisions_1 = 0
class_precisions_5 = 0
test_class_precisions_5 = 0
class_precisions_10 = 0
test_class_precisions_10 = 0
# pre-load all batches for this epoch (it's MUCH faster to pre-load in bulk than to separate loading per batch)
voxel_iters = {} # empty dict because diff subjects have differing # of voxels
image_iters = torch.zeros(num_iterations_per_epoch, batch_size*len(subj_list), 3, 224, 224).float()
annot_iters = {}
perm_iters, betas_iters, select_iters = {}, {}, {}
images_indexes = {}
for s, train_dl in enumerate(train_dls):
with torch.cuda.amp.autocast(dtype=data_type):
iter = -1
for behav0, past_behav0, future_behav0, old_behav0 in train_dl:
# Load images to cpu from hdf5 (requires sorted indexing)
image_idx = behav0[:,0,0].cpu().long().numpy()
image0, image_sorted_idx = np.unique(image_idx, return_index=True)
if len(image0) != len(image_idx): # hdf5 cant handle duplicate indexing
continue
iter += 1
image0 = torch.tensor(images[image0], dtype=data_type)
image_iters[iter,s*batch_size:s*batch_size+batch_size] = image0
images_indexes[f"subj0{s}_iter{iter}"] = image_sorted_idx
# Load voxels for current batch, matching above indexing
voxel_idx = behav0[:,0,5].cpu().long().numpy()
voxel_sorted_idx = voxel_idx[image_sorted_idx]
voxel0 = voxels[f'subj0{subj_list[s]}'][voxel_sorted_idx]
voxel0 = torch.Tensor(voxel0).unsqueeze(1)
if epoch < int(mixup_pct * num_epochs):
voxel0, perm, betas, select = utils.mixco(voxel0)
perm_iters[f"subj0{subj_list[s]}_iter{iter}"] = perm
betas_iters[f"subj0{subj_list[s]}_iter{iter}"] = betas
select_iters[f"subj0{subj_list[s]}_iter{iter}"] = select
voxel_iters[f"subj0{subj_list[s]}_iter{iter}"] = voxel0
if iter >= num_iterations_per_epoch-1:
break
# you now have voxel_iters and image_iters with num_iterations_per_epoch batches each
for train_i in range(num_iterations_per_epoch):
with torch.cuda.amp.autocast(dtype=data_type):
optimizer.zero_grad()
loss=0.
voxel_list = [voxel_iters[f"subj0{s}_iter{train_i}"].detach().to(device) for s in subj_list]
image = image_iters[train_i].detach()
image = image.to(device)
if use_image_aug:
image = img_augment(image)
clip_target = clip_img_embedder(image)
assert not torch.any(torch.isnan(clip_target))
if epoch < int(mixup_pct * num_epochs):
perm_list = [perm_iters[f"subj0{s}_iter{train_i}"].detach().to(device) for s in subj_list]
perm = torch.cat(perm_list, dim=0)
betas_list = [betas_iters[f"subj0{s}_iter{train_i}"].detach().to(device) for s in subj_list]
betas = torch.cat(betas_list, dim=0)
select_list = [select_iters[f"subj0{s}_iter{train_i}"].detach().to(device) for s in subj_list]
select = torch.cat(select_list, dim=0)
voxel_ridge_list = [model.ridge(voxel_list[si],si) for si,s in enumerate(subj_list)]
voxel_ridge = torch.cat(voxel_ridge_list, dim=0)
backbone, clip_voxels, blurry_image_enc_ = model.backbone(voxel_ridge)
if clip_scale>0:
clip_voxels_norm = nn.functional.normalize(clip_voxels.flatten(1), dim=-1)
clip_target_norm = nn.functional.normalize(clip_target.flatten(1), dim=-1)
# if use_prior:
# loss_prior, prior_out = model.diffusion_prior(text_embed=backbone, image_embed=clip_target)
# loss_prior_total += loss_prior.item()
# loss_prior *= prior_scale
# loss += loss_prior
# recon_cossim += nn.functional.cosine_similarity(prior_out, clip_target).mean().item()
# recon_mse += mse(prior_out, clip_target).item()
logits = model.RRClassifier(backbone.flatten(1))
#print(logits.shape, torch.argmax(semantic_cluster_onehot[images_indexes[f"subj0{s}_iter{train_i}"]], dim=1).shape)
#print(logits, torch.argmax(semantic_cluster_onehot[images_indexes[f"subj0{s}_iter{train_i}"]], dim=1))
loss_RR = nn.functional.cross_entropy(logits, torch.argmax(semantic_cluster_onehot[images_indexes[f"subj0{s}_iter{train_i}"]], dim=1).to(logits.device))
#print("backbone.shape",backbone.shape, "clip_voxels.shape",clip_voxels.shape, "blurry_image_enc_[0].shape",blurry_image_enc_[0].shape, "blurry_image_enc_[1].shape",blurry_image_enc_[1].shape)
#something
loss_RR_total += loss_RR.item()
loss += loss_RR
if clip_scale>0:
if epoch < int(mixup_pct * num_epochs):
loss_clip = utils.mixco_nce(
clip_voxels_norm,
clip_target_norm,
temp=.006,
perm=perm, betas=betas, select=select)
else:
epoch_temp = soft_loss_temps[epoch-int(mixup_pct*num_epochs)]
loss_clip = utils.soft_clip_loss(
clip_voxels_norm,
clip_target_norm,
temp=epoch_temp)
loss_clip_total += loss_clip.item()
loss_clip *= clip_scale
loss += loss_clip
if blurry_recon:
image_enc_pred, transformer_feats = blurry_image_enc_
image_enc = autoenc.encode(2*image-1).latent_dist.mode() * 0.18215
loss_blurry = l1(image_enc_pred, image_enc)
loss_blurry_total += loss_blurry.item()
if epoch < int(mixup_pct * num_epochs):
image_enc_shuf = image_enc[perm]
betas_shape = [-1] + [1]*(len(image_enc.shape)-1)
image_enc[select] = image_enc[select] * betas[select].reshape(*betas_shape) + \
image_enc_shuf[select] * (1 - betas[select]).reshape(*betas_shape)
image_norm = (image - mean)/std
image_aug = (blur_augs(image) - mean)/std
_, cnx_embeds = cnx(image_norm)
_, cnx_aug_embeds = cnx(image_aug)
cont_loss = utils.soft_cont_loss(
nn.functional.normalize(transformer_feats.reshape(-1, transformer_feats.shape[-1]), dim=-1),
nn.functional.normalize(cnx_embeds.reshape(-1, cnx_embeds.shape[-1]), dim=-1),
nn.functional.normalize(cnx_aug_embeds.reshape(-1, cnx_embeds.shape[-1]), dim=-1),
temp=0.2)
loss_blurry_cont_total += cont_loss.item()
loss += (loss_blurry + 0.1*cont_loss) * blur_scale #/.18215
if clip_scale>0:
# forward and backward top 1 accuracy
labels = torch.arange(len(clip_voxels_norm)).to(clip_voxels_norm.device)
fwd_percent_correct += utils.topk(utils.batchwise_cosine_similarity(clip_voxels_norm, clip_target_norm), labels, k=1).item()
bwd_percent_correct += utils.topk(utils.batchwise_cosine_similarity(clip_target_norm, clip_voxels_norm), labels, k=1).item()
if blurry_recon:
with torch.no_grad():
# only doing pixcorr eval on a subset of the samples per batch because its costly & slow to compute autoenc.decode()
random_samps = np.random.choice(np.arange(len(image)), size=len(image)//5, replace=False)
blurry_recon_images = (autoenc.decode(image_enc_pred[random_samps]/0.18215).sample/ 2 + 0.5).clamp(0,1)
pixcorr = utils.pixcorr(image[random_samps], blurry_recon_images)
blurry_pixcorr += pixcorr.item()
class_precisions_1 += classPrecision(logits, semantic_cluster_onehot[images_indexes[f"subj0{s}_iter{train_i}"]])
class_precisions_5 += classPrecision(logits, semantic_cluster_onehot[images_indexes[f"subj0{s}_iter{train_i}"]], 5)
class_precisions_10 += classPrecision(logits, semantic_cluster_onehot[images_indexes[f"subj0{s}_iter{train_i}"]], 10)
utils.check_loss(loss)
accelerator.backward(loss)
optimizer.step()
losses.append(loss.item())
lrs.append(optimizer.param_groups[0]['lr'])
if lr_scheduler_type is not None:
lr_scheduler.step()
model.eval()
if local_rank==0:
with torch.no_grad(), torch.cuda.amp.autocast(dtype=data_type):
for test_i, (behav, past_behav, future_behav, old_behav) in enumerate(test_dl):
# all test samples should be loaded per batch such that test_i should never exceed 0
assert len(behav) == num_test
## Average same-image repeats ##
if test_image is None:
voxel = voxels[f'subj0{subj}'][behav[:,0,5].cpu().long()].unsqueeze(1)
image = behav[:,0,0].cpu().long()
unique_image, sort_indices = torch.unique(image, return_inverse=True)
for im in unique_image:
locs = torch.where(im == image)[0]
if len(locs)==1:
locs = locs.repeat(3)
elif len(locs)==2:
locs = locs.repeat(2)[:3]
assert len(locs)==3
if test_image is None:
test_image = torch.Tensor(images[im][None])
test_voxel = voxel[locs][None]
else:
test_image = torch.vstack((test_image, torch.Tensor(images[im][None])))
test_voxel = torch.vstack((test_voxel, voxel[locs][None]))
loss=0.
test_indices = torch.arange(len(test_voxel))[:300]
voxel = test_voxel[test_indices].to(device)
image = test_image[test_indices].to(device)
assert len(image) == 300
clip_target = clip_img_embedder(image.float())
for rep in range(3):
voxel_ridge = model.ridge(voxel[:,rep],0) # 0th index of subj_list
backbone0, clip_voxels0, blurry_image_enc_ = model.backbone(voxel_ridge)
logits0 = model.RRClassifier(backbone0.flatten(1))
if rep==0:
clip_voxels = clip_voxels0
backbone = backbone0
logits = logits0
else:
clip_voxels += clip_voxels0
backbone += backbone0
logits += logits0
clip_voxels /= 3
backbone /= 3
logits /= 3
print(logits.shape, torch.argmax(semantic_cluster_onehot[test_indices], dim=1).shape)
RR_loss = nn.functional.cross_entropy(logits, torch.argmax(semantic_cluster_onehot[test_indices], dim=1).to(logits.device))
test_loss_RR_total += RR_loss.item()
loss += RR_loss
if clip_scale>0:
clip_voxels_norm = nn.functional.normalize(clip_voxels.flatten(1), dim=-1)
clip_target_norm = nn.functional.normalize(clip_target.flatten(1), dim=-1)
# for some evals, only doing a subset of the samples per batch because of computational cost
random_samps = np.random.choice(np.arange(len(image)), size=len(image)//5, replace=False)
# if use_prior:
# loss_prior, contaminated_prior_out = model.diffusion_prior(text_embed=backbone[random_samps], image_embed=clip_target[random_samps])
# test_loss_prior_total += loss_prior.item()
# loss_prior *= prior_scale
# loss += loss_prior
if clip_scale>0:
loss_clip = utils.soft_clip_loss(
clip_voxels_norm,
clip_target_norm,
temp=.006)
test_loss_clip_total += loss_clip.item()
loss_clip = loss_clip * clip_scale
loss += loss_clip
if blurry_recon:
image_enc_pred, _ = blurry_image_enc_
blurry_recon_images = (autoenc.decode(image_enc_pred[random_samps]/0.18215).sample / 2 + 0.5).clamp(0,1)
pixcorr = utils.pixcorr(image[random_samps], blurry_recon_images)
test_blurry_pixcorr += pixcorr.item()
if clip_scale>0:
# forward and backward top 1 accuracy
labels = torch.arange(len(clip_voxels_norm)).to(clip_voxels_norm.device)
test_fwd_percent_correct += utils.topk(utils.batchwise_cosine_similarity(clip_voxels_norm, clip_target_norm), labels, k=1).item()
test_bwd_percent_correct += utils.topk(utils.batchwise_cosine_similarity(clip_target_norm, clip_voxels_norm), labels, k=1).item()
test_class_precisions_1 += classPrecision(logits, semantic_cluster_onehot[test_indices])
test_class_precisions_5 += classPrecision(logits, semantic_cluster_onehot[test_indices], 5)
test_class_precisions_10 += classPrecision(logits, semantic_cluster_onehot[test_indices], 10)
utils.check_loss(loss)
test_losses.append(loss.item())
assert (test_i+1) == 1
logs = {"train/loss": np.mean(losses[-(train_i+1):]),
"test/loss": np.mean(test_losses[-(test_i+1):]),
"train/lr": lrs[-1],
"train/num_steps": len(losses),
"test/num_steps": len(test_losses),
"train/fwd_pct_correct": fwd_percent_correct / (train_i + 1),
"train/bwd_pct_correct": bwd_percent_correct / (train_i + 1),
"test/test_fwd_pct_correct": test_fwd_percent_correct / (test_i + 1),
"test/test_bwd_pct_correct": test_bwd_percent_correct / (test_i + 1),
"train/loss_clip_total": loss_clip_total / (train_i + 1),
"train/loss_blurry_total": loss_blurry_total / (train_i + 1),
"train/loss_blurry_cont_total": loss_blurry_cont_total / (train_i + 1),
"test/loss_clip_total": test_loss_clip_total / (test_i + 1),
"train/blurry_pixcorr": blurry_pixcorr / (train_i + 1),
"test/blurry_pixcorr": test_blurry_pixcorr / (test_i + 1),
"train/recon_cossim": recon_cossim / (train_i + 1),
"test/recon_cossim": test_recon_cossim / (test_i + 1),
"train/recon_mse": recon_mse / (train_i + 1),
"test/recon_mse": test_recon_mse / (test_i + 1),
"train/loss_prior": loss_prior_total / (train_i + 1),
"test/loss_prior": test_loss_prior_total / (test_i + 1),
"train/loss_RR": loss_RR_total / (train_i + 1),
"test/loss_RR": test_loss_RR_total / (test_i + 1),
"train/class_precisions_1": class_precisions_1 / (train_i + 1),
"test/class_precisions_1": test_class_precisions_1 / (test_i + 1),
"train/class_precisions_5": class_precisions_5 / (train_i + 1),
"test/class_precisions_5": test_class_precisions_5 / (test_i + 1),
"train/class_precisions_10": class_precisions_10 / (train_i + 1),
"test/class_precisions_10": test_class_precisions_10 / (test_i + 1),
}
# if finished training, save jpg recons if they exist
if (epoch == num_epochs-1) or (epoch % ckpt_interval == 0):
if blurry_recon:
image_enc = autoenc.encode(2*image[:4]-1).latent_dist.mode() * 0.18215
# transform blurry recon latents to images and plot it
fig, axes = plt.subplots(1, 8, figsize=(10, 4))
jj=-1
for j in [0,1,2,3]:
jj+=1
axes[jj].imshow(utils.torch_to_Image((autoenc.decode(image_enc[[j]]/0.18215).sample / 2 + 0.5).clamp(0,1)))
axes[jj].axis('off')
jj+=1
axes[jj].imshow(utils.torch_to_Image((autoenc.decode(image_enc_pred[[j]]/0.18215).sample / 2 + 0.5).clamp(0,1)))
axes[jj].axis('off')
if wandb_log:
logs[f"test/blur_recons"] = wandb.Image(fig, caption=f"epoch{epoch:03d}")
plt.close()
else:
plt.show()
progress_bar.set_postfix(**logs)
if wandb_log: wandb.log(logs)
# Save model checkpoint and reconstruct
if (ckpt_saving) and (epoch % ckpt_interval == 0):
save_ckpt(f'last')
# wait for other GPUs to catch up if needed
accelerator.wait_for_everyone()
torch.cuda.empty_cache()
print("\n===Finished!===\n")
if ckpt_saving:
save_ckpt(f'last')
# In[ ]:
plt.plot(losses)
plt.show()
plt.plot(test_losses)
plt.show()
# In[ ]:
import wandb
wandb.login()
|