| | |
| | |
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | print("importing modules") |
| | import os |
| | import sys |
| | import json |
| | import argparse |
| | import numpy as np |
| | import time |
| | import random |
| | import string |
| | import h5py |
| | from tqdm import tqdm |
| | import webdataset as wds |
| | from PIL import Image |
| | import pandas as pd |
| | import nibabel as nib |
| | import nilearn |
| |
|
| | import matplotlib.pyplot as plt |
| | import torch |
| | import torch.nn as nn |
| | from torchvision import transforms |
| |
|
| | |
| | torch.backends.cuda.matmul.allow_tf32 = True |
| |
|
| | import utils |
| | from utils import load_preprocess_betas, resample, applyxfm, apply_thresh, resample_betas |
| |
|
| | |
| | import importlib.util |
| | parent_utils_path = "/home/ri4541/mindeye_preproc/analysis/utils.py" |
| | spec = importlib.util.spec_from_file_location("utils", parent_utils_path) |
| | preproc = importlib.util.module_from_spec(spec) |
| | parent_dir = os.path.dirname(parent_utils_path) |
| | if parent_dir not in sys.path: |
| | sys.path.append(parent_dir) |
| | spec.loader.exec_module(preproc) |
| |
|
| | if utils.is_interactive(): |
| | from IPython.display import clear_output |
| | get_ipython().run_line_magic('load_ext', 'autoreload') |
| | |
| | get_ipython().run_line_magic('autoreload', '2') |
| | |
| | seed = utils.get_slurm_seed() |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | if utils.is_interactive(): |
| | sub = "sub-005" |
| | session = "ses-03" |
| | task = 'C' |
| | func_task_name = 'C' |
| | else: |
| | sub = os.environ["sub"] |
| | session = os.environ["session"] |
| | task = os.environ["task"] |
| |
|
| | if session == "all": |
| | ses_list = ["ses-01", "ses-02", "ses-03"] |
| | design_ses_list = ["ses-01", "ses-02", "ses-03"] |
| | else: |
| | ses_list = [session] |
| | design_ses_list = [session] |
| | |
| | task_name = f"_task-{task}" if task != 'study' else '' |
| | resample_voxel_size = False |
| | resample_post_glmsingle = False |
| | load_from_resampled_file = False |
| | |
| | train_test_split = 'MST' |
| | remove_close_to_MST = False |
| | remove_random_n = False |
| |
|
| | if remove_close_to_MST or remove_random_n: |
| | assert remove_close_to_MST != remove_random_n |
| |
|
| | n_to_remove = 0 |
| | if remove_random_n: |
| | assert train_test_split == 'MST' |
| | n_to_remove = 150 |
| | |
| | if resample_voxel_size: |
| | |
| | resampled_vox_size = 2.5 |
| | resample_method = "sinc" |
| | |
| | |
| | vox_dim_str = str(resampled_vox_size).replace('.', '_') |
| | resampled_suffix = f"resampled_{vox_dim_str}mm_{resample_method}" |
| | mask_resampled_suffix = resampled_suffix |
| | if resample_post_glmsingle: |
| | resampled_suffix += '_postglmsingle' |
| | else: |
| | resampled_suffix += '_preglmsingle' |
| |
|
| |
|
| | |
| |
|
| |
|
| | session_label = preproc.get_session_label(ses_list) |
| | print('session label:', session_label) |
| | n_runs, _ = preproc.get_runs_per_session(sub, session, ses_list) |
| |
|
| |
|
| | |
| |
|
| |
|
| | if utils.is_interactive(): |
| | glmsingle_path = f"/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/glmsingle_{sub}_{session_label}_task-{task}" |
| | else: |
| | glmsingle_path = os.environ["glmsingle_path"] |
| | |
| | designdir = "/home/ri4541/real_time_mindEye2" |
| | print(glmsingle_path) |
| |
|
| | if resample_voxel_size: |
| | |
| | |
| | if resample_post_glmsingle: |
| | |
| | orig_glmsingle_path = glmsingle_path |
| | glmsingle_path += f"_{resampled_suffix}" |
| | print("resampled glmsingle path:", glmsingle_path) |
| | if load_from_resampled_file: |
| | |
| | assert os.path.exists(glmsingle_path) |
| | else: |
| | |
| | os.makedirs(glmsingle_path,exist_ok=True) |
| | else: |
| | |
| | glmsingle_path += f"_{resampled_suffix}" |
| | print("glmsingle path:", glmsingle_path) |
| |
|
| | assert os.path.exists(glmsingle_path) |
| | print("glmsingle path exists!") |
| |
|
| |
|
| | |
| |
|
| |
|
| | data, starts, images, is_new_run, image_names, unique_images, len_unique_images = preproc.load_design_files( |
| | sub=sub, |
| | session=session, |
| | func_task_name=task, |
| | designdir=designdir, |
| | design_ses_list=design_ses_list |
| | ) |
| |
|
| | if sub == 'sub-001': |
| | if session == 'ses-01': |
| | assert image_names[0] == 'images/image_686_seed_1.png' |
| | elif session in ('ses-02', 'all'): |
| | assert image_names[0] == 'all_stimuli/special515/special_40840.jpg' |
| | elif session == 'ses-03': |
| | assert image_names[0] == 'all_stimuli/special515/special_69839.jpg' |
| | elif session == 'ses-04': |
| | assert image_names[0] == 'all_stimuli/rtmindeye_stimuli/image_686_seed_1.png' |
| | elif sub == 'sub-003': |
| | assert image_names[0] == 'all_stimuli/rtmindeye_stimuli/image_686_seed_1.png' |
| |
|
| | unique_images = np.unique(image_names.astype(str)) |
| | unique_images = unique_images[(unique_images!="nan")] |
| | len_unique_images = len(unique_images) |
| | print("n_runs",n_runs) |
| |
|
| | if (sub == 'sub-001' and session == 'ses-04') or (sub == 'sub-003' and session == 'ses-01'): |
| | assert len(unique_images) == 851 |
| |
|
| | print(image_names[:4]) |
| | print(starts[:4]) |
| | print(is_new_run[:4]) |
| |
|
| | if remove_random_n: |
| | |
| | |
| | |
| | |
| | |
| | np.random.seed(seed) |
| | options_to_remove = [x for x in set(image_names) if str(x) != 'nan' and x != 'blank.jpg' and 'MST_pairs' not in x and 'special515' not in x and list(image_names).count(x)==1] |
| | options_to_remove_special515 = [x for x in set(image_names) if str(x) != 'nan' and x != 'blank.jpg' and 'MST_pairs' not in x and 'special515' in x and list(image_names).count(x)>1] |
| | imgs_to_remove = np.random.choice(options_to_remove, size=99, replace=False) |
| | imgs_to_remove = np.append(imgs_to_remove, np.random.choice(options_to_remove_special515, size=17, replace=False)) |
| |
|
| | image_idx = np.array([]) |
| | vox_image_names = np.array([]) |
| | all_MST_images = dict() |
| | for i, im in enumerate(image_names): |
| | |
| | if im == "blank.jpg": |
| | i+=1 |
| | continue |
| | if str(im) == "nan": |
| | i+=1 |
| | continue |
| | vox_image_names = np.append(vox_image_names, im) |
| | if remove_close_to_MST: |
| | if "closest_pairs" in im: |
| | i+=1 |
| | continue |
| | elif remove_random_n: |
| | if im in imgs_to_remove: |
| | i+=1 |
| | continue |
| | |
| | image_idx_ = np.where(im==unique_images)[0].item() |
| | image_idx = np.append(image_idx, image_idx_) |
| | |
| | if (sub == 'sub-001' and session == 'ses-04') or (sub == 'sub-003' and session == 'ses-01'): |
| | import re |
| | if ('w_' in im or 'paired_image_' in im or re.match(r'all_stimuli/rtmindeye_stimuli/\d{1,2}_\d{1,3}\.png$', im) or re.match(r'images/\d{1,2}_\d{1,3}\.png$', im)): |
| | |
| | |
| | all_MST_images[i] = im |
| | i+=1 |
| | elif 'MST' in im: |
| | all_MST_images[i] = im |
| | i+=1 |
| | |
| | image_idx = torch.Tensor(image_idx).long() |
| | |
| | |
| | |
| |
|
| | unique_MST_images = np.unique(list(all_MST_images.values())) |
| |
|
| | MST_ID = np.array([], dtype=int) |
| | if remove_close_to_MST: |
| | close_to_MST_idx = np.array([], dtype=int) |
| | if remove_random_n: |
| | random_n_idx = np.array([], dtype=int) |
| |
|
| | vox_idx = np.array([], dtype=int) |
| | j=0 |
| | for i, im in enumerate(image_names): |
| | |
| | if im == "blank.jpg": |
| | i+=1 |
| | continue |
| | if str(im) == "nan": |
| | i+=1 |
| | continue |
| | if remove_close_to_MST: |
| | if "closest_pairs" in im: |
| | close_to_MST_idx = np.append(close_to_MST_idx, i) |
| | i+=1 |
| | continue |
| | if remove_random_n: |
| | if im in imgs_to_remove: |
| | vox_idx = np.append(vox_idx, j) |
| | i+=1 |
| | j+=1 |
| | continue |
| | j+=1 |
| | curr = np.where(im == unique_MST_images) |
| | |
| | if curr[0].size == 0: |
| | MST_ID = np.append(MST_ID, np.array(len(unique_MST_images))) |
| | else: |
| | MST_ID = np.append(MST_ID, curr) |
| | |
| | assert len(MST_ID) == len(image_idx) |
| | |
| | |
| | print(MST_ID.shape) |
| | if (sub == 'sub-001' and session == 'ses-04') or (sub == 'sub-003' and session == 'ses-01'): |
| | assert len(all_MST_images) == 100 |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | import imageio.v2 as imageio |
| | resize_transform = transforms.Resize((224, 224)) |
| | MST_images = [] |
| | images = None |
| | for im_name in tqdm(image_idx): |
| | if sub == 'sub-001' and session == 'ses-01': |
| | image_file = f"all_stimuli/rtmindeye_stimuli/{unique_images[im_name]}" |
| | else: |
| | image_file = f"{unique_images[im_name]}" |
| | im = imageio.imread(image_file) |
| | im = torch.Tensor(im / 255).permute(2,0,1) |
| | im = resize_transform(im.unsqueeze(0)) |
| | if images is None: |
| | images = im |
| | else: |
| | images = torch.vstack((images, im)) |
| | if (sub == 'sub-001' and session == 'ses-04') or (sub == 'sub-003' and session == 'ses-01'): |
| | if ('w_' in image_file or 'paired_image_' in image_file or re.match(r'all_stimuli/rtmindeye_stimuli/\d{1,2}_\d{1,3}\.png$', image_file) or re.match(r'all_stimuli/rtmindeye_stimuli/images/\d{1,2}_\d{1,3}\.png$', image_file)): |
| | MST_images.append(True) |
| | else: |
| | MST_images.append(False) |
| | else: |
| | if ("MST_pairs" in image_file): |
| | MST_images.append(True) |
| | else: |
| | MST_images.append(False) |
| |
|
| | print("images", images.shape) |
| | MST_images = np.array(MST_images) |
| | print("MST_images", len(MST_images)) |
| | if (sub == 'sub-001' and session == 'ses-04') or (sub == 'sub-003' and session == 'ses-01'): |
| | assert len(MST_images[MST_images==True]) == 100 |
| | print("MST_images==True", len(MST_images[MST_images==True])) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| |
|
| | assert unique_MST_images.shape[0] % 2 == 0 |
| | MST_pairmate_names = unique_MST_images.reshape(int(unique_MST_images.shape[0]/2),2) |
| | |
| |
|
| | MST_pairmate_indices = np.empty(shape=MST_pairmate_names.shape, dtype=int) |
| | for p, pair in enumerate(MST_pairmate_names): |
| | for i, im in enumerate(pair): |
| | MST_pairmate_indices[p][i] = np.where(np.isin(list(all_MST_images.values()), im))[0][0] |
| | |
| | print(MST_pairmate_indices.shape, MST_pairmate_indices) |
| |
|
| |
|
| | |
| |
|
| |
|
| | if (sub == 'sub-001' and session in ('ses-02', 'ses-03', 'all')): |
| | |
| | |
| | MST_pairs = utils.find_paired_indices(torch.tensor(MST_ID)) |
| | MST_pairs = np.array(sorted(MST_pairs[:-1], key=lambda x: x[0])) |
| |
|
| | |
| |
|
| | fig, ax = plt.subplots(1, 3, figsize=(10,4)) |
| | fig.suptitle('Sample MST pairs') |
| |
|
| | ax[0].imshow(images[MST_pairs[-1][0]].permute(1,2,0).numpy()) |
| | ax[0].set_title(f"Trial 0") |
| |
|
| | ax[1].imshow(images[MST_pairs[-1][1]].permute(1,2,0).numpy()) |
| | ax[1].set_title(f"Trial 1") |
| |
|
| | ax[2].imshow(images[MST_pairs[-1][2]].permute(1,2,0).numpy()) |
| | ax[2].set_title(f"Trial 2") |
| |
|
| | plt.setp(ax, xticks=[], yticks=[]) |
| | plt.tight_layout() |
| | plt.show() |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | pairs = utils.find_paired_indices(image_idx) |
| | pairs = sorted(pairs, key=lambda x: x[0]) |
| |
|
| | fig, axes = plt.subplots(1, 3, figsize=(6, 2)) |
| | for i, ax in enumerate(axes): |
| | ax.imshow(images[i].permute(1, 2, 0).numpy()) |
| | ax.set_title(f"Trial {i}") |
| | ax.axis("off") |
| |
|
| | plt.tight_layout() |
| | |
| | |
| | plt.show() |
| |
|
| |
|
| | |
| |
|
| |
|
| | p=0 |
| |
|
| | |
| | fig, ax = plt.subplots(1, 2, figsize=(10,8)) |
| |
|
| | ax[0].imshow(images[pairs[p][0]].permute(1,2,0).numpy()) |
| | ax[0].set_title(f"Repeat 1") |
| |
|
| | ax[1].imshow(images[pairs[p][1]].permute(1,2,0).numpy()) |
| | ax[1].set_title(f"Repeat 2") |
| |
|
| | plt.setp(ax, xticks=[], yticks=[]) |
| | plt.tight_layout() |
| | plt.show() |
| |
|
| |
|
| | |
| |
|
| |
|
| | if resample_voxel_size: |
| | from nilearn.masking import apply_mask, unmask |
| | ref_name = f'{glmsingle_path}/boldref_resampled.nii.gz' |
| | omat_name = f'{glmsingle_path}/boldref_omat' |
| |
|
| |
|
| | |
| |
|
| |
|
| | def get_image_pairs(sub, session, func_task_name, designdir): |
| | """Loads design files and processes image pairs for a given session.""" |
| | _, _, _, _, image_names, unique_images, _ = preproc.load_design_files( |
| | sub=sub, |
| | session=session, |
| | func_task_name=func_task_name, |
| | designdir=designdir, |
| | design_ses_list=[session] |
| | ) |
| | return utils.process_images(image_names, unique_images) |
| |
|
| |
|
| | |
| |
|
| |
|
| | from collections import defaultdict |
| |
|
| | all_dicts = [] |
| | for s_idx, s in enumerate(ses_list): |
| | im, vo, _ = get_image_pairs(sub, s, func_task_name, designdir) |
| | assert len(im) == len(vo) |
| | all_dicts.append({k:v for k,v in enumerate(vo)}) |
| |
|
| | assert session_label == 'ses-03' |
| | image_to_indices = defaultdict(lambda: [[] for _ in range(len(ses_list))]) |
| | for ses_idx, idx_to_name in enumerate(all_dicts): |
| | for idx, name in idx_to_name.items(): |
| | image_to_indices[name][ses_idx].append(idx) |
| | |
| | image_to_indices = dict(image_to_indices) |
| |
|
| |
|
| | |
| |
|
| |
|
| | from nilearn.plotting import plot_roi |
| | assert sub == 'sub-005' and session == "ses-03" |
| | print('loading brain mask') |
| | |
| | avg_mask = nib.load('/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/glmsingle_sub-005_task-C/sub-005_final_brain.nii.gz') |
| | final_mask = nib.load('/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/glmsingle_sub-005_task-C/sub-005_final_mask.nii.gz') |
| |
|
| | |
| | dimsize=avg_mask.header.get_zooms() |
| | affine_mat = avg_mask.affine |
| | brain=avg_mask.get_fdata() |
| | xyz=brain.shape |
| |
|
| | print('Mask dimensions:', dimsize) |
| | print('') |
| | print('Affine:') |
| | print(affine_mat) |
| | print('') |
| | print(f'There are {int(np.sum(brain))} voxels in the included brain mask\n') |
| |
|
| | plot_roi(final_mask, bg_img=avg_mask) |
| | plt.show() |
| |
|
| |
|
| | |
| |
|
| |
|
| | union_mask = np.load('/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/glmsingle_sub-005_task-C/union_mask_from_ses-01-02.npy') |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | vox = None |
| | needs_postprocessing = False |
| | params = (session, ses_list, remove_close_to_MST, image_names, remove_random_n, vox_idx) |
| |
|
| | if resample_post_glmsingle == True: |
| | glm_save_path_resampled = f"{glmsingle_path}/vox_resampled.nii.gz" |
| | if load_from_resampled_file == True: |
| | |
| | vox = nib.load(glm_save_path_resampled) |
| | else: |
| | |
| | assert os.path.exists(ref_name) and os.path.exists(omat_name), "need to generate the boldref and omat separately since we don't have access to the functional data here; either do so using flirt on the command line or copy over the glmsingle resampled outputs" |
| | vox = load_preprocess_betas(orig_glmsingle_path, *params) |
| | vox = resample_betas(orig_glmsingle_path, sub, session, task_name, vox, glmsingle_path, glm_save_path_resampled, ref_name, omat_name) |
| | needs_postprocessing = True |
| |
|
| | if vox is None: |
| | |
| | vox = load_preprocess_betas(glmsingle_path, *params) |
| |
|
| | if needs_postprocessing == True: |
| | vox = nilearn.masking.apply_mask(vox, avg_mask) |
| | vox = vox.reshape(-1, vox.shape[-1]) |
| | print(vox.shape) |
| |
|
| | assert len(vox) == len(image_idx) |
| |
|
| |
|
| | |
| |
|
| |
|
| | ses_mask = nib.load(f'/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/glmsingle_sub-005_{session_label}_task-C/sub-005_{session_label}_task-C_brain.nii.gz') |
| | assert np.all(ses_mask.affine == final_mask.affine) |
| | assert np.all(ses_mask.shape == final_mask.shape) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | v = nilearn.masking.unmask(vox, ses_mask) |
| | |
| | vox = nilearn.masking.apply_mask(v, final_mask) |
| | print(vox.shape) |
| | vox = vox[:, union_mask] |
| | print("applied union roi mask") |
| | print(vox.shape) |
| |
|
| |
|
| | |
| |
|
| |
|
| | pairs_homog = np.array([[p[0], p[1]] for p in pairs]) |
| |
|
| |
|
| | |
| |
|
| |
|
| | same_corrs = [] |
| | diff_corrs = [] |
| | for isamp, samp in enumerate(vox[pairs_homog]): |
| | avg_same_img = [] |
| | for i in range(samp.shape[0]): |
| | for j in range(i, samp.shape[0]): |
| | if i != j: |
| | avg_same_img.append(np.array([np.corrcoef(samp[i, :], samp[j, :])[0,1]])) |
| | |
| | same_corrs.append(np.mean(avg_same_img)) |
| | |
| | avg_diff_img = [] |
| | for isamp_j, samp_j in enumerate(vox[pairs_homog]): |
| | if isamp_j != isamp: |
| | for i in range(samp_j.shape[0]): |
| | for j in range(i, samp_j.shape[0]): |
| | if i != j: |
| | avg_diff_img.append(np.array([np.corrcoef(samp[i, :], samp_j[j, :])[0,1]])) |
| | |
| | |
| | diff_corrs.append(np.mean(avg_diff_img)) |
| |
|
| |
|
| | print(len(same_corrs), len(diff_corrs)) |
| | same_corrs = np.array(same_corrs) |
| | diff_corrs = np.array(diff_corrs) |
| |
|
| |
|
| | plt.figure(figsize=(5,4)) |
| | plt.title(f"{sub}_{session} same/diff Pearson corr.") |
| | plt.plot(np.sort(same_corrs),c='blue',label='same') |
| | plt.plot(np.sort(diff_corrs),c='cyan',label='diff') |
| | plt.axhline(0,c='k',ls='--') |
| | plt.legend() |
| | plt.xlabel("sample") |
| | plt.ylabel("Pearson R") |
| | plt.show() |
| |
|
| |
|
| | |
| |
|
| |
|
| | vox_pairs = utils.zscore(vox[pairs_homog]) |
| | plt.figure(figsize=(5,4)) |
| | plt.title(f"{sub}_{session} same minus diff difference Pearson corr.") |
| | plt.plot(np.sort(same_corrs) - np.sort(diff_corrs),c='cyan',label='difference') |
| | plt.axhline(0,c='k',ls='--') |
| | plt.legend() |
| | plt.xlabel("sample") |
| | plt.ylabel("Pearson R") |
| | plt.show() |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | utils.seed_everything(seed) |
| | MST_idx = np.array([v for k,v in image_to_indices.items() if 'MST_pairs' in k]) |
| | |
| | |
| | train_image_indices = np.where(MST_images == False)[0] |
| | test_image_indices = np.where(MST_images == True)[0] |
| | print(len(train_image_indices), len(test_image_indices)) |
| |
|
| |
|
| | |
| |
|
| |
|
| | train_mean = np.mean(vox[train_image_indices],axis=0) |
| | train_std = np.std(vox[train_image_indices],axis=0) |
| | |
| | |
| | |
| |
|
| | vox = utils.zscore(vox,train_mean=train_mean,train_std=train_std) |
| | print("voxels have been zscored") |
| | print(vox[:,0].mean(), vox[:,0].std()) |
| | print("vox", vox.shape) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | images = torch.Tensor(images) |
| | vox = torch.Tensor(vox) |
| | assert len(images) == len(vox) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | from accelerate import Accelerator, DeepSpeedPlugin |
| |
|
| | 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.float32 |
| |
|
| | accelerator = Accelerator(split_batches=False) |
| | batch_size = 8 |
| |
|
| |
|
| | |
| |
|
| |
|
| | 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() |
| | global_batch_size = batch_size * num_devices |
| | print("global_batch_size", global_batch_size) |
| | if num_devices==0 or not distributed: num_devices = 1 |
| | num_workers = num_devices |
| | print(accelerator.state) |
| |
|
| | |
| | if accelerator.mixed_precision == "bf16": |
| | data_type = torch.bfloat16 |
| | elif accelerator.mixed_precision == "fp16": |
| | data_type = torch.float16 |
| | else: |
| | data_type = torch.float32 |
| |
|
| | print("distributed =",distributed, "num_devices =", num_devices, "local rank =", local_rank, "world size =", world_size, "data_type =", data_type) |
| | print = accelerator.print |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | |
| | if utils.is_interactive(): |
| | model_name = 'testing_MST' |
| | print("model_name:", model_name) |
| | |
| | |
| | |
| | |
| |
|
| | jupyter_args = f"--data_path=/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2 \ |
| | --model_name={model_name} \ |
| | --no-multi_subject --subj=1 --batch_size={batch_size} \ |
| | --hidden_dim=1024 --clip_scale=1. \ |
| | --no-blurry_recon --blur_scale=.5 \ |
| | --use_prior --prior_scale=30 \ |
| | --n_blocks=4 --max_lr=3e-4 --mixup_pct=.33 --num_epochs=30 --no-use_image_aug \ |
| | --ckpt_interval=999 --no-ckpt_saving --new_test \ |
| | --multisubject_ckpt=/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/train_logs/multisubject_subj01_1024hid_nolow_300ep" |
| | print(jupyter_args) |
| | jupyter_args = jupyter_args.split() |
| |
|
| |
|
| | |
| |
|
| |
|
| | parser = argparse.ArgumentParser(description="Model Training Configuration") |
| | parser.add_argument( |
| | "--model_name", type=str, default="testing", |
| | help="name of model, used for ckpt saving and wandb logging (if enabled)", |
| | ) |
| | parser.add_argument( |
| | "--data_path", type=str, default="/weka/proj-fmri/shared/natural-scenes-dataset", |
| | help="Path to where NSD data is stored / where to download it to", |
| | ) |
| | 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=0, |
| | help="Number of training sessions to include (if multi_subject, this variable doesnt matter)", |
| | ) |
| | parser.add_argument( |
| | "--use_prior",action=argparse.BooleanOptionalAction,default=False, |
| | 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=32, |
| | help="Batch size can be increased by 10x if only training v2c and not diffusion diffuser", |
| | ) |
| | parser.add_argument( |
| | "--wandb_log",action=argparse.BooleanOptionalAction,default=False, |
| | help="whether to log to wandb", |
| | ) |
| | parser.add_argument( |
| | "--resume_from_ckpt",action=argparse.BooleanOptionalAction,default=False, |
| | help="if not using wandb and want to resume from a ckpt", |
| | ) |
| | 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( |
| | "--low_mem",action=argparse.BooleanOptionalAction,default=False, |
| | help="whether to preload images to cpu to speed things up but consume more memory", |
| | ) |
| | 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=True, |
| | help="whether to use image augmentation", |
| | ) |
| | parser.add_argument( |
| | "--num_epochs",type=int,default=120, |
| | 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=2, |
| | ) |
| | parser.add_argument( |
| | "--hidden_dim",type=int,default=1024, |
| | ) |
| | parser.add_argument( |
| | "--seq_past",type=int,default=0, |
| | ) |
| | parser.add_argument( |
| | "--seq_future",type=int,default=0, |
| | ) |
| | 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-4, |
| | ) |
| |
|
| | if utils.is_interactive(): |
| | args = parser.parse_args(jupyter_args) |
| | else: |
| | args = parser.parse_args() |
| |
|
| | |
| | for attribute_name in vars(args).keys(): |
| | globals()[attribute_name] = getattr(args, attribute_name) |
| | |
| | outdir = os.path.abspath(f'/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/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 |
| | import kornia.augmentation as K |
| | 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"], |
| | ) |
| | |
| | blur_augment = K.RandomGaussianBlur(kernel_size=(21, 21), sigma=(51.0, 51.0), p=1.) |
| | |
| | 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) |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | if ckpt_saving: |
| | |
| | if 'MST' in model_name: |
| | eval_dir = os.environ["eval_dir"] |
| | print('saving MST info in', eval_dir) |
| | |
| | if not os.path.exists(eval_dir): |
| | os.mkdir(eval_dir) |
| |
|
| | np.save(f"{eval_dir}/MST_ID.npy", MST_ID) |
| | np.save(f"{eval_dir}/MST_pairmate_indices.npy", MST_pairmate_indices) |
| |
|
| | if remove_random_n: |
| | np.save(f"{eval_dir}/imgs_to_remove.npy", imgs_to_remove) |
| |
|
| | np.save(f"{eval_dir}/train_image_indices.npy", train_image_indices) |
| | np.save(f"{eval_dir}/test_image_indices.npy", test_image_indices) |
| | np.save(f"{eval_dir}/images.npy", images) |
| | np.save(f"{eval_dir}/vox.npy", vox) |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | 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 = len(train_image_indices) |
| |
|
| | 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) |
| |
|
| |
|
| | |
| |
|
| |
|
| | train_data = {} |
| | train_dl = {} |
| |
|
| | train_data[f'subj0{subj}'] = torch.utils.data.TensorDataset(torch.tensor(train_image_indices)) |
| | test_data = torch.utils.data.TensorDataset(torch.tensor(test_image_indices)) |
| |
|
| |
|
| | |
| |
|
| |
|
| | num_voxels = {} |
| | voxels = {} |
| | for s in subj_list: |
| | print(f"Training with {num_sessions} sessions") |
| | train_dl = torch.utils.data.DataLoader(train_data[f'subj0{s}'], batch_size=len(train_data), shuffle=False, drop_last=True, pin_memory=True) |
| | |
| | |
| | num_voxels_list.append(vox[0].shape[-1]) |
| | num_voxels[f'subj0{s}'] = vox[0].shape[-1] |
| | voxels[f'subj0{s}'] = vox |
| | print(f"num_voxels for subj0{s}: {num_voxels[f'subj0{s}']}") |
| |
|
| | print("Loaded all subj train dls and vox!\n") |
| |
|
| | |
| | if multi_subject: |
| | subj = subj_list[0] |
| | test_dl = torch.utils.data.DataLoader(test_data, batch_size=len(test_data), shuffle=False, drop_last=True, pin_memory=True) |
| |
|
| | print(f"Loaded test dl for subj{subj}!\n") |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | |
| | sys.path.append('generative_models/') |
| | import sgm |
| | from generative_models.sgm.modules.encoders.modules import FrozenOpenCLIPImageEmbedder |
| | |
| | |
| |
|
| | try: |
| | print(clip_img_embedder) |
| | except: |
| | 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 |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | model = utils.prepare_model_and_training( |
| | num_voxels_list=num_voxels_list, |
| | n_blocks=n_blocks, |
| | hidden_dim=hidden_dim, |
| | clip_emb_dim=clip_emb_dim, |
| | clip_seq_dim=clip_seq_dim, |
| | use_prior=use_prior, |
| | clip_scale=clip_scale |
| | ) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | b = torch.randn((2,1,num_voxels_list[0])) |
| | print(b.shape, model.ridge(b,0).shape) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | 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) |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | if use_prior: |
| | from models import * |
| |
|
| | |
| | out_dim = clip_emb_dim |
| | depth = 6 |
| | dim_head = 52 |
| | heads = clip_emb_dim//52 |
| | timesteps = 100 |
| |
|
| | prior_network = VersatileDiffusionPriorNetwork( |
| | 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) |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | 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} |
| | ]) |
| |
|
| | 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': |
| | if num_iterations_per_epoch==0: |
| | num_iterations_per_epoch=1 |
| | 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: |
| | 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) |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | epoch = 0 |
| | losses, test_losses, lrs = [], [], [] |
| | best_test_loss = 1e9 |
| | torch.cuda.empty_cache() |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | tag='last' |
| | |
| | outdir = "/scratch/gpfs/ri4541/MindEyeV2/src/mindeyev2/train_logs/sub-005_all_task-C_bs24_MST_rishab_MSTsplit_union_mask_finetune_0" |
| | print(f"\n---loading {outdir}/{tag}.pth ckpt---\n") |
| | checkpoint = torch.load(outdir+f'/{tag}.pth', map_location='cpu') |
| | state_dict = checkpoint['model_state_dict'] |
| | model.load_state_dict(state_dict, strict=True) |
| | del checkpoint |
| | print("ckpt loaded!") |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | def freeze_model(model): |
| | for param in model.parameters(): |
| | param.requires_grad = False |
| | return model |
| |
|
| | model = freeze_model(model) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| | model, optimizer, train_dl, lr_scheduler = accelerator.prepare(model, optimizer, train_dl, lr_scheduler) |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | model.eval() |
| | logs = {} |
| | if local_rank == 0: |
| | with torch.no_grad(), torch.cuda.amp.autocast(dtype=data_type): |
| | for i in range(1): |
| | for j in range(2): |
| | subset_indices = MST_idx[:, i, j].reshape(-1) |
| | subset_dataset = torch.utils.data.TensorDataset(torch.tensor(subset_indices)) |
| | subset_dl = torch.utils.data.DataLoader( |
| | subset_dataset, batch_size=len(MST_idx), shuffle=False, |
| | drop_last=True, pin_memory=True |
| | ) |
| |
|
| | |
| | test_fwd_percent_correct = 0 |
| | test_bwd_percent_correct = 0 |
| |
|
| | for test_i, behav in enumerate(subset_dl): |
| | behav = behav[0] |
| | loss = 0. |
| | image = images[behav.long().cpu()].to(device) |
| | voxel = vox[behav.long().cpu()] |
| | voxel = torch.Tensor(voxel).unsqueeze(1).to(device) |
| | clip_img_embedder = clip_img_embedder.to(device) |
| | clip_target = clip_img_embedder(image.float()) |
| |
|
| | voxel_ridge = model.ridge(voxel, 0) |
| | backbone, clip_voxels, blurry_image_enc_ = model.backbone(voxel_ridge) |
| |
|
| | clip_voxels_norm = torch.nn.functional.normalize(clip_voxels, dim=-1) |
| | clip_target_norm = torch.nn.functional.normalize(clip_target, dim=-1) |
| |
|
| | if clip_scale > 0: |
| | 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() |
| | print(test_fwd_percent_correct) |
| | print(test_bwd_percent_correct) |
| | logs.update({ |
| | f"subset_{i}_{j}_test/fwd_pct_correct": test_fwd_percent_correct / (test_i + 1), |
| | f"subset_{i}_{j}_test/bwd_pct_correct": test_bwd_percent_correct / (test_i + 1), |
| | }) |
| |
|
| | print("--- Full Dataset Evaluation ---") |
| | for k, v in logs.items(): |
| | print(f"{k}: {v:.4f}") |
| |
|
| |
|
| | |
| |
|
| |
|
| | top_k = 5 |
| |
|
| | for x in range(len(MST_idx)): |
| | |
| | y = torch.topk(utils.batchwise_cosine_similarity(clip_voxels_norm, clip_target_norm)[x], k=top_k).indices.to('cpu').tolist() |
| |
|
| | |
| | fig, axs = plt.subplots(1, top_k + 1, figsize=(3 * (top_k + 1), 3)) |
| | |
| | |
| | orig_img = utils.torch_to_Image(images[MST_idx[x]]) |
| | axs[0].imshow(orig_img) |
| | axs[0].set_title("Original") |
| | axs[0].axis("off") |
| |
|
| | |
| | for idx, i in enumerate(y): |
| | pred_img = utils.torch_to_Image(images[MST_idx[i]]) |
| | axs[idx + 1].imshow(pred_img) |
| | axs[idx + 1].set_title(f"Top {idx+1}") |
| | axs[idx + 1].axis("off") |
| | |
| | plt.tight_layout() |
| | plt.show() |
| |
|
| |
|
| | |
| |
|
| |
|
| | clip_voxels_norm.shape, clip_target_norm.shape |
| |
|
| |
|
| | |
| |
|
| |
|
| | len(test_data) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| |
|
| |
|
| | |
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | x = [im for im in image_names if str(im) not in ('blank.jpg', 'nan')] |
| | assert len(image_idx) == len(x) |
| | pairs = [] |
| | for i, p in enumerate(MST_pairmate_names): |
| | assert p[0] != p[1] |
| | pairs.append([utils.find_all_indices(x,p[0]), utils.find_all_indices(x,p[1])]) |
| |
|
| | pairs = np.array(pairs) |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | pairs[0][0][0] |
| |
|
| |
|
| | |
| |
|
| |
|
| | pairs[0] |
| |
|
| |
|
| | |
| |
|
| |
|
| | ix = 2 |
| | x[pairs[ix][0][0]], x[pairs[ix][0][1]], x[pairs[ix][1][0]], x[pairs[ix][1][1]] |
| |
|
| |
|
| | |
| |
|
| |
|
| | pairs.shape |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | def evaluate_mst_pairs(mst_pairs): |
| | score = 0 |
| | total = 0 |
| | |
| | with torch.no_grad(), torch.cuda.amp.autocast(dtype=data_type): |
| | for pair in tqdm(mst_pairs): |
| | voxel = vox[image_idx[pair[0]]].to(device)[None] |
| | voxel = torch.Tensor(voxel).unsqueeze(1).to(device) |
| | |
| | imageA = images[image_idx[pair[0]]].to(device)[None] |
| | imageB = images[image_idx[pair[1]]].to(device)[None] |
| | |
| | clip_targetA = clip_img_embedder(imageA.float()) |
| | clip_targetB = clip_img_embedder(imageB.float()) |
| | |
| | voxel_ridge = model.ridge(voxel,0) |
| | backbone, clip_voxels, _ = model.backbone(voxel_ridge) |
| | |
| | u = clip_voxels.flatten(1)[0] |
| | a = clip_targetA.flatten(1)[0] |
| | b = clip_targetB.flatten(1)[0] |
| | |
| | clip_voxels_norm = nn.functional.normalize(clip_voxels.flatten(1), dim=-1) |
| | clip_targetA_norm = nn.functional.normalize(clip_targetA.flatten(1), dim=-1) |
| | clip_targetB_norm = nn.functional.normalize(clip_targetB.flatten(1), dim=-1) |
| | |
| | u_norm = clip_voxels_norm.flatten(1)[0] |
| | a_norm = clip_targetA_norm.flatten(1)[0] |
| | b_norm = clip_targetB_norm.flatten(1)[0] |
| |
|
| | assert not torch.allclose(u, u_norm) |
| | assert not torch.allclose(a, a_norm) |
| | assert not torch.allclose(b, b_norm) |
| | |
| | if utils.batchwise_cosine_similarity(clip_voxels_norm, clip_targetA_norm) > utils.batchwise_cosine_similarity(clip_voxels_norm, clip_targetB_norm): |
| | score += 1 |
| | total += 1 |
| | |
| | voxel = vox[image_idx[pair[1]]].to(device)[None] |
| | voxel = torch.Tensor(voxel).unsqueeze(1).to(device) |
| | |
| | voxel_ridge = model.ridge(voxel,0) |
| | backbone, clip_voxels, _ = model.backbone(voxel_ridge) |
| | clip_voxels_norm = nn.functional.normalize(clip_voxels.flatten(1), dim=-1) |
| | |
| | if utils.batchwise_cosine_similarity(clip_voxels_norm, clip_targetB_norm) > utils.batchwise_cosine_similarity(clip_voxels_norm, clip_targetA_norm): |
| | score += 1 |
| | total += 1 |
| | |
| | sim_A = utils.batchwise_cosine_similarity(clip_voxels_norm, clip_targetA_norm) |
| | sim_B = utils.batchwise_cosine_similarity(clip_voxels_norm, clip_targetB_norm) |
| | |
| | return score/total, score, total |
| |
|
| |
|
| | |
| |
|
| |
|
| | def evaluate_mst_pairs(mst_pairs): |
| | with torch.no_grad(), torch.cuda.amp.autocast(dtype=data_type): |
| | failed_A = [] |
| | failed_B = [] |
| | failed_non_corr = [] |
| |
|
| | |
| | all_indices = np.unique(mst_pairs.flatten()) |
| | |
| | |
| | all_images = images[image_idx[all_indices]].to(device) |
| | all_voxels = torch.Tensor(vox[image_idx[all_indices]]).unsqueeze(1).to(device) |
| | |
| | |
| | all_clip_targets = clip_img_embedder(all_images.float()) |
| | all_clip_targets_norm = nn.functional.normalize(all_clip_targets.flatten(1), dim=-1) |
| | |
| | |
| | all_voxel_ridge = model.ridge(all_voxels, 0) |
| | _, all_clip_voxels, _ = model.backbone(all_voxel_ridge) |
| | all_clip_voxels_norm = nn.functional.normalize(all_clip_voxels.flatten(1), dim=-1) |
| | |
| | |
| | idx_to_pos = {idx: pos for pos, idx in enumerate(all_indices)} |
| | |
| | |
| | corr_score = 0 |
| | non_corr_score = 0 |
| | corr_total = len(mst_pairs) * 2 |
| | non_corr_total = len(mst_pairs) * (len(mst_pairs)-1) * 4 |
| |
|
| | |
| | |
| | idxA = np.array([pair[0] for pair in mst_pairs]) |
| | idxB = np.array([pair[1] for pair in mst_pairs]) |
| | |
| | posA = np.array([idx_to_pos[idx] for idx in idxA]) |
| | posB = np.array([idx_to_pos[idx] for idx in idxB]) |
| | |
| | voxA_embeddings = all_clip_voxels_norm[posA] |
| | voxB_embeddings = all_clip_voxels_norm[posB] |
| | imgA_embeddings = all_clip_targets_norm[posA] |
| | imgB_embeddings = all_clip_targets_norm[posB] |
| | |
| | simA_A = utils.batchwise_cosine_similarity(voxA_embeddings, imgA_embeddings) |
| | simA_B = utils.batchwise_cosine_similarity(voxA_embeddings, imgB_embeddings) |
| | simB_B = utils.batchwise_cosine_similarity(voxB_embeddings, imgB_embeddings) |
| | simB_A = utils.batchwise_cosine_similarity(voxB_embeddings, imgA_embeddings) |
| |
|
| | |
| | |
| | |
| | correct_A = torch.diag(simA_A) > torch.diag(simA_B) |
| | |
| | correct_B = torch.diag(simB_B) > torch.diag(simB_A) |
| |
|
| | corr_score += correct_A.sum().item() |
| | corr_score += correct_B.sum().item() |
| |
|
| | |
| | failed_A = [i for i, correct in enumerate(correct_A.cpu()) if not correct] |
| | failed_B = [i for i, correct in enumerate(correct_B.cpu()) if not correct] |
| | |
| | |
| | N = len(mst_pairs) |
| | |
| | row_idx = torch.arange(N).unsqueeze(1) |
| | col_idx = torch.arange(N).unsqueeze(0) |
| | off_diag_mask = row_idx != col_idx |
| | |
| | diagA_A = simA_A.diag().unsqueeze(1).expand(-1, N) |
| | diagB_B = simB_B.diag().unsqueeze(1).expand(-1, N) |
| | |
| | |
| |
|
| | |
| | off_diag_mask_device = off_diag_mask.to(device) |
| |
|
| | fail_AA = (simA_A < diagA_A) & off_diag_mask_device |
| | fail_AB = (simA_B < diagA_A) & off_diag_mask_device |
| | fail_BB = (simB_B < diagB_B) & off_diag_mask_device |
| | fail_BA = (simB_A < diagB_B) & off_diag_mask_device |
| |
|
| | non_corr_score += fail_AA.sum().item() |
| | non_corr_score += fail_AB.sum().item() |
| | non_corr_score += fail_BB.sum().item() |
| | non_corr_score += fail_BA.sum().item() |
| |
|
| | |
| | fail_sources = [fail_AA, fail_AB, fail_BB, fail_BA] |
| | for fail_matrix, label in zip(fail_sources, ["AA", "AB", "BB", "BA"]): |
| | fail_coords = torch.nonzero(fail_matrix, as_tuple=False).cpu().numpy() |
| | for i, j in fail_coords: |
| | failed_non_corr.append({"type": label, "i": i, "j": j, "pair_i": mst_pairs[i], "pair_j": mst_pairs[j]}) |
| |
|
| | return corr_score, corr_total, int(non_corr_score), non_corr_total, failed_A, failed_B, failed_non_corr |
| |
|
| |
|
| | |
| |
|
| |
|
| | all_scores = [] |
| | all_failures = [] |
| |
|
| | for i in range(2): |
| | for j in range(2): |
| | mst_pairs = np.stack([pairs[:, 0, i], pairs[:, 1, j]], axis=1) |
| | corr_score, corr_total, non_corr_score, non_corr_total, failed_A, failed_B, failed_non_corr = evaluate_mst_pairs(mst_pairs) |
| |
|
| | |
| | all_scores.append((corr_score, corr_total, non_corr_score, non_corr_total)) |
| | all_failures.append({ |
| | "repeat_A": i, |
| | "repeat_B": j, |
| | "failed_A": failed_A, |
| | "failed_B": failed_B, |
| | "failed_non_corr": failed_non_corr, |
| | "mst_pairs": mst_pairs, |
| | }) |
| |
|
| | |
| | print(f"pairmate A repeat {i} vs pairmate B repeat {j}:") |
| | print(f"2-AFC corresponding = {corr_score}/{corr_total} ({corr_score/corr_total:.2%})") |
| | print(f"2-AFC non-corresponding = {non_corr_score}/{non_corr_total} ({non_corr_score/non_corr_total:.2%})") |
| | print("") |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | print(np.all(pairs[:, 0, 0] == pairs[:, 0, 1])) |
| | print(np.all(pairs[:, 1, 0] == pairs[:, 1, 1])) |
| |
|
| |
|
| | |
| |
|
| |
|
| | print(np.unique(pairs[:, 0, :], axis=1).shape[-1]) |
| | print(np.unique(pairs[:, 1, :], axis=1).shape[-1]) |
| |
|
| |
|
| | |
| |
|
| |
|
| | test1 = np.stack([pairs[:, 0, 0], pairs[:, 1, 0]], axis=1) |
| | test2 = np.stack([pairs[:, 0, 0], pairs[:, 1, 1]], axis=1) |
| | print(np.array_equal(test1, test2)) |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | a = pairs[:, 0, 0] |
| | b = pairs[:, 1, 0] |
| | c = pairs[:, 1, 1] |
| |
|
| | print(np.mean(np.abs(b - c))) |
| | print(np.mean(np.abs(a - b))) |
| | print(np.mean(np.abs(a - c))) |
| |
|
| |
|
| | |
| |
|
| |
|
| | ix = 5 |
| | display(utils.torch_to_Image(images[mst_pairs[ix][0]])) |
| | display(utils.torch_to_Image(images[mst_pairs[ix][1]])) |
| |
|
| |
|
| | |
| |
|
| |
|
| | subset_indices |
| |
|
| |
|
| | |
| |
|
| |
|
| | MST_idx.shape |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | x = MST_idx[:, 0, 0].reshape(-1) |
| | y = MST_idx[:, 0, 1].reshape(-1) |
| | for i in range(len(x)): |
| | assert utils.torch_to_Image(images[x[i]]) == utils.torch_to_Image(images[y[i]]) |
| | display(utils.torch_to_Image(images[x[i]])) |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | scores, totals |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| | |
| | |
| |
|
| |
|
| | |
| |
|
| |
|
| |
|
| |
|
| |
|
| | |
| |
|
| |
|
| |
|
| |
|
| |
|