-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreal_data_loss_comparison.py
211 lines (173 loc) · 8.25 KB
/
real_data_loss_comparison.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
"""Convergence rate between iterative-step-z and learn-step-z algorithm for
TA decomposition.
"""
# Authors: Hamza Cherkaoui <hamza.cherkaoui@inria.fr>
# License: BSD (3-clause)
import os
import shutil
import time
import argparse
import json
import pickle
import matplotlib as mpl
mpl.rcParams['pgf.texsystem'] = 'pdflatex'
mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amssymb}']
mpl.rcParams['xtick.labelsize'] = 18
mpl.rcParams['ytick.labelsize'] = 18
mpl.rcParams['axes.labelsize'] = 18
import matplotlib.pyplot as plt
import numpy as np
from nilearn.input_data import NiftiMasker
from carpet.utils import init_vuz
from pyta import TA
from pyta.hrf_model import double_gamma_hrf
from pyta.convolution import make_toeplitz
from pyta.utils import compute_lbda_max, logspace_layers
from pyta.loss_and_grad import _obj_t_analysis
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--max-iter', type=int, default=20,
help='Max number of iterations for the global loop.')
parser.add_argument('--temp-reg', type=float, default=0.5,
help='Temporal regularisation parameter.')
parser.add_argument('--max-iter-z', type=int, default=100,
help='Max number of iterations for the z-step.')
parser.add_argument('--load-net', type=str, default=None, nargs='+',
help='Load pretrained network parameters.')
parser.add_argument('--max-training-iter', type=int, default=1000,
help='Max number of iterations to train the '
'learnable networks for the z-step.')
parser.add_argument('--n-time-frames', type=int, default=100,
help='Number of timeframes to retain from the the '
'data fMRI.')
parser.add_argument('--plots-dir', type=str, default='outputs',
help='Outputs directory for plots.')
parser.add_argument('--iter-mult', type=float, default='2.0',
help='Multiplicative coefficient to obtain the number'
' of iteration.')
parser.add_argument('--seed', type=int, default=None,
help='Set the seed for the experiment. Can be used '
'for debug or to freeze experiments.')
args = parser.parse_args()
print(__doc__)
print('*' * 80)
t0_global = time.time()
if not os.path.exists(args.plots_dir):
os.makedirs(args.plots_dir)
filename = os.path.join(args.plots_dir, 'command_line.json')
print(f"Archiving '{filename}' under '{args.plots_dir}'")
with open(filename, 'w') as jsonfile:
json.dump(args._get_kwargs(), jsonfile)
print(f"Archiving '{__file__}' under '{args.plots_dir}'")
shutil.copyfile(__file__, os.path.join(args.plots_dir, __file__))
###########################################################################
# Parameters to set for the experiment
hrf_time_frames = 30
nx = ny = nz = 10
lw = 7
###########################################################################
# Real data loading
t_r = 0.735
n_times_valid = args.n_time_frames - hrf_time_frames + 1
h = double_gamma_hrf(t_r, hrf_time_frames)
D = (np.eye(n_times_valid, k=-1) - np.eye(n_times_valid, k=0))[:, :-1]
H = make_toeplitz(h, n_times_valid).T
# load data
sub1_img = 'data/6025086_20227_MNI_RS.nii.gz'
sub2_img = 'data/6025837_20227_MNI_RS.nii.gz'
masker = NiftiMasker(standardize=True, detrend=True, low_pass=0.1,
high_pass=0.01, t_r=t_r, memory='__cache_dir__') # noqa: E128
masker.fit([sub1_img, sub2_img])
y_train = masker.inverse_transform(masker.transform(sub1_img)).get_data()
y_test = masker.inverse_transform(masker.transform(sub2_img)).get_data()
# reduce dimensionality
start_ = 10
mask_roi = (slice(start_, start_ + nx),
slice(start_, start_ + ny),
slice(start_, start_ + nz),
slice(0, args.n_time_frames))
y_train = y_train[mask_roi]
y_test = y_test[mask_roi]
# lbda-max scale data
y_train /= compute_lbda_max(H, y_train, per_sample=False)
y_test /= compute_lbda_max(H, y_test, per_sample=False)
print(f"Shape of the train-set : {y_train.shape}")
print(f"Shape of the test-set : {y_test.shape}")
###########################################################################
# Main experimentation
all_layers = logspace_layers(n_layers=10, max_depth=args.max_iter_z)
params = dict(t_r=t_r, h=h, n_times_valid=n_times_valid,
name='Iterative-z',
max_iter_z=int(args.iter_mult * args.max_iter_z),
solver_type='fista-z-step', verbose=1)
ta_iter = TA(**params)
t0 = time.time()
_, _, _ = ta_iter.prox_t(y_test, args.temp_reg)
print(f"ta_iterative.prox_t finished : {time.time() - t0:.2f}s")
loss_ta_iter = ta_iter.l_loss_prox_t
n_samples = nx * ny * nz
y_test_ravel = y_test.reshape(n_samples, args.n_time_frames)
_, u0, _ = init_vuz(H, D, y_test_ravel, args.temp_reg)
loss_ta_learn = [_obj_t_analysis(u0, y_test_ravel, h, args.temp_reg)]
init_net_params = None
params = dict(t_r=t_r, h=h, n_times_valid=n_times_valid,
net_solver_training_type='recursive',
name='Learned-z', solver_type='learn-z-step', verbose=1,
max_iter_training_net=args.max_training_iter)
for i, n_layers in enumerate(all_layers):
params['max_iter_z'] = n_layers
if args.load_net is not None:
# load and re-used pre-fitted parameters case
filename = sorted(args.load_net)[i] # order is important
with open(filename, 'rb') as pfile:
init_net_params = pickle.load(pfile)
print(f"Loading parameters from '{filename}'")
params['init_net_parameters'] = init_net_params
ta_learn = TA(**params)
else:
# fit parameters and save parameters case
params['init_net_parameters'] = init_net_params
ta_learn = TA(**params)
ta_learn.fit(y_train, args.temp_reg)
init_net_params = ta_learn.net_solver.export_parameters()
filename = f'fitted_params_n_layers_{n_layers:02d}.pkl'
filename = os.path.join(args.plots_dir, filename)
with open(filename, 'wb') as pfile:
pickle.dump(init_net_params, pfile)
print(f"Saving fitted parameters under '{filename}'")
t0 = time.time()
_, u, _ = ta_learn.prox_t(y_test, args.temp_reg, reshape_4d=False)
print(f"ta_learn.prox_t finished : {time.time() - t0:.2f}s")
loss_ta_learn.append(_obj_t_analysis(u, y_test_ravel, h,
args.temp_reg))
loss_ta_learn = np.array(loss_ta_learn)
###########################################################################
# Plotting
params = dict(t_r=t_r, h=h, n_times_valid=n_times_valid, max_iter_z=10000,
name='Ref-z', solver_type='fista-z-step', verbose=0)
ta_ref = TA(**params)
t0 = time.time()
_, _, _ = ta_ref.prox_t(y_test, args.temp_reg)
print(f"ta_ref.prox_t finished : {time.time() - t0:.2f}s")
min_loss = ta_ref.l_loss_prox_t[-1]
all_layers = [0] + all_layers
eps = 1.0e-20
plt.figure(f"[{__file__}] Loss functions", figsize=(6, 3))
xx = np.arange(start=0, stop=int(args.iter_mult * args.max_iter_z + 1))
plt.semilogy(xx, loss_ta_iter - min_loss, lw=lw, color='C1',
label='Accelerated PGD - analysis')
plt.semilogy(all_layers, loss_ta_learn - min_loss, lw=lw, color='C3',
label='LPGD-Taut')
plt.legend(bbox_to_anchor=(0.0, 1.02, 1.0, 0.2), loc="lower left",
mode="expand", borderaxespad=0, ncol=1, fontsize=18)
plt.grid()
plt.xlabel("Layers $t$")
plt.ylabel(r'$\mathbb E \left[P_x(u^{(t)}) - P_x(u^{*}) \right]$')
plt.tight_layout()
filename = os.path.join(args.plots_dir, "loss_comparison.pdf")
plt.savefig(filename, dpi=300)
delta_t = time.time() - t0_global
delta_t = time.strftime("%H h %M min %S s", time.gmtime(delta_t))
print("Script runs in: {}".format(delta_t))
plt.show()