Ejemplo n.º 1
0
def test_adj():
    N = 512
    Nproj = int(3 * N / 2)
    Nslices = 1
    filter_type = 'None'
    cor = N / 2
    interp_type = 'cubic'
    gpu = 0

    f = np.float32(np.random.random([Nslices, N, N]))
    R = np.float32(np.random.random([Nslices, Nproj, N]))

    lp = lpTransform.lpTransform(N, Nproj, Nslices, filter_type, cor,
                                 interp_type)
    lp.precompute(1)
    lp.initcmem(1, gpu)

    Rf = lp.fwd(f, gpu)
    frec = lp.adj(R, gpu)
    Rrec = lp.fwd(frec, gpu)

    # scale test
    RR = lp.fwd(lp.adj(R, gpu), gpu)
    scale = np.sum(np.float64(R * RR)) / np.sum(np.float64(RR * RR))

    # dot product test
    sum1 = sum(np.float64(np.ndarray.flatten(Rrec) * np.ndarray.flatten(R)))
    sum2 = sum(np.float64(np.ndarray.flatten(frec) * np.ndarray.flatten(frec)))
    err = np.linalg.norm(sum1 - sum2) / np.linalg.norm(sum2)
    print([scale, err])
    return [scale, err]
Ejemplo n.º 2
0
def test_gpus_many_map():
    N = 2048
    Nproj = N
    Ns = 2048
    filter_type = 'None'
    cor = N//2
    interp_type = 'cubic'

    # init random array
    R = np.float32(np.sin(np.arange(0, Ns*Nproj*N)/float(Ns*Nproj)))
    R = np.reshape(R, [Ns, Nproj, N])
    # input parameters
    tomo = R
    reg_par = 0.001  # *np.max(tomo)
    num_iter = 100
    recon = np.zeros([Ns, N, N], dtype="float32")+1e-3
    method = "em"
    gpu_list = [0, 1, 2, 3]
    # list of available methods for reconstruction
    lpmethods_list = {
        'fbp': lpmethods.fbp,
        'grad': lpmethods.grad,
        'cg': lpmethods.cg,
        'tv': lpmethods.tv,
        'em': lpmethods.em
    }

    ngpus = len(gpu_list)
    # number of slices for simultaneous processing by 1 gpu
    # (depends on gpu memory size, chosen for gpus with >= 8GB memory)
    Nssimgpu = min(int(pow(2, 26)/float(N*N)), int(np.ceil(Ns/float(ngpus))))

    tic()
    # class lprec
    lp = lpTransform.lpTransform(
        N, Nproj, Nssimgpu, filter_type, cor, interp_type)
    # if not fbp, precompute for the forward transform
    lp.precompute(method != 'fbp')
    print("Init time %f" % (toc()))
    # list of slices sets for simultaneous processing b gpus
    ids_list = [None]*int(np.ceil(Ns/float(Nssimgpu)))
    for k in range(0, len(ids_list)):
        ids_list[k] = range(k*Nssimgpu, min(Ns, (k+1)*Nssimgpu))

    tic()
    # init memory for each gpu
    for igpu in range(0, ngpus):
        gpu = gpu_list[igpu]
        # if not fbp, allocate memory for the forward transform arrays
        lp.initcmem(method != 'fbp', gpu)

    # run reconstruciton on many gpus
    with cf.ThreadPoolExecutor(ngpus) as e:
        shift = 0
        for reconi in e.map(partial(lpmultigpu, lp, lpmethods_list[method], recon, tomo, num_iter, reg_par, gpu_list), ids_list):
            recon[np.arange(0, reconi.shape[0])+shift] = reconi
            shift += reconi.shape[0]
    print("Rec time %f" % (toc()))
