superr.py 9 KB
Newer Older
1
2
3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
4
5
6
import numpy as np
import h5py
import math
7
from six.moves import range
8
myrank=0
9
10
11
try:
    import skimage.restoration
except:
Alessandro Mirone's avatar
Alessandro Mirone committed
12
    print( " ATTENTION : SKIMAGE RESORATION NOT LOADED ")
13

Alessandro Mirone's avatar
written    
Alessandro Mirone committed
14
15
16
17
18
19
20
21



def mdiv(grad):
    res = np.zeros(grad.shape[1:])
    res[ :-1  , :, :  ] += this_grad[0, :-1  , :,:]
    res[ 1:-1 , :, :  ] -= this_grad[0, :-2  , :,:]
    res[ -1    , :, : ] -= this_grad[0,  -2  , :,:]
22
23
24
25
26
27
    res[ :, :-1  , :  ] += this_grad[1, :, :-1  ,:]
    res[ :, 1:-1 , :  ] -= this_grad[1, :, :-2  ,:]
    res[ :, -1   , : ]  -= this_grad[1, :,  -2  ,:]
    res[ :, : , :-1   ] += this_grad[1, : ,:, :-1 ]
    res[ :, : , 1:-1  ] -= this_grad[1, : ,:, :-2 ]
    res[ :, : , -1   ]  -= this_grad[1, : ,:,  -2 ]
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
28
29
    return res
def mygradient(img):
30
    shape = [3 ] + list(img.shape)
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
31
32
33
    gradient = np.zeros(shape, dtype=img.dtype)
    gradient[0,:,:,: ] = np.diff(img, axis=0)
    gradient[1,:,:,: ] = np.diff(img, axis=1)
34
    gradient[2,:,:,: ] = np.diff(img, axis=2)
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
35
36
37
38
    return gradient
def v_project(v,weight ):
    norms = np.minimum( weight, np.sqrt(  v[0]*v[0] + v[1]*v[1]     ))
    return v/ norms
Christoph Sahle's avatar
Christoph Sahle committed
39
def my_denoise_tv_chambolle_positive(image, weight=0.1,  n_iter_max=200):
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    ndim = image.ndim
    g = np.zeros_like(p)
    x       =   np.zeros_like(image)
    tmpxa   =   np.zeros_like(image)
    v = np.zeros((image.ndim, ) + image.shape, dtype=image.dtype)
    i = 0
    sigma = 1.0/math.sqrt(8.0)
    tau   = 1.0/math.sqrt(8.0)
    while i < n_iter_max:
        tmpxa[:] = x + sigma * (  ( image-x)       + mydiv(  v   ) )
        tmpxa[:] = np.maximum (tmpxa)
        tmpxa[:] = tmpxa-x
        x[:]     =  x +  tmpxa
        tmpxa[:] =  x +  tmpxa
        v[:]  = v + tau * mygrad(tmpxa)
        v = v_project(v,weight )
    return x
        

59
def _denoise_tv_chambolle_nd(image, weight=0.1, eps=2.e-4, n_iter_max=200,
60
                             positivity=False, cdim = None):
61
    """Perform total-variation denoising on n-dimensional images.
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
62

63
64
65
66
67
68
69
70
71
72
    Parameters
    ----------
    image : ndarray
        n-D input data to be denoised.
    weight : float, optional
        Denoising weight. The greater `weight`, the more denoising (at
        the expense of fidelity to `input`).
    eps : float, optional
        Relative difference of the value of the cost function that determines
        the stop criterion. The algorithm stops when:
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
73

74
            (E_(n-1) - E_n) < eps * E_0
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
75

76
77
    n_iter_max : int, optional
        Maximal number of iterations used for the optimization.
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
78

79
80
    positivity : bool, optional
        Adds positivity constraint
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
81

82
83
84
85
86
87
88
89
90
    Returns
    -------
    out : ndarray
        Denoised array of floats.

    Notes
    -----
    Rudin, Osher and Fatemi algorithm.

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
    LICENCE
    -------

                    Copyright (C) 2011, the scikit-image team
                    All rights reserved.
                    
                    Redistribution and use in source and binary forms, with or without
                    modification, are permitted provided that the following conditions are
                    met:
                    
                     1. Redistributions of source code must retain the above copyright
                        notice, this list of conditions and the following disclaimer.
                     2. Redistributions in binary form must reproduce the above copyright
                        notice, this list of conditions and the following disclaimer in
                        the documentation and/or other materials provided with the
                        distribution.
                     3. Neither the name of skimage nor the names of its contributors may be
                        used to endorse or promote products derived from this software without
                        specific prior written permission.
                    
                    this software is provided by the author ``as is'' and any express or
                    implied warranties, including, but not limited to, the implied
                    warranties of merchantability and fitness for a particular purpose are
                    disclaimed. in no event shall the author be liable for any direct,
                    indirect, incidental, special, exemplary, or consequential damages
                    (including, but not limited to, procurement of substitute goods or
                    services; loss of use, data, or profits; or business interruption)
                    however caused and on any theory of liability, whether in contract,
                    strict liability, or tort (including negligence or otherwise) arising
                    in any way out of the use of this software, even if advised of the
                    possibility of such damage.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    """

    ndim = image.ndim
    p = np.zeros((image.ndim, ) + image.shape, dtype=image.dtype)
    g = np.zeros_like(p)
    d = np.zeros_like(image)
    i = 0
    while i < n_iter_max:
        if i > 0:
            # d will be the (negative) divergence of p
            d = -p.sum(0)
            slices_d = [slice(None), ] * ndim
            slices_p = [slice(None), ] * (ndim + 1)
            for ax in range(ndim):
                slices_d[ax] = slice(1, None)
                slices_p[ax+1] = slice(0, -1)
                slices_p[0] = ax
