-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanalysis_extractRawData.py
209 lines (141 loc) · 6.25 KB
/
analysis_extractRawData.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
import argparse
from genericpath import exists
import sys
import os
from sklearn.manifold import TSNE
import gc
import os
os.chdir("..")
sys.path.append("./")
import torch
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dataset", type=str, default="hcpTask")
parser.add_argument("-c", "--device", type=str, default="0")
parser.add_argument("-s", "--seed", type=str, default=-1)
parser.add_argument("-f", "--fold", type=int, default = -1)
print("cwd = {}".format(os.getcwd()))
argv = parser.parse_args()
import numpy as np
import torch
from Dataset.datasetDetails import datasetDetailsDict
from Dataset.dataset import getDataset
from utils import Option
from tqdm import tqdm
from Analysis.relevanceCalculator import generate_relevance
datasetName = argv.dataset
datasetDetails = datasetDetailsDict[datasetName]
if(datasetName == "abide1"):
foldCount = 10
targetSeeds = [0,1,2,3,4]
else:
foldCount = 5
targetSeeds = [0]
if(argv.seed != -1):
targetSeeds = [argv.seed]
for seed in targetSeeds:
if(datasetName == "abide1"):
dataset = getDataset(Option({
"batchSize" : None,
"dynamicLength" : None,
"foldCount" : foldCount,
"datasetSeed" : seed,
"targetTask" : "disease",
"atlas" : "schaefer7_400",
"datasetName" : datasetName
}))
elif(datasetName == "hcpRest"):
dataset = getDataset(Option({
"batchSize" : None,
"dynamicLength" : None,
"foldCount" : foldCount,
"datasetSeed" : seed,
"targetTask" : "gender",
"atlas" : "schaefer7_400",
"datasetName" : datasetName
}))
elif(datasetName == "hcpTask"):
dataset = getDataset(Option({
"batchSize" : None,
"dynamicLength" : None,
"foldCount" : foldCount,
"datasetSeed" : seed,
"targetTask" : "DoesNotMatter",
"atlas" : "schaefer7_400",
"datasetName" : datasetName
}))
# load model here
datasetNameToModelPathMapper = {
"hcpRest" : "./Analysis/TargetSavedModels/hcpRest/seed_{}/".format(seed),
"hcpTask" : "./Analysis/TargetSavedModels/hcpTask/seed_{}/".format(seed),
"abide1" : "./Analysis/TargetSavedModels/abide1/seed_{}/".format(seed),
}
datasetNameToFolder = {
"hcpRest" : "./Analysis/Data/hcpRest/seed_{}/".format(seed),
"hcpTask" : "./Analysis/Data/hcpTask/seed_{}/".format(seed),
"abide1" : "./Analysis/Data/abide1/seed_{}/".format(seed),
}
device = "cuda:{}".format(argv.device)
targetFolds = []
if(argv.fold == -1):
targetFolds = range(foldCount)
else:
targetFolds = [argv.fold]
for fold in targetFolds:
print("\n extracting for seed {}, fold {}\n".format(seed, fold))
dataset.setFold(fold, train=True)
data = dataset.data
labels = dataset.labels
subjIds = dataset.subjectIds
trainIdx = dataset.trainIdx
testIdx = dataset.testIdx
for i, subjId in enumerate(tqdm(subjIds, ncols=60)):
targetModelFile = datasetNameToModelPathMapper[datasetName] + "model_{}.save".format(fold) # sanity checker, fix fold+1 to fold
modell = torch.load(targetModelFile, map_location="cpu")
model = modell.model.to("cuda:{}".format(argv.device))
torch.cuda.empty_cache()
isInTrain = i in trainIdx
timeseries = torch.tensor(data[i]).float().to(device)
label = torch.tensor(labels[i]).long().to(device)
targetDumpFolder = datasetNameToFolder[datasetName]
if(isInTrain):
targetDumpFolder += "FOLD_{}/TRAIN/{}-{}".format(fold, int(label), subjId)
else:
targetDumpFolder += "FOLD_{}/TEST/{}-{}".format(fold, int(label),subjId)
os.makedirs(targetDumpFolder, exist_ok=True)
timeseries = ( timeseries - timeseries.mean(dim=1, keepdims=True) ) / timeseries.std(dim=1, keepdims=True)
timeseries = timeseries[None, :, :]
model.eval()
inputToken_relevances = generate_relevance(model, timeseries, None)#label) # (nW, T)
viz = inputToken_relevances.detach().cpu().numpy().mean(axis=0)[None,:].repeat(400,axis=0)
np.save(targetDumpFolder + "/label.npy", label.cpu().numpy())
np.save(targetDumpFolder + "/clsRelevancyMap.npy", inputToken_relevances.detach().cpu().numpy())
# SAVE TOKENS
# FIRST INPUT ITSELF
token_0 = timeseries.detach().cpu().numpy()[0].T
np.save(targetDumpFolder + "/token_layerIn.npy", token_0)
layerCount = len(model.blocks)
for layer in range(layerCount):
token_layer = model.tokens[layer][0].cpu().detach().numpy()
attentionMaps = model.blocks[layer].transformer.attention.attentionMaps.cpu().detach().numpy()
np.save(targetDumpFolder + "/attentionMaps_layer{}.npy".format(layer), attentionMaps)
relative_position_bias_table = model.blocks[layer].transformer.attention.relative_position_bias_table.cpu().detach().numpy()
np.save(targetDumpFolder + "/relative_position_bias_table_layer{}.npy".format(layer), relative_position_bias_table)
# clean previous caches values
for token in model.tokens:
del token
del model.tokens
model.tokens = []
for i in range(len(model.blocks)):
model.blocks[i].transformer.attention.handle.remove()
del model.blocks[i].transformer.attention.attentionGradients
del model.blocks[i].transformer.attention.attentionMaps
del token_0
del timeseries
del viz
del label
#del attentionMaps
del relative_position_bias_table
del inputToken_relevances
del model
del modell
torch.cuda.empty_cache()