Ejemplo n.º 3
0
def test_adj_ptr():
    N = 512
    Nproj = np.int(3 * N / 2)
    Nslices = 1
    filter_type = 'None'
    cor = N / 2
    interp_type = 'cubic'
    gpu = 0

    # init random arrays
    f = np.float32(np.random.random([Nslices, N, N]))
    R = np.float32(np.random.random([Nslices, Nproj, N]))

    # copy arrays to gpu
    fg = cp.array(f)
    Rg = cp.array(R)
    # allocate gpu memory for results
    Rfg = cp.zeros([Nslices, Nproj, N], dtype="float32")
    fRg = cp.zeros([Nslices, N, N], dtype="float32")

    # class lprec
    lp = lpTransform.lpTransform(N, Nproj, Nslices, filter_type, cor,
                                 interp_type)
    lp.precompute(1)
    lp.initcmem(1, gpu)

    # compute with gpu pointers
    lp.fwdp(Rfg, fg, gpu)
    lp.adjp(fRg, Rg, gpu)

    # self adjoint test
    sum1 = sum(
        np.float64(
            np.ndarray.flatten(Rfg.get()) * np.ndarray.flatten(Rg.get())))
    sum2 = sum(
        np.float64(
            np.ndarray.flatten(fRg.get()) * np.ndarray.flatten(fg.get())))
    err0 = np.linalg.norm(sum1 - sum2) / np.linalg.norm(sum2)
    print(err0)

    return err0
Ejemplo n.º 4
0
def lprec(tomo, center, recon, theta, **kwargs):
    """
    Reconstruct object using the Log-polar based method
    https://github.com/math-vrn/lprec

    Extra options
    ----------
    lpmethod : str
        LP reconsruction method to use
            - 'fbp'
            - 'grad'
            - 'cg'
            - 'tv'
            - 'em'
    filter_type:
        Filter for backprojection
            - 'ramp'
            - 'shepp-logan'
            - 'cosine'
            - 'cosine2'
            - 'hamming'
            - 'hann'
            - 'parzen'
    interp_type:
        Type of interpolation between Cartesian, polar and log-polar coordinates
            - 'linear'
            - 'cubic'
    Example
    -------
    >>> import tomopy
    >>> obj = tomopy.shepp3d() # Generate an object.
    >>> ang = tomopy.angles(180) # Generate uniformly spaced tilt angles.
    >>> sim = tomopy.project(obj, ang) # Calculate projections.
    >>>
    >>> # Reconstruct object:
    >>> rec = tomopy.recon(sim, ang, algorithm=tomopy.lprec,
    >>>       lpmethod='lpfbp', filter_name='parzen', interp_type='cubic', ncore=1)
    >>>
    >>> # Show 64th slice of the reconstructed object.
    >>> import pylab
    >>> pylab.imshow(rec[64], cmap='gray')
    >>> pylab.show()
    """
    from lprec import lpTransform

    # set default options
    opts = kwargs
    for o in default_options['lprec']:
        if o not in kwargs:
            opts[o] = default_options['lprec'][o]

    filter_name = opts['filter_name']
    interp_type = opts['interp_type']
    lpmethod = opts['lpmethod']
    num_iter = opts['num_iter']
    reg_par = opts['reg_par']

    # Init lp method
    # number of slices for simultanious processing by 1 gpu, chosen for 4GB gpus
    Nslices, Nproj, N = tomo.shape
    Nslices0 = min(int(pow(2, 23) / float(N * N)), Nslices)
    lphandle = lpTransform.lpTransform(N, Nproj, Nslices0, filter_name,
                                       int(center[0] + 0.5), interp_type)

    if (lpmethod == 'fbp'):
        # precompute only for the adj transform
        lphandle.precompute(0)
        lphandle.initcmem(0)
        for k in range(0, int(np.ceil(Nslices / float(Nslices0)))):
            ids = range(k * Nslices0, min(Nslices, (k + 1) * Nslices0))
            recon[ids] = lphandle.adj(tomo[ids])
    else:
        # iterative schemes
        # precompute for both fwd and adj transforms
        lphandle.precompute(1)
        lphandle.initcmem(1)

        from lprec import iterative
        lpitermethods = {
            'grad': iterative.grad,
            'cg': iterative.cg,
            'tv': iterative.tv,
            'em': iterative.em
        }
        # run
        for k in range(0, int(np.ceil(Nslices / float(Nslices0)))):
            ids = range(k * Nslices0, min(Nslices, (k + 1) * Nslices0))
            recon[ids] = lpitermethods[lpmethod](lphandle, recon[ids],
                                                 tomo[ids], num_iter, reg_par)
