Giter Site home page Giter Site logo

Comments (6)

kylechampley avatar kylechampley commented on August 10, 2024 2

Glad it worked for you! I'm going to close this issue, but feel free to reopen it or open a new one if something else comes up.

from leap.

kylechampley avatar kylechampley commented on August 10, 2024

Yes, batch mode is implemented in leaptorch.py. It is either executed as a part of a forward and backward pass or you can explicitly call it. Look at test_recon_NN.py. On lines 235 and 279 we have:
proj = Projector(forward_project=True, use_static=True, use_gpu=use_cuda, gpu_device=device)
and
g = proj(f)

The proj(f) does a batch forward projection, but if we would have done:
proj = Projector(forward_project=False, use_static=True, use_gpu=use_cuda, gpu_device=device)
then
f = proj(g)
would do batch backprojection (adjoint of forward projection)

from leap.

hongtao-argmin avatar hongtao-argmin commented on August 10, 2024

Hi Kyle,

Thank you so much. It looks we should set the "batch_size" to the desired value first, then we can use batch mode, which means it does not recognize the batch mode automatically? Second, is there a way to copy or clone the "Projector()" object? Otherwise, it seems we must set forward_project=False to call the backprojection, which seems inconvenient?

from leap.

hongtao-argmin avatar hongtao-argmin commented on August 10, 2024

Moreover, I used the following code to check the adjoint:
**
proj_im = proj(f)
yy = torch.randn_like(proj_im)
proj.forward_project=False
torch.sum(yy * proj_im)
torch.sum(proj(yy) * f)
**
but the values of torch.sum(yy * proj_im) and torch.sum(proj(yy) * f) are significantly different. Looks I used something is wrong. Could you help me to clarify this? Thanks.

from leap.

kylechampley avatar kylechampley commented on August 10, 2024

I'm not sure what you are trying to do, so maybe using leaptorch is just confusing. Below is some code that I think does what you need it to do.

import sys
import os
import time
import numpy as np
from leapctype import *
leapct = tomographicModels()

def project_batch(g_batch,f_batch):
    for n in range(g_batch.shape[0]):
        leapct.project(g_batch[n,:,:,:],f_batch[n,:,:,:])
    return g_batch
        
def backproject_batch(g_batch,f_batch):
    for n in range(g_batch.shape[0]):
        leapct.backproject(g_batch[n,:,:,:],f_batch[n,:,:,:])
    return f_batch

batch_size = 4

numCols = 512
numAngles = 2*2*int(360*numCols/1024)
pixelSize = 0.65*512/numCols
numRows = numCols//2

leapct.set_parallelbeam(numAngles=numAngles, numRows=numRows, numCols=numCols, pixelHeight=pixelSize*11.0/14.0, pixelWidth=pixelSize*11.0/14.0, centerRow=0.5*(numRows-1), centerCol=0.5*(numCols-1), phis=leapct.setAngleArray(numAngles, 360.0))
leapct.set_default_volume()

vol_dim1, vol_dim2, vol_dim3 = leapct.get_volume_dim()
proj_dim1, proj_dim2, proj_dim3 = leapct.get_projection_dim()

#device = torch.device("cuda:0")

vol_data = torch.from_numpy(np.zeros((batch_size, vol_dim1, vol_dim2, vol_dim3), dtype=np.float32))
proj_data = torch.from_numpy(np.zeros((batch_size, proj_dim1, proj_dim2, proj_dim3), dtype=np.float32))
vol_data = vol_data.float().to(torch.device("cuda:0"))
proj_data = proj_data.float().to(torch.device("cuda:0"))

f = leapct.allocate_volume()
g = leapct.allocate_projections()
leapct.set_FORBILD(f,True)
f = torch.from_numpy(f)
f = f.float().to(torch.device("cuda:0"))
vol_data[:,:,:,:] = f[None,:,:,:]


Bf = vol_data.clone()

project_batch(proj_data, vol_data)
yy = torch.randn_like(proj_data)
print(torch.sum(yy * proj_data))
print(torch.sum(backproject_batch(yy,Bf) * vol_data))

from leap.

hongtao-argmin avatar hongtao-argmin commented on August 10, 2024

Thank you so much. This works for me.

Actually, I want to train a NN solver but assume the forward model can be changed.

BTW, I personally think the batch mode is more efficient than the following implementation? (maybe I am wrong)

def project_batch(g_batch,f_batch):
for n in range(g_batch.shape[0]):
leapct.project(g_batch[n,:,:,:],f_batch[n,:,:,:])
return g_batch

because this projection used loop but the batch mode in PyTorch automatically calls the parallel computing?

from leap.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.