142
143
144
145
                if cdim is None:
                    d[tuple(slices_d)] += p[tuple(slices_p)]
                else:
                    d[tuple(slices_d)] += p[tuple(slices_p)]  *cdim[ax]                    
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
                slices_d[ax] = slice(None)
                slices_p[ax+1] = slice(None)
            out_nopos = image + d
        else:
            out_nopos = image

        if not positivity:
            out = out_nopos
        else:
            out = np.maximum(0, out_nopos)
            removed = np.minimum(out_nopos, 0) 
            d = d-removed

        E = (d ** 2).sum()

        # g stores the gradients of out along each axis
        # e.g. g[0] is the first order finite difference along axis 0
        slices_g = [slice(None), ] * (ndim + 1)
        for ax in range(ndim):
            slices_g[ax+1] = slice(0, -1)
            slices_g[0] = ax
167
168
169
170
171
            if cdim is None:
                g[tuple(slices_g)] = np.diff(out, axis=ax)
            else:
                g[tuple(slices_g)] = np.diff(out, axis=ax)*cdim[ax]
                
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
            slices_g[ax+1] = slice(None)

        norm = np.sqrt((g ** 2).sum(axis=0))[np.newaxis, ...]
        E += weight * norm.sum()
        tau = 1. / (2.*ndim)
        norm *= tau / weight
        norm += 1.
        p -= tau * g
        p /= norm
        E /= float(image.size)
        if i == 0:
            E_init = E
            E_previous = E
        else:
            if np.abs(E_previous - E) < eps * E_init:
                break
            else:
                E_previous = E
        i += 1
    return out
Alessandro Mirone's avatar
written    
Alessandro Mirone committed
192
    
193
194
195
196
197
198
199
200
201
202
203
204
205
def superr( scalDD, scalDS, scalSS, niter=15, beta=1.0e-8):
    """ 
    -    scalDS  which is  an array  [ZDIM,YDIM,XDIM]  , type "d" .
    -    scalDD  which is the total sum of the squared datas.
    -    scalSS  which is an array [XDIM,XDIM]  , type "d" .
    """
    ZDIM,YDIM,XDIM = scalDS.shape
    Volume = np.zeros( [ZDIM,YDIM,XDIM]  ,"f" )
    assert( scalSS.shape == (XDIM,XDIM))


    scalSS = scalSS.astype("f")
    scalDS = scalDS.astype("f")
206
207


Alessandro Mirone's avatar
Alessandro Mirone committed
208
    print( "  SHAPES ", scalDS.shape, scalSS.shape )
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
    
    Fista (  scalDD, scalDS, scalSS, Volume,niter=niter, beta=beta)
    
    return Volume

def calculate_grad( scalDD, scalDS , scalSS,   solution, grad) :
    grad [:]  = np.tensordot(  solution, scalSS, axes=[[-1],[-1]])
    err  = (grad*solution).sum()
    if scalDS is not None:
        err -= (scalDS*solution).sum()*2
        err +=  scalDD
        grad [:] -= scalDS
    return err/2
    
def    Fista( scalDD, scalDS , scalSS,  solution      , niter=500, beta=0.1 ):


    grad   = np.zeros_like(solution)
    grad2  = np.zeros_like(solution)
    x_old  = np.zeros_like(solution)
    y      = np.zeros_like(solution)

    err = 0.0
    err=calculate_grad( scalDD, scalDS , scalSS,   solution, grad) 
    for i in range(20):
        
        calculate_grad(None, None , scalSS,   grad, grad2)
        Lip = math.sqrt( np.linalg.norm(grad2/100000.0)   )*100000
        grad[:]  = grad2/ Lip
        if myrank==0:
Alessandro Mirone's avatar
Alessandro Mirone committed
240
            print( "LIP ", Lip)
241
242
243
244
245
246
247
248
249
    Lip = Lip*1.2

    t=1.0
    y[:] = solution
    x_old[:] = solution
    for iter in range(abs(niter)):
        err = calculate_grad(scalDD, scalDS , scalSS,   y, grad) 

        solution[:] =  y - grad/Lip
250
251
        
        # solution[:]=skimage.restoration.denoise_tv_chambolle(solution, weight=beta, eps=0.000002) 
252
        solution[:]=_denoise_tv_chambolle_nd(solution, weight=beta, eps=0.000002, positivity=True, cdim = [0,0,1])
253
254
255
256
257
258
259
260
261
262
263
264
265


        ## solution[:] = np.maximum(solution, 0)

        tnew = ( 1+math.sqrt(1.0+4*t*t) )/2


        y[:] = solution +(t-1)/tnew *( solution - x_old )
        t = tnew
        if niter<0:
            t=1
        x_old[:] = solution
        if myrank==0:
Alessandro Mirone's avatar
Alessandro Mirone committed
266
267
            print( " errore est %e  mod_grad est  %e\n" % (  err, grad.std()) )

268
269
270



271

272

273
274
275



276