Ejemplo n.º 5
0
def lprec(tomo, center, recon, theta, **kwargs):
    """
    Reconstruct object using the Log-polar based method
    https://github.com/math-vrn/lprec

    Extra options
    ----------
    lpmethod : str
        LP reconsruction method to use
            - 'fbp'
            - 'grad'
            - 'cg'
            - 'tv'
            - 'em'
    filter_type:
        Filter for backprojection
            - 'ramp'
            - 'shepp-logan'
            - 'cosine'
            - 'cosine2'
            - 'hamming'
            - 'hann'
            - 'parzen'
    interp_type:
        Type of interpolation between Cartesian, polar and log-polar coordinates
            - 'linear'
            - 'cubic'
    Example
    -------
    >>> import tomopy
    >>> obj = tomopy.shepp3d() # Generate an object.
    >>> ang = tomopy.angles(180) # Generate uniformly spaced tilt angles.
    >>> sim = tomopy.project(obj, ang) # Calculate projections.
    >>>
    >>> # Reconstruct object:
    >>> rec = tomopy.recon(sim, ang, algorithm=tomopy.lprec,
    >>>       lpmethod='lpfbp', filter_name='parzen', interp_type='cubic', ncore=1)
    >>>
    >>> # Show 64th slice of the reconstructed object.
    >>> import pylab
    >>> pylab.imshow(rec[64], cmap='gray')
    >>> pylab.show()
    """
    from lprec import lpTransform
    from lprec import lpmethods
    import concurrent.futures as cf
    from functools import partial

    # set default options
    opts = kwargs
    for o in default_options['lprec']:
        if o not in kwargs:
            opts[o] = default_options['lprec'][o]

    filter_name = opts['filter_name']
    interp_type = opts['interp_type']
    lpmethod = opts['lpmethod']
    num_iter = opts['num_iter']
    reg_par = opts['reg_par']
    gpu_list = opts['gpu_list']

    # list of available methods for reconstruction
    lpmethods_list = {
        'fbp': lpmethods.fbp,
        'grad': lpmethods.grad,
        'cg': lpmethods.cg,
        'tv': lpmethods.tv,
        'em': lpmethods.em
    }

    [Ns, Nproj, N] = tomo.shape
    ngpus = len(gpu_list)

    # number of slices for simultaneous processing by 1 gpu
    # (depends on gpu memory size, chosen for gpus with >= 4GB memory)
    Nssimgpu = min(int(pow(2, 24) / float(N * N)),
                   int(np.ceil(Ns / float(ngpus))))

    # class lprec
    lp = lpTransform.lpTransform(N, Nproj, Nssimgpu, filter_name,
                                 int(center[0]), interp_type)
    # if not fbp, precompute for the forward transform
    lp.precompute(lpmethod != 'fbp')

    # list of slices sets for simultaneous processing b gpus
    ids_list = [None] * int(np.ceil(Ns / float(Nssimgpu)))
    for k in range(0, len(ids_list)):
        ids_list[k] = range(k * Nssimgpu, min(Ns, (k + 1) * Nssimgpu))

    # init memory for each gpu
    for igpu in range(0, ngpus):
        gpu = gpu_list[igpu]
        # if not fbp, allocate memory for the forward transform arrays
        lp.initcmem(lpmethod != 'fbp', gpu)

    # run reconstruciton on many gpus
    with cf.ThreadPoolExecutor(ngpus) as e:
        shift = 0
        for reconi in e.map(
                partial(lpmultigpu, lp, lpmethods_list[lpmethod], recon, tomo,
                        num_iter, reg_par, gpu_list), ids_list):
            recon[np.arange(0, reconi.shape[0]) + shift] = reconi
            shift += reconi.shape[0]

    return recon
