-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·259 lines (196 loc) · 8.38 KB
/
test.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
#%%
# To add a new cell, type '# %%'
# To add a new markdown cell, type '# %% [markdown]'
# %%
import os, argparse
from itertools import combinations
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model', default='model')
parser.add_argument('-p', '--participant', default=1, type=int)
parser.add_argument('-g', '--gpu', default='0')
# args = parser.parse_args(['-m','model_less_rtlhb_p1','-r','1','-p','1'])
args = parser.parse_args()
print(args)
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
# example of training an conditional gan on the fashion mnist dataset
import numpy as np
import tensorflow as tf
from numpy.random import randint, randn
from tensorflow.keras.models import load_model
from matplotlib import pyplot as plt
from sklearn import preprocessing
from sklearn.metrics.pairwise import cosine_similarity
np.set_printoptions(suppress=True)
from utils.visualize import fmriviz
from utils.preprocess import dataloader, preprocess, postprocess
##%%
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples, n_classes=60):
z_input = tf.random.normal((n_samples, latent_dim), mean=0.0, stddev=1.0, dtype=tf.dtypes.float32)
# generate labels
ix = randint(0, n_classes, n_samples)
return [z_input, ix]
# def prepare_images(vecs, voxel_map):
# images = []
# for raw in vecs:
# img = fmriviz.prepare_image(raw, voxel_map)
# images += [img]
# images = np.array(images)
# X = expand_dims(images, axis=-1)
# return X
participant = args.participant
samples = dataloader.data[participant].samples
voxel_map = dataloader.data[participant].voxel_map
trial_map = dataloader.data[participant].trial_map
features = dataloader.features
labels = dataloader.data[participant].labels
# Note: very important to have correct labels array
nouns = list(trial_map.keys())
lencoder = preprocessing.LabelEncoder()
Y = lencoder.fit_transform(nouns)
train_vectors, embeddings = preprocess.prepare_data(features,trial_map,samples,nouns)
snr = preprocess.get_snr(participant, samples, trial_map)
# snr_img = fmriviz.prepare_image(snr, voxel_map)
# size of the latent space
latent_dim = 1000
def transform_fake_images(fake, voxel_map):
predictions = []
for img in fake:
vector = postprocess.img2vector(img, voxel_map)
predictions += [vector]
return np.array(predictions)
def test(snr, combinations, predictions, true_images):
arr_similarity = []
for pair in combinations:
idx = list(pair)
similarity = postprocess.evaluate(snr, predictions[idx], true_images[idx])
arr_similarity += [similarity]
return np.array(arr_similarity)
def cl_eval(snr, predictions, true_images, top=500):
arr_similarity = []
for i in range(len(predictions)):
similarity = postprocess.classic_eval(snr, predictions[i], true_images[i], 0.7, top)
# similarity = postprocess.classic_eval(true_images[i], predictions[i], true_images[i], 0.7, 500)
arr_similarity += [similarity]
accuracy = sum(arr_similarity * 1)/len(arr_similarity)
print('Accuracy: %f' % (accuracy))
return np.array(arr_similarity)
#%%
# load model
model = load_model(os.path.join('pretrained', args.model + '.h5'))
#%%----------------------------------------test------------------------------------------
test_combinations = list(combinations(Y, 2))
predictions = np.zeros((1,samples.shape[1]))
latent_points, _ = generate_latent_points(1000, len(Y))
for i in range(6):
start = i * 10
end = (i + 1) * 10
X = model.predict([latent_points[start:end], Y[start:end]])
fake_image = X[:,:,:,:,0]
preds = transform_fake_images(fake_image, voxel_map)
predictions = np.concatenate((predictions, preds), axis=0)
predictions = predictions[1:]
true_vecs = train_vectors
arr_similarity = test(snr, test_combinations, predictions, true_vecs)
accuracy = np.mean(arr_similarity)
print('Accuracy: %f' % (accuracy))
temp = cl_eval(snr, predictions, true_vecs)
# %% --------------------------------main plots-------------------------------------------------------------------
sample_idx = 15
# vmin = np.min(predictions[sample_idx])/2
# vmax = np.max(predictions[sample_idx])/2
vmin = np.min(true_vecs[sample_idx])
vmax = np.max(true_vecs[sample_idx])
# rescaled = predictions[sample_idx] * 5
theimg = fmriviz.prepare_image(predictions[sample_idx], voxel_map, fill=vmin)
fmriviz.plot_slices(theimg, vmin, vmax)
# theimg = fmriviz.prepare_image(rescaled, voxel_map, fill=vmin)
# fmriviz.plot_slices(theimg, vmin, vmax)
# tvox = postprocess.get_top_voxels(predictions[0], 500)
# binary = np.full(true_vecs[0].shape, -0.2)
# binary[tvox] = 1
# theimg = fmriviz.prepare_image(binary, voxel_map, fill=-1)
# fmriviz.plot_slices(theimg, -1, 1, cmap='gray_r')
# tvox = postprocess.get_top_voxels(snr, 500)
# binary = np.full(true_vecs[0].shape, -0.2)
# binary[tvox] = 1
# theimg = fmriviz.prepare_image(binary, voxel_map, fill=-1)
# fmriviz.plot_slices(theimg, -1, 1, cmap='gray_r')
# tvox = postprocess.get_top_voxels(true_vecs[0], 500)
# binary = np.full(true_vecs[0].shape, -0.2)
# binary[tvox] = 1
# theimg = fmriviz.prepare_image(binary, voxel_map, fill=-1)
# fmriviz.plot_slices(theimg, -1, 1, cmap='gray_r')
vmin = np.min(true_vecs[sample_idx])
vmax = np.max(true_vecs[sample_idx])
theimg = fmriviz.prepare_image(true_vecs[sample_idx], voxel_map, fill=vmin)
fmriviz.plot_slices(theimg, vmin, vmax)
#%%
# all_predictions = []
# custom_labels = [["cup","cup"],["hammer","hammer"],["house","house"],["knife","knife"],["screwdriver","screwdriver"]]
# all_img_pairs = []
# nouns = set(trial_map.keys())
# test_combinations = list(combinations(nouns, 2))
# for pair in test_combinations:
# custom_y = lencoder.transform(pair)
# # print(custom_y)
# fake_images, predy = generate_pred_pairs(model, custom_y)
# predictions = transform_fake_images(fake_images, voxel_map)
# true_vecs = train_vectors[predy]
# # print(lencoder.inverse_transform(predy))
# all_predictions += [[predictions, true_vecs]]
# # all_img_pairs += [[fake_images,trainX[predy,:,:,:,0], custom_labels[i]]]
# all_predictions = np.array(all_predictions)
# # %%
# asimtemp = test(snr, all_predictions[:,0], all_predictions[:,1])
# #%% ---------------------------------------------------------------------
# for i in range(5):
# p1_gen = all_predictions[i][0][1]
# p1_real = all_predictions[i][1][1]
# lbl = test_combinations[i][1]
# vmin = np.min(p1_real)
# vmax = np.max(p1_real)
# sample_image = fmriviz.prepare_image(p1_real, voxel_map, fill=vmin)
# fmriviz.plot_slices(sample_image,vmin,vmax, filename="GAN_" + lbl + "_real")
# sample_image = fmriviz.prepare_image(p1_gen, voxel_map, fill=vmin)
# fmriviz.plot_slices(sample_image,vmin,vmax, filename="GAN_" + lbl + "_gen")
# # %%
# cosine_similarity(fake_images[0].reshape(1,-1), theimg.reshape(1,-1))
# # %%
# cosine_similarity(trainX[predy[0],:,:,:,0].reshape(1,-1), theimg.reshape(1,-1))
# # %%
# cosine_similarity(thevec.reshape(1,-1), samples[predy[0]].reshape(1,-1))
# # %%
# top = postprocess.get_top_voxels(samples[predy[0]],500)
# cosine_similarity(thevec[top].reshape(1,-1), samples[predy[0]][top].reshape(1,-1))
# # %%
# top = postprocess.get_top_voxels(snr,500)
# cosine_similarity((thevec * snr)[top].reshape(1,-1), samples[predy[0]][top].reshape(1,-1))
#%% -----------------------------------volume plots---------------------------------------------------------------------
# nouns = list(trial_map.keys())
# y_Bar = lencoder.transform(nouns)
# predictions = np.zeros((1,51,61,23))
# latent_points, _ = generate_latent_points(1000, len(y_Bar))
# for i in range(6):
# start = i * 10
# end = (i + 1) * 10
# X = model.predict([latent_points[start:end], y_Bar[start:end]])
# fake_images = X[:,:,:,:,0]
# predictions = np.concatenate((predictions, fake_images), axis=0)
# predictions = predictions[1:]
#%%
# vol_match = []
# for pred in predictions:
# vec = postprocess.img2vector(pred, voxel_map)
# img = fmriviz.prepare_image(vec, voxel_map)
# vol_match += [cosine_similarity(img.reshape(1,-1), pred.reshape(1,-1))[0][0]]
# match = sum(vol_match * 1)/len(vol_match)
# print('Volume Match: %f' % (match))
# # %%
# vmin = np.min(true_vecs[0])
# vmax = np.max(true_vecs[0])
# vec = postprocess.img2vector(predictions[0], voxel_map)
# img = fmriviz.prepare_image(vec, voxel_map, vmin)
# fmriviz.plot_slices(img, vmin, vmax, "refrigrator_gen_3dgan_remapped")
# %%