-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtrain_convex.py
339 lines (280 loc) · 9.3 KB
/
train_convex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import argparse
import faulthandler
import logging
import numpy as np
import pickle
import os
import time
import wandb
from training import compute_corr
from models import (
get_dataset,
get_feature_model,
get_aggregator,
preprocess_data,
get_projection_matrix,
tune_batch_size,
)
from convex_models import (
compute_ridge_estimate,
compute_boosting_estimate,
compute_l1_estimate,
)
from research_code.cka_step4 import cka
import torch
ff = 0.1
def save_to_wandb(results, weights, args, offline=False):
if offline:
os.environ["WANDB_MODE"] = "dryrun"
else:
os.environ["WANDB_MODE"] = "run"
run = wandb.init(project="train_fmri_convex.py", config=vars(args), reinit=True)
config = wandb.config
# This allows the info to be visible in the dashboard
wandb.log(results)
# This saves the whole results (no histogramming). Has to happen after log.
wandb.run.summary.update(results)
# Also save the best weights.
if not args.no_save:
weight_path = os.path.join(wandb.run.dir, "optimal_weights.pkl")
with open(weight_path, "wb") as f:
pickle.dump(weights, f)
# These weights are big, only save if necessary
wandb.save(weight_path)
results["weight_path"] = weight_path
out_path = os.path.join(wandb.run.dir, "results.pkl")
with open(out_path, "wb") as f:
pickle.dump(results, f)
wandb.save(out_path)
run.finish()
def compute_layer(
trainloader,
reportloader,
feature_model,
aggregator,
activations,
metadata,
args,
max_r,
):
print(f"Processing layer {args.layer_name}")
t0 = time.time()
X, Y = preprocess_data(
trainloader, feature_model, aggregator, activations, metadata, args
)
if X is None:
print(f"Skipping layer {args.layer_name}")
return
# Use k-fold cross-validation
kfold = 5
splits = (np.arange(X.shape[0]) / 100).astype(np.int) % kfold
m = X.mean(axis=0, keepdims=True)
s = X.std(axis=0, keepdims=True) + ff
Ym = Y.mean(axis=0, keepdims=True)
Y = Y - Ym
# Use in-place operators instead of (X - m) / s to save memory.
X.add_(-m)
X.divide_(s)
if args.pca > -1:
V = get_projection_matrix(X, n=args.pca)
X = torch.matmul(X, V)
X_report, Y_report = preprocess_data(
reportloader, feature_model, aggregator, activations, metadata, args
)
if X is None:
print(f"Skipping layer {args.layer_name}")
return
Y_report = Y_report - Ym
# Use in-place operators instead of (X - m) / s to save memory.
X_report.add_(-m)
X_report.divide_(s)
if args.pca > -1:
X_report = torch.matmul(X_report, V)
if args.method == "ridge":
results, weights = compute_ridge_estimate(X, Y, X_report, Y_report, splits)
elif args.method == "boosting":
results, weights = compute_boosting_estimate(X, Y, X_report, Y_report, splits)
elif args.method == "l1":
results, weights = compute_l1_estimate(X, Y, X_report, Y_report, splits)
else:
raise NotImplementedError("Method not implemented")
cka_report = cka(X_report, Y_report)
if not args.save_predictions:
del weights["Y_preds"]
results["feature_mean"] = m.squeeze().cpu().detach().numpy()
results["fit_time"] = time.time() - t0
results["cka_report"] = cka_report.item()
results["layer"] = args.layer
results["subset"] = args.subset
results["max_r"] = max_r
if not args.no_wandb:
try:
save_to_wandb(results, weights, args, offline=False)
except wandb.errors.error.UsageError:
print(">>> Could not save to cloud, using offline save this once.")
save_to_wandb(results, weights, args, offline=True)
else:
print(results)
def check_existing(args, metadata):
api = wandb.Api()
runs = api.runs(
"pmin/train_fmri_convex.py",
{
"$and": [
{"config.exp_name": args.exp_name},
{"config.dataset": args.dataset},
{"config.aggregator": args.aggregator},
{"config.aggregator_sz": args.aggregator_sz},
{"config.pca": args.pca},
{"config.features": args.features},
{"config.subset": args.subset},
{"config.method": args.method},
{"config.resize": args.resize},
{"state": "finished"},
]
},
)
return len(runs) >= len(metadata["layers"])
def main(args):
print("Fitting model")
print(args)
device = "cuda"
try:
os.makedirs(args.ckpt_root)
except FileExistsError:
pass
try:
os.makedirs(args.cache_root)
except FileExistsError:
pass
trainset = get_dataset(args, "traintune")
reportset = get_dataset(args, "report")
if hasattr(trainset, "max_r"):
# Store it in the dataset
max_r = trainset.max_r
else:
max_r = 1.0
args.ntau = trainset.ntau
feature_model, activations, metadata = get_feature_model(args)
# Override size
metadata["sz"] = args.resize
if args.skip_existing:
exists = check_existing(args, metadata)
if exists:
print(">>> Run already exists, skipping")
return
aggregator = get_aggregator(metadata, args)
feature_model.to(device=device)
if args.autotune:
batch_size = tune_batch_size(feature_model, trainset, metadata)
else:
batch_size = args.batch_size
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batch_size, shuffle=False, pin_memory=True
)
reportloader = torch.utils.data.DataLoader(
reportset, batch_size=batch_size, shuffle=False, pin_memory=True
)
# Do this for every layer under the sun.
for layer_num, layer_name in enumerate(metadata["layers"].keys()):
args.layer = layer_num # For backwards compatibility
args.layer_name = layer_name
compute_layer(
trainloader,
reportloader,
feature_model,
aggregator,
activations,
metadata,
args,
max_r,
)
if __name__ == "__main__":
logging.basicConfig(level='DEBUG')
faulthandler.enable()
desc = "Map a pretrained neural net to a time series of brain data (neurons or brains) using ridge regression."
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("--exp_name", required=True, help="Friendly name of experiment")
parser.add_argument(
"--features",
default="gaborpyramid3d",
type=str,
help="What kind of features to use",
)
parser.add_argument(
"--aggregator",
default="average",
type=str,
help="What kind of aggregator to use",
)
parser.add_argument(
"--aggregator_sz",
default=8,
type=int,
help="The size the aggregator will be used with",
)
parser.add_argument("--batch_size", default=4, type=int, help="Batch size")
parser.add_argument(
"--pca",
default=-1,
type=int,
help="Size of PCA before model fit (if applicable)",
)
parser.add_argument(
"--no_wandb", default=False, help="Skip using W&B", action="store_true"
)
parser.add_argument(
"--no_save", default=False, help="Skip saving weights", action="store_true"
)
parser.add_argument(
"--skip_existing", default=False, help="Skip existing runs", action="store_true"
)
parser.add_argument(
"--autotune",
default=False,
help="Tune the batch size to maximize memory consumption",
action="store_true",
)
parser.add_argument(
"--subsample_layers",
default=False,
help="Subsample layers (saves disk space & mem)",
action="store_true",
)
parser.add_argument(
"--save_predictions",
default=False,
help="Save predictions among weights",
action="store_true",
)
parser.add_argument(
"--dataset", default="vim2", help="Dataset (currently vim2, pvc4)"
)
parser.add_argument(
"--subset", default="s1", help="Either subject name or neuron num"
)
parser.add_argument("--data_root", default="./data_derived", help="Data path")
parser.add_argument(
"--cache_root", default="./cache", help="Precomputed cache path"
)
parser.add_argument(
"--slowfast_root", default="", help="Path where SlowFast is installed"
)
parser.add_argument(
"--ckpt_root",
default="./pretrained",
help="Path where trained model checkpoints will be downloaded",
)
parser.add_argument(
"--method",
default="ridge",
help="Method to fit the model (ridge or boosting)",
)
parser.add_argument(
"--resize",
default=112,
type=int,
help="Size of stimulus",
)
args = parser.parse_args()
main(args)