Ejemplo n.º 6
0
def lprec(tomo, center, recon, theta, **kwargs):
    """
    Reconstruct object using the Log-polar based method
    https://github.com/math-vrn/lprec

    Extra options
    ----------
    lpmethod : str
        LP reconsruction method to use
            - 'fbp'
            - 'grad'
            - 'cg'
            - 'tv'
            - 'em'
            - 'tve'
            - 'tvl1'
    filter_type:
        Filter for backprojection
            - 'ramp'
            - 'shepp-logan'
            - 'cosine'
            - 'cosine2'
            - 'hamming'
            - 'hann'
            - 'parzen'
    interp_type:
        Type of interpolation between Cartesian, polar and log-polar coordinates
            - 'linear'
            - 'cubic'
    Example
    -------
    >>> import tomopy
    >>> obj = tomopy.shepp3d() # Generate an object.
    >>> ang = tomopy.angles(180) # Generate uniformly spaced tilt angles.
    >>> sim = tomopy.project(obj, ang) # Calculate projections.
    >>>
    >>> # Reconstruct object:
    >>> rec = tomopy.recon(sim, ang, algorithm=tomopy.lprec,
    >>>       lpmethod='lpfbp', filter_name='parzen', interp_type='cubic', ncore=1)
    >>>
    >>> # Show 64th slice of the reconstructed object.
    >>> import pylab
    >>> pylab.imshow(rec[64], cmap='gray')
    >>> pylab.show()
    """
    from lprec import lpTransform
    from lprec import lpmethods
    import concurrent.futures as cf
    from functools import partial

    # set default options
    opts = kwargs
    for o in default_options['lprec']:
        if o not in kwargs:
            opts[o] = default_options['lprec'][o]

    filter_name = opts['filter_name']
    interp_type = opts['interp_type']
    lpmethod = opts['lpmethod']
    num_iter = opts['num_iter']
    reg_par = opts['reg_par']
    gpu_list = opts['gpu_list']

    # list of available methods for reconstruction
    lpmethods_list = {
        'fbp': lpmethods.fbp,
        'grad': lpmethods.grad,
        'cg': lpmethods.cg,
        'tv': lpmethods.tv,
        'em': lpmethods.em,
        'tve': lpmethods.tve,
        'tvl1': lpmethods.tvl1,
    }

    [Ns, Nproj, N] = tomo.shape
    ngpus = len(gpu_list)

    # number of slices for simultaneous processing by 1 gpu
    # (depends on gpu memory size, chosen for gpus with >= 4GB memory)
    Nssimgpu = min(int(pow(2, 24)/float(N*N)), int(np.ceil(Ns/float(ngpus))))

    # class lprec
    lp = lpTransform.lpTransform(
        N, Nproj, Nssimgpu, filter_name, int(center[0]), interp_type)
    # if not fbp, precompute for the forward transform
    lp.precompute(lpmethod != 'fbp')

    # list of slices sets for simultaneous processing b gpus
    ids_list = [None]*int(np.ceil(Ns/float(Nssimgpu)))
    for k in range(0, len(ids_list)):
        ids_list[k] = range(k*Nssimgpu, min(Ns, (k+1)*Nssimgpu))

    # init memory for each gpu
    for igpu in range(0, ngpus):
        gpu = gpu_list[igpu]
        # if not fbp, allocate memory for the forward transform arrays
        lp.initcmem(lpmethod != 'fbp', gpu)

    lock = threading.Lock()
    global BUSYGPUS
    BUSYGPUS = np.zeros(ngpus)
    # run reconstruciton on many gpus
    with cf.ThreadPoolExecutor(ngpus) as e:
        shift = 0
        for reconi in e.map(partial(lpmultigpu, lp, lpmethods_list[lpmethod], recon, tomo, num_iter, reg_par, gpu_list, lock), ids_list):
            recon[np.arange(0, reconi.shape[0])+shift] = reconi
            shift += reconi.shape[0]

    return recon
