imicpe 0.0.9.5__py3-none-any.whl → 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- imicpe/_version.py +1 -1
- imicpe/cs/__init__.py +9 -0
- imicpe/cs/l1.py +111 -0
- imicpe/cs/masks.py +80 -0
- imicpe/cs/metrics.py +7 -0
- imicpe/cs/operators.py +298 -0
- imicpe/cs/shepp_logan_phantom.py +21 -0
- imicpe/cs/tikhonov.py +95 -0
- {imicpe-0.0.9.5.dist-info → imicpe-1.0.0.dist-info}/METADATA +3 -2
- imicpe-1.0.0.dist-info/RECORD +20 -0
- {imicpe-0.0.9.5.dist-info → imicpe-1.0.0.dist-info}/WHEEL +1 -1
- imicpe-0.0.9.5.dist-info/RECORD +0 -13
- {imicpe-0.0.9.5.dist-info → imicpe-1.0.0.dist-info}/LICENSE +0 -0
- {imicpe-0.0.9.5.dist-info → imicpe-1.0.0.dist-info}/top_level.txt +0 -0
imicpe/_version.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__="0.0
|
|
1
|
+
__version__="1.0.0"
|
imicpe/cs/__init__.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from .metrics import mse, snr
|
|
2
|
+
from .operators import fwt, iwt, fwt2, iwt2
|
|
3
|
+
from .tikhonov import tikhonov
|
|
4
|
+
from .l1 import l1
|
|
5
|
+
from .shepp_logan_phantom import phantom_shepp_logan
|
|
6
|
+
from .masks import mat2mask, starPattern, getAcquisitionImage #, sub2ind, ind2sub
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
cameraman = os.path.join(os.path.dirname(__file__), 'cameraman.tif')
|
imicpe/cs/l1.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
|
|
2
|
+
import numpy as np
|
|
3
|
+
from scipy import ndimage
|
|
4
|
+
|
|
5
|
+
from .operators import *
|
|
6
|
+
from tqdm import tqdm_notebook as tqdm
|
|
7
|
+
|
|
8
|
+
def l1(opreg,A,At,z,x0,lam):
|
|
9
|
+
"""
|
|
10
|
+
l1 Algorithme Forward-Backward pour résoudre le problème
|
|
11
|
+
xhat = argmin ||Hx-z||_2^2 + lam.||Gx||_1
|
|
12
|
+
x
|
|
13
|
+
|
|
14
|
+
en particulier :
|
|
15
|
+
- le modèle LASSO si G = Id,
|
|
16
|
+
- le modèle TV si G = D (gradient) ou L (laplacien),
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
opreg (string) nom de l'opérateur G sur lequel opère la contrainte de parcimonie {'id', 'gradient', 'laplacien'}
|
|
20
|
+
A (fonction)
|
|
21
|
+
At (fonction)
|
|
22
|
+
z
|
|
23
|
+
x0 (numpy.ndarray)
|
|
24
|
+
lam (float)
|
|
25
|
+
|
|
26
|
+
Returns:
|
|
27
|
+
xhat (numpy.ndarray) solution du problème
|
|
28
|
+
loss (numpy.ndarray) évolution de la fonction de coût au cours des itérations
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
print('Running l1 model with ' +opreg+ ' sparsity constraint...\n\t')
|
|
32
|
+
|
|
33
|
+
### init ###
|
|
34
|
+
dim = x0.ndim
|
|
35
|
+
match opreg:
|
|
36
|
+
case 'id':
|
|
37
|
+
G = Id
|
|
38
|
+
Gt = Id
|
|
39
|
+
case 'gradient':
|
|
40
|
+
G = D
|
|
41
|
+
Gt = Dt
|
|
42
|
+
case 'laplacien':
|
|
43
|
+
G = L
|
|
44
|
+
Gt = Lt
|
|
45
|
+
|
|
46
|
+
# operator norms
|
|
47
|
+
lipA = opNorm(A,At,dim,x0)
|
|
48
|
+
lipG = opNorm(G,Gt,dim,x0)
|
|
49
|
+
|
|
50
|
+
# cost functions
|
|
51
|
+
def f(x): # data fidelity
|
|
52
|
+
return np.sum(x**2)/2
|
|
53
|
+
|
|
54
|
+
def R(x): # regularization
|
|
55
|
+
return np.sum(np.abs(x))
|
|
56
|
+
|
|
57
|
+
def E(x,lam): # total cost
|
|
58
|
+
return f(A(x)-z) + lam*R(G(x))
|
|
59
|
+
|
|
60
|
+
# proximity operator
|
|
61
|
+
def proxl1(x,gam):
|
|
62
|
+
return x - np.maximum(np.minimum(x,gam*np.ones(x.shape)),-gam*np.ones(x.shape))
|
|
63
|
+
|
|
64
|
+
### Algo ###
|
|
65
|
+
niter = 1e3; # max number of iterations
|
|
66
|
+
# model hyperparameters
|
|
67
|
+
mu = 5; # Bregman parameter (in [1,10], should not vary)
|
|
68
|
+
|
|
69
|
+
# algo hyperparameters
|
|
70
|
+
gamx = .9/(lipA**2 + mu*lipG**2); #.5e-1; # gradient descent step (x subproblem)
|
|
71
|
+
gamu = 1/mu; # proximal descent step (y subproblem)
|
|
72
|
+
|
|
73
|
+
# initialize variables
|
|
74
|
+
En = np.zeros((int(niter+1),),float) * np.nan
|
|
75
|
+
xn = x0 #np.random.standard_normal((z.shape))
|
|
76
|
+
un = G(xn) # splitting variable
|
|
77
|
+
bn = np.zeros(un.shape,float) # Bregman variable
|
|
78
|
+
|
|
79
|
+
En[0] = E(xn,lam)
|
|
80
|
+
|
|
81
|
+
# loop parameters
|
|
82
|
+
k = 0
|
|
83
|
+
tol = 1e-10
|
|
84
|
+
stop_crit = En[0]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
with tqdm(total=niter) as pbar:
|
|
88
|
+
while (k < niter) and (stop_crit > tol):
|
|
89
|
+
# yn subproblem
|
|
90
|
+
Gxn = G(xn)
|
|
91
|
+
un = proxl1(un - gamu*mu*(un-Gxn-bn/mu) , lam*gamu)
|
|
92
|
+
|
|
93
|
+
# xn subproblem (relaxed): gradient descent step instead of GS iteration
|
|
94
|
+
xn = xn - gamx*( At(A(xn)-z) - mu*Gt(un-Gxn-bn/mu) )
|
|
95
|
+
|
|
96
|
+
# bn subproblem
|
|
97
|
+
bn = bn - mu*(un-G(xn))
|
|
98
|
+
|
|
99
|
+
# compute loss
|
|
100
|
+
En[k+1] = E(xn,lam)
|
|
101
|
+
|
|
102
|
+
# update loop parameters
|
|
103
|
+
stop_crit = (En[k] - En[k+1])/En[k]
|
|
104
|
+
k += 1
|
|
105
|
+
pbar.update(1)
|
|
106
|
+
|
|
107
|
+
pbar.close()
|
|
108
|
+
xhat = xn
|
|
109
|
+
loss = En
|
|
110
|
+
|
|
111
|
+
return xhat, loss
|
imicpe/cs/masks.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
|
|
2
|
+
import numpy as np
|
|
3
|
+
from scipy import ndimage
|
|
4
|
+
import pywt
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
mat2mask = lambda mat, H, W, M: np.reshape(mat.T, (H, W, M))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def starPattern(N, M):
|
|
11
|
+
"""
|
|
12
|
+
starPattern Génère un masque de taille NxN en étoile (tomographie) correspondant à M mesures.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
N (int) Taille du masque
|
|
16
|
+
M (int) Nombre de mesures
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
Amat (numpy.ndarray) Matrice d'acquisition
|
|
20
|
+
mask (numpy.ndarray) Masque
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
mask2mat = lambda mask: np.reshape(mask, (M, N**2))
|
|
25
|
+
|
|
26
|
+
H = int(N)
|
|
27
|
+
W = int(N)
|
|
28
|
+
|
|
29
|
+
n = int(N)
|
|
30
|
+
r = np.linspace(-1, 1, 3*n)*n
|
|
31
|
+
|
|
32
|
+
nrho = 2**4
|
|
33
|
+
R = np.round(np.linspace(-n/2, n/2, nrho))#.astype(int)
|
|
34
|
+
|
|
35
|
+
ntheta = M//nrho
|
|
36
|
+
T = np.linspace(0, np.pi, ntheta+1, endpoint=False)
|
|
37
|
+
|
|
38
|
+
mask = np.zeros((H, W, ntheta, nrho))
|
|
39
|
+
for itt in range(ntheta):
|
|
40
|
+
theta = T[itt]
|
|
41
|
+
|
|
42
|
+
for itr in range(nrho):
|
|
43
|
+
rho = R[itr]
|
|
44
|
+
|
|
45
|
+
x = np.round(r*np.cos(theta) + n/2 - rho*np.sin(theta))#.astype(int)
|
|
46
|
+
y = np.round(r*np.sin(theta) + n/2 + rho*np.cos(theta))#.astype(int)
|
|
47
|
+
|
|
48
|
+
valid = np.where((x >= 0) & (x < n) & (y >= 0) & (y < n))
|
|
49
|
+
x = x[valid].astype(int)
|
|
50
|
+
y = y[valid].astype(int)
|
|
51
|
+
|
|
52
|
+
tmpM = np.zeros((H, W))
|
|
53
|
+
tmpM[y, x] = 1
|
|
54
|
+
|
|
55
|
+
mask[:, :, itt, itr] = tmpM
|
|
56
|
+
|
|
57
|
+
mask = mask.reshape((H, W, M))
|
|
58
|
+
Amat = mask2mat(mask)
|
|
59
|
+
|
|
60
|
+
return Amat, mask
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def getAcquisitionImage(x,mask):
|
|
64
|
+
_, _, Nmeasures = mask.shape
|
|
65
|
+
|
|
66
|
+
zim = np.sum(mask * np.tile(x[..., None], (1, 1, Nmeasures)), axis=2)
|
|
67
|
+
zim = zim / np.max(zim)
|
|
68
|
+
|
|
69
|
+
return zim
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
# def sub2ind(array_shape, rows, cols):
|
|
74
|
+
# ind = rows*array_shape[1] + cols
|
|
75
|
+
# return ind.astype(int)
|
|
76
|
+
|
|
77
|
+
# def ind2sub(array_shape, ind):
|
|
78
|
+
# rows = (ind.astype('int') / array_shape[1])
|
|
79
|
+
# cols = (ind.astype('int') % array_shape[1]) # or numpy.mod(ind.astype('int'), array_shape[1])
|
|
80
|
+
# return (int(rows), int(cols))
|
imicpe/cs/metrics.py
ADDED
imicpe/cs/operators.py
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
|
|
2
|
+
import numpy as np
|
|
3
|
+
from scipy import ndimage
|
|
4
|
+
import pywt
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
############################################################
|
|
8
|
+
## identity operator
|
|
9
|
+
############################################################
|
|
10
|
+
def Id(x):
|
|
11
|
+
"""
|
|
12
|
+
Id Opérateur identité
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
X (numpy.ndarray) signal 1D
|
|
16
|
+
ou: image non vectorisée 2D
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
(numpy.ndarray) X
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
return x
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
############################################################
|
|
26
|
+
## differential forward and backward operators
|
|
27
|
+
############################################################
|
|
28
|
+
# gradient
|
|
29
|
+
def D(x):
|
|
30
|
+
"""
|
|
31
|
+
D Calcule le gradient par différences finies à droite.
|
|
32
|
+
Autrement dit, D(x) calcule le produit matriciel Dx.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
X (numpy.ndarray) signal 1D
|
|
36
|
+
ou: image non vectorisée 2D
|
|
37
|
+
|
|
38
|
+
Returns:
|
|
39
|
+
(numpy.ndarray) Gradient de X
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
if x.ndim == 1:
|
|
43
|
+
grad = np.concatenate((x[1:] - x[:-1], [0]))/2.
|
|
44
|
+
|
|
45
|
+
elif x.ndim == 2:
|
|
46
|
+
sz = x.shape
|
|
47
|
+
Dx_im = np.concatenate(( x[:,1:] - x[:,:-1] , np.zeros((sz[0],1)) ), axis=1)/ 2.
|
|
48
|
+
Dy_im = np.concatenate(( x[1:,:] - x[:-1,:] , np.zeros((1,sz[1])) ), axis=0)/ 2.
|
|
49
|
+
|
|
50
|
+
grad = np.array([Dx_im,Dy_im])
|
|
51
|
+
return grad
|
|
52
|
+
|
|
53
|
+
def Dt(x):
|
|
54
|
+
"""
|
|
55
|
+
Dt Calcule l’adjoint gradient par différences finies à droite.
|
|
56
|
+
Autrement dit, Dt(x) calcule le produit matriciel D'x.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
X (numpy.ndarray) signal 1D
|
|
60
|
+
ou: image non vectorisée 2D
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
(numpy.ndarray) Divergence de X
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
if x.ndim == 1:
|
|
67
|
+
div = - np.concatenate(([x[0]], x[1:-1] - x[:-2], [-x[-2]])) /2.
|
|
68
|
+
|
|
69
|
+
elif x.ndim == 3:
|
|
70
|
+
x1 = x[0]
|
|
71
|
+
x2 = x[1]
|
|
72
|
+
div = - np.concatenate((x1[:,[0]], x1[:,1:-1] - x1[:,:-2], -x1[:,[-2]]), axis=1) /2. \
|
|
73
|
+
- np.concatenate((x2[[0],:], x2[1:-1,:] - x2[:-2,:], -x2[[-2],:]), axis=0) /2.
|
|
74
|
+
return div
|
|
75
|
+
|
|
76
|
+
# laplacian
|
|
77
|
+
def L(x):
|
|
78
|
+
"""
|
|
79
|
+
L Calcule la dérivée seconde d’un signal, ou le laplacien dans le cas d’une image.
|
|
80
|
+
Autrement dit, L(x) calcule le produit matriciel Lx.
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
X (numpy.ndarray) signal 1D
|
|
84
|
+
ou: image non vectorisée 2D
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
(numpy.ndarray) Laplacien de X
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
if x.ndim == 1:
|
|
91
|
+
ker = np.array([1, -2, 1])
|
|
92
|
+
#lap = np.convolve(x,ker,'same')
|
|
93
|
+
lap = ndimage.convolve1d(x,ker,mode='nearest')
|
|
94
|
+
elif x.ndim == 2:
|
|
95
|
+
ker = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) # V4
|
|
96
|
+
#ker = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]]) # V8
|
|
97
|
+
lap = ndimage.convolve(x,ker,mode='nearest')
|
|
98
|
+
return lap
|
|
99
|
+
|
|
100
|
+
def Lt(x):
|
|
101
|
+
"""
|
|
102
|
+
Lt Calcule l’adjoint du laplacien.
|
|
103
|
+
Autrement dit, Lt(x) calcule le produit matriciel L'x.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
X (numpy.ndarray) signal 1D
|
|
107
|
+
ou: image non vectorisée 2D
|
|
108
|
+
|
|
109
|
+
Returns:
|
|
110
|
+
(numpy.ndarray) Adjoint du Laplacien de X
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
if x.ndim == 1:
|
|
114
|
+
ker = np.array([1, -2, 1])
|
|
115
|
+
#lap = np.correlate(x,ker,'same')
|
|
116
|
+
lap = ndimage.correlate1d(x,ker,mode='nearest')
|
|
117
|
+
elif x.ndim == 2:
|
|
118
|
+
ker = np.array([[0, 1, 0], [1, -4, 1], [0, 1, 0]]) # V4
|
|
119
|
+
#ker = np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]]) # V8
|
|
120
|
+
lap = ndimage.correlate(x,ker,mode='nearest')
|
|
121
|
+
return lap
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
############################################################
|
|
125
|
+
## Wavelet transforms
|
|
126
|
+
############################################################
|
|
127
|
+
def fwt(x,wavelet,level):
|
|
128
|
+
"""
|
|
129
|
+
P Calcule la transformée en ondelettes directe 1D.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
x (numpy.ndarray) signal 1D
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
(numpy.ndarray) Vecteur des coefficients de la décomposition en ondelettes de x
|
|
136
|
+
"""
|
|
137
|
+
|
|
138
|
+
dim = x.ndim
|
|
139
|
+
|
|
140
|
+
coeffs = pywt.wavedec(x, wavelet, level=level, mode="periodization")
|
|
141
|
+
coeff_arr, _, _ = pywt.ravel_coeffs(coeffs)
|
|
142
|
+
|
|
143
|
+
return coeff_arr
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def iwt(x,wavelet,level,coeffs_slices=None,coeffs_shapes=None,):
|
|
147
|
+
"""
|
|
148
|
+
P Calcule la transformée en ondelettes inverse 1D.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
x (numpy.ndarray) Vecteur des coefficients d'ondelettes
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
(numpy.ndarray) Signal correspondant aux coefficients d'ondelettes donnés par x
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
J = level
|
|
158
|
+
N = len(x)
|
|
159
|
+
|
|
160
|
+
if coeffs_shapes is None:
|
|
161
|
+
# compute coeffs size at each level
|
|
162
|
+
sizes = [N // (2**j) for j in range(J, 0, -1)] + [N // (2**J)]
|
|
163
|
+
|
|
164
|
+
# coefficients splitting
|
|
165
|
+
start = 0
|
|
166
|
+
coeffs = []
|
|
167
|
+
for size in reversed(sizes):
|
|
168
|
+
coeffs.append(x[start:start + size])
|
|
169
|
+
start += size
|
|
170
|
+
else:
|
|
171
|
+
coeffs = pywt.unravel_coeffs(x, coeffs_slices, coeffs_shapes, output_format='wavedec')
|
|
172
|
+
|
|
173
|
+
# reconstruct corresponding signal
|
|
174
|
+
signal = pywt.waverec(coeffs, wavelet, mode="periodization")
|
|
175
|
+
|
|
176
|
+
return signal
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def fwt2(x,wavelet,level):
|
|
180
|
+
"""
|
|
181
|
+
P Calcule la transformée en ondelettes directe 2D.
|
|
182
|
+
|
|
183
|
+
Args:
|
|
184
|
+
x (numpy.ndarray) image NON vectorisée 2D
|
|
185
|
+
|
|
186
|
+
Returns:
|
|
187
|
+
(numpy.ndarray) Vecteur des coefficients de la décomposition en ondelettes de x
|
|
188
|
+
"""
|
|
189
|
+
|
|
190
|
+
coeffs = pywt.wavedec2(x, wavelet, level=level, mode="periodization")
|
|
191
|
+
coeff_arr, _, _ = pywt.ravel_coeffs(coeffs)
|
|
192
|
+
|
|
193
|
+
return coeff_arr
|
|
194
|
+
|
|
195
|
+
def iwt2(x,wavelet,level,coeffs_slices,coeffs_shapes):
|
|
196
|
+
"""
|
|
197
|
+
P Calcule la transformée en ondelettes inverse 2D.
|
|
198
|
+
|
|
199
|
+
Args:
|
|
200
|
+
x (numpy.ndarray) Vecteur des coefficients d'ondelettes
|
|
201
|
+
|
|
202
|
+
Returns:
|
|
203
|
+
(numpy.ndarray) Image correspondante aux coefficients d'ondelettes donnés par x
|
|
204
|
+
"""
|
|
205
|
+
|
|
206
|
+
J = level
|
|
207
|
+
N = len(x)
|
|
208
|
+
|
|
209
|
+
coeffs = pywt.unravel_coeffs(x, coeffs_slices, coeffs_shapes,
|
|
210
|
+
output_format='wavedec2')
|
|
211
|
+
|
|
212
|
+
# reconstruct corresponding signal
|
|
213
|
+
image = pywt.waverec2(coeffs, wavelet, mode="periodization")
|
|
214
|
+
|
|
215
|
+
return image
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
############################################################
|
|
219
|
+
## Operator and matrix norm
|
|
220
|
+
############################################################
|
|
221
|
+
def opNorm(op,opt,dim,xn):
|
|
222
|
+
"""
|
|
223
|
+
opNorm Calcule la norme de l'opérateur OP, dont
|
|
224
|
+
l'opérateur transposé est OPT, en dimension DIM
|
|
225
|
+
|
|
226
|
+
Args:
|
|
227
|
+
OP (function) opérateur direct
|
|
228
|
+
OPT (function) opérateur adjoint
|
|
229
|
+
DIM (int) 1 or 2
|
|
230
|
+
|
|
231
|
+
Returns:
|
|
232
|
+
(float) norme de l'opérateur OP
|
|
233
|
+
"""
|
|
234
|
+
|
|
235
|
+
def T(x):
|
|
236
|
+
return opt(op(x))
|
|
237
|
+
|
|
238
|
+
# match dim:
|
|
239
|
+
# case 1:
|
|
240
|
+
# xn = np.random.standard_normal((64))
|
|
241
|
+
# case 2:
|
|
242
|
+
# xn = np.random.standard_normal((64,64))
|
|
243
|
+
|
|
244
|
+
xnn = xn
|
|
245
|
+
|
|
246
|
+
n = np.zeros((1000,),float)
|
|
247
|
+
n[1] = 1
|
|
248
|
+
tol = 1e-4
|
|
249
|
+
rhon = n[1]+2*tol
|
|
250
|
+
|
|
251
|
+
k = 1
|
|
252
|
+
while abs(n[k]-rhon)/n[k] >= tol:
|
|
253
|
+
xn = T(xnn)
|
|
254
|
+
xnn = T(xn)
|
|
255
|
+
|
|
256
|
+
rhon = n[k]
|
|
257
|
+
n[k+1] = np.sum(xnn**2)/np.sum(xn**2)
|
|
258
|
+
|
|
259
|
+
k = k+1
|
|
260
|
+
|
|
261
|
+
N = n[k-1] + 1e-16
|
|
262
|
+
return 1.01* N**(.25) # sqrt(L) gives |||T|||=|||D'D||| ie |||D|||^2
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def matNorm(M):
|
|
266
|
+
"""
|
|
267
|
+
matNorm Calcule la norme de la matrice M
|
|
268
|
+
|
|
269
|
+
Args:
|
|
270
|
+
M (numpy.ndarray) matrice dont on souhaite calculer la norme
|
|
271
|
+
|
|
272
|
+
Returns:
|
|
273
|
+
(float) norme de la matrice M
|
|
274
|
+
"""
|
|
275
|
+
|
|
276
|
+
def T(x):
|
|
277
|
+
return np.dot(M.T, np.dot(M,x))
|
|
278
|
+
|
|
279
|
+
xn = np.random.standard_normal((M.shape[1]))
|
|
280
|
+
xnn = xn
|
|
281
|
+
|
|
282
|
+
n = np.zeros((1000,),float)
|
|
283
|
+
n[1] = 1
|
|
284
|
+
tol = 1e-4
|
|
285
|
+
rhon = n[1]+2*tol
|
|
286
|
+
|
|
287
|
+
k = 1
|
|
288
|
+
while abs(n[k]-rhon)/n[k] >= tol:
|
|
289
|
+
xn = T(xnn)
|
|
290
|
+
xnn = T(xn)
|
|
291
|
+
|
|
292
|
+
rhon = n[k]
|
|
293
|
+
n[k+1] = np.sum(xnn**2)/np.sum(xn**2)
|
|
294
|
+
|
|
295
|
+
k = k+1
|
|
296
|
+
|
|
297
|
+
N = n[k-1] + 1e-16
|
|
298
|
+
return 1.01* N**(.25) # sqrt(L) gives |||T|||=|||D'D||| ie |||D|||^2
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
|
|
2
|
+
import numpy as np
|
|
3
|
+
|
|
4
|
+
from skimage.data import shepp_logan_phantom
|
|
5
|
+
from skimage.transform import rescale, resize
|
|
6
|
+
|
|
7
|
+
def phantom_shepp_logan(N):
|
|
8
|
+
"""
|
|
9
|
+
phantom_shepp_logan Génère le phantom de Shepp-Logan 2D de taille NxN.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
N (int) Taille du phantom
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
(numpy.ndarray) Image du phantom
|
|
16
|
+
"""
|
|
17
|
+
p = shepp_logan_phantom()
|
|
18
|
+
p = resize(p, (int(N),int(N)), anti_aliasing=False)
|
|
19
|
+
p[p<1e-10] = .1
|
|
20
|
+
|
|
21
|
+
return p
|
imicpe/cs/tikhonov.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
|
|
2
|
+
import numpy as np
|
|
3
|
+
from scipy import ndimage
|
|
4
|
+
|
|
5
|
+
from .operators import *
|
|
6
|
+
from tqdm import tqdm_notebook as tqdm
|
|
7
|
+
|
|
8
|
+
def tikhonov(opreg,A,At,z,x0,lam):
|
|
9
|
+
"""
|
|
10
|
+
tikhonov Algorithme de descente de gradient pour résoudre le problème
|
|
11
|
+
xhat = argmin ||Hx-z||_2^2 + lam.||Gx||_2^2
|
|
12
|
+
x
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
opreg (string) nom de l'opérateur G sur lequel opère la contrainte de parcimonie {'id', 'gradient', 'laplacien'}
|
|
17
|
+
A (fonction)
|
|
18
|
+
At (fonction)
|
|
19
|
+
z
|
|
20
|
+
x0 (numpy.ndarray)
|
|
21
|
+
lam (float)
|
|
22
|
+
|
|
23
|
+
Returns:
|
|
24
|
+
xhat (numpy.ndarray) solution du problème
|
|
25
|
+
loss (numpy.ndarray) évolution de la fonction de coût au cours des itérations
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
print('Running Tikhonov model with ' +opreg+ ' sparsity constraint...\n\t')
|
|
29
|
+
|
|
30
|
+
### init ###
|
|
31
|
+
dim = x0.ndim
|
|
32
|
+
match opreg:
|
|
33
|
+
case 'id':
|
|
34
|
+
G = Id
|
|
35
|
+
Gt = Id
|
|
36
|
+
case 'gradient':
|
|
37
|
+
G = D
|
|
38
|
+
Gt = Dt
|
|
39
|
+
case 'laplacien':
|
|
40
|
+
G = L
|
|
41
|
+
Gt = Lt
|
|
42
|
+
|
|
43
|
+
# operator norms
|
|
44
|
+
lipA = opNorm(A,At,dim,x0)
|
|
45
|
+
lipG = opNorm(G,Gt,dim,x0)
|
|
46
|
+
|
|
47
|
+
# cost functions
|
|
48
|
+
def f(x): # data fidelity
|
|
49
|
+
return np.sum(x**2)/2
|
|
50
|
+
|
|
51
|
+
def R(x): # regularization
|
|
52
|
+
return np.sum(x**2)/2
|
|
53
|
+
|
|
54
|
+
def E(x,lam): # total cost
|
|
55
|
+
return f(A(x)-z) + lam*R(G(x))
|
|
56
|
+
|
|
57
|
+
# proximity operator
|
|
58
|
+
def proxl1(x,gam):
|
|
59
|
+
return x - np.maximum(np.minimum(x,gam*np.ones(x.shape)),-gam*np.ones(x.shape))
|
|
60
|
+
|
|
61
|
+
### Algo ###
|
|
62
|
+
niter = 1e3; # max number of iterations
|
|
63
|
+
|
|
64
|
+
# algo hyperparameters
|
|
65
|
+
gam = .9/(2*lipA**2 + 2*lam*lipG**2); # gradient descent step
|
|
66
|
+
|
|
67
|
+
# initialize variables
|
|
68
|
+
En = np.zeros((int(niter+1),),float) * np.nan
|
|
69
|
+
xn = x0 #np.random.standard_normal((z.shape))
|
|
70
|
+
|
|
71
|
+
En[0] = E(xn,lam)
|
|
72
|
+
|
|
73
|
+
# loop parameters
|
|
74
|
+
k = 0
|
|
75
|
+
tol = 1e-10
|
|
76
|
+
stop_crit = En[0]
|
|
77
|
+
|
|
78
|
+
with tqdm(total=niter) as pbar:
|
|
79
|
+
while (k < niter) and (stop_crit > tol):
|
|
80
|
+
# xn subproblem
|
|
81
|
+
xn = xn - 2*gam*(At(A(xn)-z) + lam*Gt(G(xn)))
|
|
82
|
+
|
|
83
|
+
# compute loss
|
|
84
|
+
En[k+1] = E(xn,lam)
|
|
85
|
+
|
|
86
|
+
# update loop parameters
|
|
87
|
+
stop_crit = (En[k] - En[k+1])/En[k]
|
|
88
|
+
k += 1
|
|
89
|
+
pbar.update(1)
|
|
90
|
+
|
|
91
|
+
pbar.close()
|
|
92
|
+
xhat = xn
|
|
93
|
+
loss = En
|
|
94
|
+
|
|
95
|
+
return xhat, loss
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: imicpe
|
|
3
|
-
Version: 0.0
|
|
3
|
+
Version: 1.0.0
|
|
4
4
|
Summary: Toolbox for Maths,Signal,Image Teaching @ CPE
|
|
5
5
|
Author-email: Marion Foare <marion.foare@cpe.fr>, Eric Van Reeth <eric.vanreeth@cpe.fr>, Arthur Gautheron <arthur.gautheron@cpe.fr>
|
|
6
6
|
License: MIT License
|
|
@@ -18,6 +18,8 @@ Requires-Dist: plotly
|
|
|
18
18
|
Requires-Dist: torch
|
|
19
19
|
Requires-Dist: torchvision
|
|
20
20
|
Requires-Dist: tqdm
|
|
21
|
+
Requires-Dist: PyWavelets
|
|
22
|
+
Requires-Dist: scikit-image
|
|
21
23
|
|
|
22
24
|
|
|
23
25
|
A toolbox used for practical sessions at [CPE Lyon](https://www.cpe.fr/).
|
|
@@ -36,7 +38,6 @@ Developped and maintained for teaching usage only!
|
|
|
36
38
|
# Usage example
|
|
37
39
|
|
|
38
40
|
The example below uses the mse method available in the `optim.metrics` subpackage of `imicpe`.
|
|
39
|
-
It requires `numpy.randn` to generate a gaussian distribution of N points.
|
|
40
41
|
|
|
41
42
|
```python
|
|
42
43
|
import numpy as np
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
imicpe/__init__.py,sha256=WjDmvecyDIyJLYp4rCV9vsSYbQDc4L1EpYqORvEXliI,33
|
|
2
|
+
imicpe/_version.py,sha256=xZawFFh80uj7MHMJJXWFcuzkX29fVkHuS5NT5dJMCf8,20
|
|
3
|
+
imicpe/cs/__init__.py,sha256=fLe1o5jNvrB4qEVBE6amEV9i3NFwM3E_uGiiiHXrph0,339
|
|
4
|
+
imicpe/cs/l1.py,sha256=3Bq3Ryk1mRAvti0lVR4u6Lj7aq2zr_Qxye0_doO8n_0,3266
|
|
5
|
+
imicpe/cs/masks.py,sha256=5ehQ-QnYY4trBt7BIx0Pp8DUa3x5KApkc7TPp9LA9Pc,2030
|
|
6
|
+
imicpe/cs/metrics.py,sha256=DuUYpW7s1LkZTDV32D5K4wdSRXpH1Ymunjp2vq6kzfA,148
|
|
7
|
+
imicpe/cs/operators.py,sha256=r11Zvote0q4zcn_r_wRwcboKCNBNYwHY7wKrNcrfF30,8379
|
|
8
|
+
imicpe/cs/shepp_logan_phantom.py,sha256=_RDHimZI6R8uyAwXNyB3BldYjCiNDRorDmPYoJR0T6Q,485
|
|
9
|
+
imicpe/cs/tikhonov.py,sha256=6msVAXrzRBVHMIdh1mK6pf1zRjhnsdhDAQwEOpA-PuU,2518
|
|
10
|
+
imicpe/optim/__init__.py,sha256=rkguPFq7gdmBgm_Xrry2yr1oquEIxE00On2emjRMgZE,344
|
|
11
|
+
imicpe/optim/metrics.py,sha256=AHqudKDch1_jc7X2_9hz30RamEqfQ132GXr3qW81VPY,187
|
|
12
|
+
imicpe/optim/operators.py,sha256=BbyqE9ARZF-q8EkxyDOv1V8Rlz52t9sbo2gI22zQAzA,10648
|
|
13
|
+
imicpe/optim/pnnDataset.py,sha256=PFh5u0SXx761O6N6vVfWfEDmzmCm87eYzAL7mWHBRrw,3971
|
|
14
|
+
imicpe/optim/pnnTrainer.py,sha256=3ygh9XwFJN7jMPsEG3LHLa8sgBbDd9Yu_QWg4ZtM_DM,4571
|
|
15
|
+
imicpe/optim/pnnUtils.py,sha256=LXU7wRfuEi6t-2VG2NStPAOzu19OKTxzNNnbHESpC2U,1523
|
|
16
|
+
imicpe-1.0.0.dist-info/LICENSE,sha256=ACwmltkrXIz5VsEQcrqljq-fat6ZXAMepjXGoe40KtE,1069
|
|
17
|
+
imicpe-1.0.0.dist-info/METADATA,sha256=mlyMGSScoX_Y8oN3l7Gp6u32-6J5Dpvynk6ao1qD29o,1317
|
|
18
|
+
imicpe-1.0.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
19
|
+
imicpe-1.0.0.dist-info/top_level.txt,sha256=6_gSXCYolzjXHaIDeAsZ_M3nLXdqrMKt48XCz3reJc0,7
|
|
20
|
+
imicpe-1.0.0.dist-info/RECORD,,
|
imicpe-0.0.9.5.dist-info/RECORD
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
imicpe/__init__.py,sha256=WjDmvecyDIyJLYp4rCV9vsSYbQDc4L1EpYqORvEXliI,33
|
|
2
|
-
imicpe/_version.py,sha256=jrzhItrMTlop270sQHhvNJ5msaGXzNktYThtUK1YOEA,22
|
|
3
|
-
imicpe/optim/__init__.py,sha256=rkguPFq7gdmBgm_Xrry2yr1oquEIxE00On2emjRMgZE,344
|
|
4
|
-
imicpe/optim/metrics.py,sha256=AHqudKDch1_jc7X2_9hz30RamEqfQ132GXr3qW81VPY,187
|
|
5
|
-
imicpe/optim/operators.py,sha256=BbyqE9ARZF-q8EkxyDOv1V8Rlz52t9sbo2gI22zQAzA,10648
|
|
6
|
-
imicpe/optim/pnnDataset.py,sha256=PFh5u0SXx761O6N6vVfWfEDmzmCm87eYzAL7mWHBRrw,3971
|
|
7
|
-
imicpe/optim/pnnTrainer.py,sha256=3ygh9XwFJN7jMPsEG3LHLa8sgBbDd9Yu_QWg4ZtM_DM,4571
|
|
8
|
-
imicpe/optim/pnnUtils.py,sha256=LXU7wRfuEi6t-2VG2NStPAOzu19OKTxzNNnbHESpC2U,1523
|
|
9
|
-
imicpe-0.0.9.5.dist-info/LICENSE,sha256=ACwmltkrXIz5VsEQcrqljq-fat6ZXAMepjXGoe40KtE,1069
|
|
10
|
-
imicpe-0.0.9.5.dist-info/METADATA,sha256=KFIxIFaw_r0M9qZEbbWIN_hjARFtSh6_j1qAjr0DkEw,1340
|
|
11
|
-
imicpe-0.0.9.5.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
|
12
|
-
imicpe-0.0.9.5.dist-info/top_level.txt,sha256=6_gSXCYolzjXHaIDeAsZ_M3nLXdqrMKt48XCz3reJc0,7
|
|
13
|
-
imicpe-0.0.9.5.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|