-
Notifications
You must be signed in to change notification settings - Fork 91
/
stageA2_mbm_finetune.py
227 lines (191 loc) · 8.96 KB
/
stageA2_mbm_finetune.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
import os, sys
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch.nn.parallel import DistributedDataParallel
import argparse
import time
import timm.optim.optim_factory as optim_factory
import datetime
import matplotlib.pyplot as plt
import wandb
import copy
# own code
from config import Config_MBM_finetune
from dataset import create_Kamitani_dataset, create_BOLD5000_dataset
from sc_mbm.mae_for_fmri import MAEforFMRI
from sc_mbm.trainer import train_one_epoch
from sc_mbm.trainer import NativeScalerWithGradNormCount as NativeScaler
from sc_mbm.utils import save_model
os.environ["WANDB_START_METHOD"] = "thread"
os.environ['WANDB_DIR'] = "."
class wandb_logger:
def __init__(self, config):
wandb.init( project='mind-vis',
group="stepA_sc-mbm_tune",
anonymous="allow",
config=config,
reinit=True)
self.config = config
self.step = None
def log(self, name, data, step=None):
if step is None:
wandb.log({name: data})
else:
wandb.log({name: data}, step=step)
self.step = step
def watch_model(self, *args, **kwargs):
wandb.watch(*args, **kwargs)
def log_image(self, name, fig):
if self.step is None:
wandb.log({name: wandb.Image(fig)})
else:
wandb.log({name: wandb.Image(fig)}, step=self.step)
def finish(self):
wandb.finish(quiet=True)
def get_args_parser():
parser = argparse.ArgumentParser('MAE finetuning on Test fMRI', add_help=False)
# Training Parameters
parser.add_argument('--lr', type=float)
parser.add_argument('--weight_decay', type=float)
parser.add_argument('--num_epoch', type=int)
parser.add_argument('--batch_size', type=int)
parser.add_argument('--mask_ratio', type=float)
# Project setting
parser.add_argument('--root_path', type=str)
parser.add_argument('--pretrain_mbm_path', type=str)
parser.add_argument('--dataset', type=str)
parser.add_argument('--include_nonavg_test', type=bool)
# distributed training parameters
parser.add_argument('--local_rank', type=int)
return parser
def create_readme(config, path):
print(config.__dict__)
with open(os.path.join(path, 'README.md'), 'w+') as f:
print(config.__dict__, file=f)
def fmri_transform(x, sparse_rate=0.2):
# x: 1, num_voxels
x_aug = copy.deepcopy(x)
idx = np.random.choice(x.shape[0], int(x.shape[0]*sparse_rate), replace=False)
x_aug[idx] = 0
return torch.FloatTensor(x_aug)
def main(config):
if torch.cuda.device_count() > 1:
torch.cuda.set_device(config.local_rank)
torch.distributed.init_process_group(backend='nccl')
sd = torch.load(config.pretrain_mbm_path, map_location='cpu')
config_pretrain = sd['config']
output_path = os.path.join(config.root_path, 'results', 'fmri_finetune', '%s'%(datetime.datetime.now().strftime("%d-%m-%Y-%H-%M-%S")))
# output_path = os.path.join(config.root_path, 'results', 'fmri_finetune')
config.output_path = output_path
logger = wandb_logger(config) if config.local_rank == 0 else None
if config.local_rank == 0:
os.makedirs(output_path, exist_ok=True)
create_readme(config, output_path)
device = torch.device(f'cuda:{config.local_rank}') if torch.cuda.is_available() else torch.device('cpu')
torch.manual_seed(config_pretrain.seed)
np.random.seed(config_pretrain.seed)
# create model
num_voxels = (sd['model']['pos_embed'].shape[1] - 1)* config_pretrain.patch_size
model = MAEforFMRI(num_voxels=num_voxels, patch_size=config_pretrain.patch_size, embed_dim=config_pretrain.embed_dim,
decoder_embed_dim=config_pretrain.decoder_embed_dim, depth=config_pretrain.depth,
num_heads=config_pretrain.num_heads, decoder_num_heads=config_pretrain.decoder_num_heads,
mlp_ratio=config_pretrain.mlp_ratio, focus_range=None, use_nature_img_loss=False)
model.load_state_dict(sd['model'], strict=False)
model.to(device)
model_without_ddp = model
# create dataset and dataloader
if config.dataset == 'GOD':
_, test_set = create_Kamitani_dataset(path=config.kam_path, patch_size=config_pretrain.patch_size,
subjects=config.kam_subs, fmri_transform=torch.FloatTensor, include_nonavg_test=config.include_nonavg_test)
elif config.dataset == 'BOLD5000':
_, test_set = create_BOLD5000_dataset(path=config.bold5000_path, patch_size=config_pretrain.patch_size,
fmri_transform=torch.FloatTensor, subjects=config.bold5000_subs, include_nonavg_test=config.include_nonavg_test)
else:
raise NotImplementedError
print(test_set.fmri.shape)
if test_set.fmri.shape[-1] < num_voxels:
test_set.fmri = np.pad(test_set.fmri, ((0,0), (0, num_voxels - test_set.fmri.shape[-1])), 'wrap')
else:
test_set.fmri = test_set.fmri[:, :num_voxels]
print(f'Dataset size: {len(test_set)}')
sampler = torch.utils.data.DistributedSampler(test_set) if torch.cuda.device_count() > 1 else torch.utils.data.RandomSampler(test_set)
dataloader_hcp = DataLoader(test_set, batch_size=config.batch_size, sampler=sampler)
if torch.cuda.device_count() > 1:
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
model = DistributedDataParallel(model, device_ids=[config.local_rank], output_device=config.local_rank, find_unused_parameters=config.use_nature_img_loss)
param_groups = optim_factory.add_weight_decay(model, config.weight_decay)
optimizer = torch.optim.AdamW(param_groups, lr=config.lr, betas=(0.9, 0.95))
print(optimizer)
loss_scaler = NativeScaler()
if logger is not None:
logger.watch_model(model,log='all', log_freq=1000)
cor_list = []
start_time = time.time()
print('Finetuning MAE on test fMRI ... ...')
for ep in range(config.num_epoch):
if torch.cuda.device_count() > 1:
sampler.set_epoch(ep) # to shuffle the data at every epoch
cor = train_one_epoch(model, dataloader_hcp, optimizer, device, ep, loss_scaler, logger, config, start_time, model_without_ddp)
cor_list.append(cor)
if (ep % 2 == 0 or ep + 1 == config.num_epoch) and ep != 0 and config.local_rank == 0:
# save models
save_model(config_pretrain, ep, model_without_ddp, optimizer, loss_scaler, os.path.join(output_path,'checkpoints'))
# plot figures
plot_recon_figures(model, device, test_set, output_path, 5, config, logger, model_without_ddp)
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
print('Training time {}'.format(total_time_str))
if logger is not None:
logger.log('max cor', np.max(cor_list), step=config.num_epoch-1)
logger.finish()
return
@torch.no_grad()
def plot_recon_figures(model, device, dataset, output_path, num_figures = 5, config=None, logger=None, model_without_ddp=None):
dataloader = DataLoader(dataset, batch_size=1, shuffle=True)
model.eval()
fig, axs = plt.subplots(num_figures, 3, figsize=(30,15))
fig.tight_layout()
axs[0,0].set_title('Ground-truth')
axs[0,1].set_title('Masked Ground-truth')
axs[0,2].set_title('Reconstruction')
for ax in axs:
sample = next(iter(dataloader))['fmri']
sample = sample.to(device)
_, pred, mask = model(sample, mask_ratio=config.mask_ratio)
sample_with_mask = model_without_ddp.patchify(sample).to('cpu').numpy().reshape(-1, model_without_ddp.patch_size)
pred = model_without_ddp.unpatchify(pred).to('cpu').numpy().reshape(-1)
sample = sample.to('cpu').numpy().reshape(-1)
mask = mask.to('cpu').numpy().reshape(-1)
# cal the cor
cor = np.corrcoef([pred, sample])[0,1]
x_axis = np.arange(0, sample.shape[-1])
# groundtruth
ax[0].plot(x_axis, sample)
# groundtruth with mask
s = 0
for x, m in zip(sample_with_mask,mask):
if m == 0:
ax[1].plot(x_axis[s:s+len(x)], x, color='#1f77b4')
s += len(x)
# pred
ax[2].plot(x_axis, pred)
ax[2].set_ylabel('cor: %.4f'%cor, weight = 'bold')
ax[2].yaxis.set_label_position("right")
fig_name = 'reconst-%s'%(datetime.datetime.now().strftime("%d-%m-%Y-%H-%M-%S"))
fig.savefig(os.path.join(output_path, f'{fig_name}.png'))
if logger is not None:
logger.log_image('reconst', fig)
plt.close(fig)
def update_config(args, config):
for attr in config.__dict__:
if hasattr(args, attr):
if getattr(args, attr) != None:
setattr(config, attr, getattr(args, attr))
return config
if __name__ == '__main__':
args = get_args_parser()
args = args.parse_args()
config = Config_MBM_finetune()
config = update_config(args, config)
main(config)