Ejemplo n.º 7
0
def test_adj_many():
    N = 512
    Nproj = int(3 * N / 4)
    Nslices = 8
    filter_type = 'None'
    cor = N / 2
    interp_type = 'cubic'
    gpu = 0

    fid = open('tests/data/f', 'rb')
    f = np.float32(
        np.reshape(struct.unpack(N * N * 'f', fid.read(N * N * 4)), [1, N, N]))
    fa = np.zeros([Nslices, N, N], dtype=np.float32)
    for k in range(0, Nslices):
        fa[k, :, :] = f * (Nslices - k)

    fid = open('tests/data/R', 'rb')
    R = np.float32(
        np.reshape(struct.unpack(Nproj * N * 'f', fid.read(Nproj * N * 4)),
                   [1, Nproj, N]))
    Ra = np.zeros([Nslices, Nproj, N], dtype=np.float32)
    for k in range(0, Nslices):
        Ra[k, :, :] = R * (Nslices - k)

    # copy arrays to gpu
    fg = cp.array(fa)
    Rg = cp.array(Ra)

    # allocate gpu memory for results
    Rfg = cp.zeros([Nslices, Nproj, N], dtype="float32")
    fRg = cp.zeros([Nslices, N, N], dtype="float32")
    RfRg = cp.zeros([Nslices, Nproj, N], dtype="float32")

    # class lprec
    lp = lpTransform.lpTransform(N, Nproj, Nslices, filter_type, cor,
                                 interp_type)
    lp.precompute(1)
    lp.initcmem(1, gpu)

    # compute with gpu pointers
    lp.fwdp(Rfg, fg, gpu)
    lp.adjp(fRg, Rg, gpu)
    lp.fwdp(RfRg, fRg, gpu)

    # self adjoint test
    sum1 = sum(
        np.float64(
            np.ndarray.flatten(Rfg.get()) * np.ndarray.flatten(Rg.get())))
    sum2 = sum(
        np.float64(
            np.ndarray.flatten(fRg.get()) * np.ndarray.flatten(fg.get())))
    err0 = np.linalg.norm(sum1 - sum2) / np.linalg.norm(sum2)

    Rf = np.float64(Rfg.get())
    fR = np.float64(fRg.get())
    RfR = np.float64(RfRg.get())

    err1 = np.linalg.norm(fR - fa) / np.linalg.norm(fa)
    err2 = np.linalg.norm(RfR - Rf) / np.linalg.norm(Rf)

    print([err0, err1, err2])
    return [err0, err1, err2]
Ejemplo n.º 8
0
def test_gpus_many_map():
    N = 512
    Nproj = np.int(3*N/2)
    Ns = 32
    filter_type = 'None'
    cor = int(N/2)-3
    interp_type = 'cubic'

    # init random array
    R = np.float32(np.sin(np.arange(0, Ns*Nproj*N)/float(Ns*Nproj)))
    R = np.reshape(R, [Ns, Nproj, N])
    # input parameters
    tomo = R
    reg_par = -1  # *np.max(tomo)
    num_iter = 100
    recon = np.zeros([Ns, N, N], dtype="float32")+1e-3
    method = "grad"
    gpu_list = [0,1,2,3]

    # list of available methods for reconstruction
    lpmethods_list = {
        'fbp': lpmethods.fbp,
        'grad': lpmethods.grad,
        'cg': lpmethods.cg,
        'tv': lpmethods.tv,
        'em': lpmethods.em
    }
    try:
        cp.cuda.Device(1).use()
    except:
        gpu_list = [0]
    ngpus = len(gpu_list)
    global bgpus
    bgpus = np.zeros(ngpus)
    # number of slices for simultaneous processing by 1 gpu
    # (depends on gpu memory size, chosen for gpus with >= 4GB memory)
    Nssimgpu = min(int(pow(2, 24)/float(N*N)), int(np.ceil(Ns/float(ngpus))))

    # class lprec
    lp = lpTransform.lpTransform(
        N, Nproj, Nssimgpu, filter_type, cor, interp_type)
    # if not fbp, precompute for the forward transform
    lp.precompute(method != 'fbp')

    # list of slices sets for simultaneous processing b gpus
    ids_list = [None]*int(np.ceil(Ns/float(Nssimgpu)))
    for k in range(0, len(ids_list)):
        ids_list[k] = range(k*Nssimgpu, min(Ns, (k+1)*Nssimgpu))

    # init memory for each gpu
    for igpu in range(0, ngpus):
        gpu = gpu_list[igpu]
        # if not fbp, allocate memory for the forward transform arrays
        lp.initcmem(method != 'fbp', gpu)
    lock = threading.Lock()
    # run reconstruciton on many gpus
    with cf.ThreadPoolExecutor(ngpus) as e:
        shift = 0
        for reconi in e.map(partial(lpmultigpu, lp, lpmethods_list[method], recon, tomo, num_iter, reg_par, gpu_list, lock), ids_list):
            recon[np.arange(0, reconi.shape[0])+shift] = reconi
            shift += reconi.shape[0]

    norm = np.linalg.norm(np.float64(recon))
    print(norm)
    return norm
Ejemplo n.º 9
0
def test_foam():

    N = 2016
    Nproj = 299
    Ns = 1
    filter_type = 'None'
    cor = N / 2
    interp_type = 'cubic'
    gpu = 0

    num_iter = {
        'fbp': 0,
        'grad': 64,
        'cg': 16,
        'em': 64,
        'tv': 256,
        'tve': 256,
    }
    reg_par = {
        'fbp': 0,
        'grad': -1,
        'cg': 0,
        'em': 0.5,
        'tv': 0.001,
        'tve': 0.001,
    }

    lp = lpTransform.lpTransform(N, Nproj, Ns, filter_type, cor, interp_type)
    lp.precompute(1)
    lp.initcmem(1, gpu)

    fid = open('./tests/data/Rfoam', 'rb')
    tomo = np.float32(
        np.reshape(struct.unpack(Nproj * N * 'f', fid.read(Nproj * N * 4)),
                   [Ns, N, Nproj])).swapaxes(1, 2)

    # rec
    fRfbp = np.zeros([Ns, N, N], dtype="float32")
    fRfbp = lpmethods.fbp(lp, fRfbp, tomo, num_iter['fbp'], reg_par['fbp'],
                          gpu)
    fRgrad = np.zeros([Ns, N, N], dtype="float32")
    fRgrad = lpmethods.grad(lp, fRgrad, tomo, num_iter['grad'],
                            reg_par['grad'], gpu)
    fRcg = np.zeros([Ns, N, N], dtype="float32")
    fRcg = lpmethods.cg(lp, fRcg, tomo, num_iter['cg'], reg_par['cg'], gpu)
    fRem = np.zeros([Ns, N, N], dtype="float32") + 1e-3
    fRem = lpmethods.em(lp, fRem, tomo, num_iter['em'], reg_par['em'], gpu)
    fRtv = np.zeros([Ns, N, N], dtype="float32")
    fRtv = lpmethods.tv(lp, fRtv, tomo, num_iter['tv'], reg_par['tv'], gpu)
    fRtve = np.zeros([Ns, N, N], dtype="float32")
    fRtve = lpmethods.tve(lp, fRtve, tomo, num_iter['tve'], reg_par['tve'],
                          gpu)
    norm0 = np.linalg.norm(np.float64(fRfbp))
    norm1 = np.linalg.norm(np.float64(fRgrad))
    norm2 = np.linalg.norm(np.float64(fRcg))
    norm3 = np.linalg.norm(np.float64(fRem))
    norm4 = np.linalg.norm(np.float64(fRtv))
    norm5 = np.linalg.norm(np.float64(fRtve))
    # plt.subplot(2, 3, 1)
    # plt.imshow(fRfbp[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 2)
    # plt.imshow(fRgrad[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 3)
    # plt.imshow(fRcg[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 4)
    # plt.imshow(fRem[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 5)
    # plt.imshow(fRtv[-1, :, :])
    # plt.colorbar()
    # plt.show()
    print([norm0, norm1, norm2, norm3, norm4, norm5])

    return [norm0, norm1, norm2, norm3, norm4, norm5]
Ejemplo n.º 10
0
def test_gpus_many():
    N = 512
    Nproj = np.int(3 * N / 2)
    Ns = 32
    filter_type = 'None'
    cor = N / 2
    interp_type = 'cubic'

    # init random arrays
    R = np.float32(np.sin(np.arange(0, Ns * Nproj * N) / float(Ns * Nproj)))
    R = np.reshape(R, [Ns, Nproj, N])

    # input parameters
    tomo = R
    reg_par = 0.001  # *np.max(tomo)
    num_iter = 100
    recon = np.zeros([Ns, N, N], dtype="float32") + 1e-3
    method = "tv"
    gpu_list = [0, 1]
    try:
        cp.cuda.Device(1).use()
    except:
        gpu_list = [0]

    # list of available methods for reconstruction
    lpmethods_list = {
        'fbp': lpmethods.fbp,
        'grad': lpmethods.grad,
        'cg': lpmethods.cg,
        'tv': lpmethods.tv,
        'em': lpmethods.em
    }

    ngpus = len(gpu_list)
    # total number of slices for processing by 1 gpu
    Nspergpu = int(np.ceil(Ns / float(ngpus)))
    # number of slices for simultaneous processing by 1 gpu
    # (depends on gpu memory size, chosen for gpus with >= 4GB memory)
    Nssimgpu = min(int(pow(2, 24) / float(N * N)), Nspergpu)

    # class lprec
    lp = lpTransform.lpTransform(N, Nproj, Nssimgpu, filter_type, cor,
                                 interp_type)
    # if not fbp, precompute for the forward transform
    lp.precompute(method != 'fbp')

    # execute reconstruction on a thread per GPU
    jobs = [None] * ngpus
    with cf.ThreadPoolExecutor(ngpus) as e:
        for igpu in range(0, ngpus):
            gpu = gpu_list[igpu]
            # if not fbp, allocate memory for the forward transform arrays
            lp.initcmem(method != 'fbp', gpu)
            ids = range(igpu * Nspergpu, min(Ns, (igpu + 1) * Nspergpu))
            jobs[igpu] = e.submit(lpmultigpu, lp, lpmethods_list[method],
                                  recon[ids], tomo[ids], num_iter, reg_par,
                                  gpu, Nssimgpu)

    # collect results
    for igpu in range(0, ngpus):
        ids = range(igpu * Nspergpu, min(Ns, (igpu + 1) * Nspergpu))
        recon[ids] = jobs[igpu].result()

    norm = np.linalg.norm(np.float64(recon))
    print(norm)
    return norm
Ejemplo n.º 11
0
def test_lpmethods():
    N = 512
    Nproj = int(3*N/4)
    Ns = 8
    filter_type = 'None'
    cor = N/2
    interp_type = 'cubic'
    gpu = 0

    num_iter = {'fbp': 0,
                'grad': 64,
                'cg': 16,
                'em': 64,
                'tv': 256,
                }
    reg_par = {'fbp': 0,
               'grad': -1,
               'cg': 0,
               'em': 0.01,
               'tv': 0.001,
               }

    fid = open('tests/data/f', 'rb')
    f = np.float32(np.reshape(struct.unpack(
        N*N*'f', fid.read(N*N*4)), [1, N, N]))
    fa = np.zeros([Ns, N, N], dtype=np.float32)
    for k in range(0, Ns):
        fa[k, :, :] = f

    # class lprec
    lp = lpTransform.lpTransform(N, Nproj, Ns, filter_type, cor, interp_type)
    lp.precompute(1)
    lp.initcmem(1, gpu)

    # init data
    f*g = cp.array(fa)
    Rag = cp.zeros([Ns, Nproj, N], dtype="float32")
    lp.fwdp(Rag, f*g, gpu)
    Ra = Rag

    # rec
    fRfbp = np.zeros([Ns, N, N], dtype="float32")
    fRfbp = lpmethods.fbp(lp, fRfbp, Ra, num_iter['fbp'], reg_par['fbp'], gpu)
    fRgrad = np.zeros([Ns, N, N], dtype="float32")
    fRgrad = lpmethods.grad(
        lp, fRgrad, Ra, num_iter['grad'], reg_par['grad'], gpu)
    fRcg = np.zeros([Ns, N, N], dtype="float32")
    fRcg = lpmethods.cg(lp, fRcg, Ra, num_iter['cg'], reg_par['cg'], gpu)
    fRem = np.zeros([Ns, N, N], dtype="float32")+1e-3
    fRem = lpmethods.em(lp, fRem, Ra, num_iter['em'], reg_par['em'], gpu)
    fRtv = np.zeros([Ns, N, N], dtype="float32")
    fRtv = lpmethods.tv(lp, fRtv, Ra, num_iter['tv'], reg_par['tv'], gpu)

    norm0 = np.linalg.norm(np.float64(fRfbp))
    norm1 = np.linalg.norm(np.float64(fRgrad))
    norm2 = np.linalg.norm(np.float64(fRcg))
    norm3 = np.linalg.norm(np.float64(fRem))
    norm4 = np.linalg.norm(np.float64(fRtv))
    # plt.subplot(2, 3, 1)
    # plt.imshow(fRfbp[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 2)
    # plt.imshow(fRgrad[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 3)
    # plt.imshow(fRcg[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 4)
    # plt.imshow(fRem[-1, :, :])
    # plt.colorbar()
    # plt.subplot(2, 3, 5)
    # plt.imshow(fRtv[-1, :, :])
    # plt.colorbar()
    # plt.show()
    print([norm0, norm1, norm2, norm3, norm4])

    return [norm0, norm1, norm2, norm3, norm4]