Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjoint in odl.tomo.RayTransform is incorrect #1646

Open
hongtao-argmin opened this issue Jun 10, 2024 · 10 comments
Open

adjoint in odl.tomo.RayTransform is incorrect #1646

hongtao-argmin opened this issue Jun 10, 2024 · 10 comments

Comments

@hongtao-argmin
Copy link

The forward and adjoint operators are incorrect by using the following commands:

ray_trafo = odl.tomo.RayTransform(reco_space, geometry)
proj_data = ray_trafo(phantom)
backproj = ray_trafo.adjoint(proj_data)

check:
y = torch.randn_like(proj_data)
torch.sum(y* proj_data) \neq torch.sum( ray_trafo.adjoint(y)* phantom)

for different number of views.

@mehrhardt
Copy link
Contributor

ODL's adjoint is not with respect to the inner product given by "torch.sum(y*x)" but rather y.inner(x).

@hongtao-argmin
Copy link
Author

hongtao-argmin commented Jul 16, 2024

Many thanks. For my understanding, the results of inner product should give us a scalar? So y.inner(x)'' will not give us a scalar so that looks weird if we define the inner product like this? If we use y=y.flatten()'' and ``y.inner(x.flatten)'', then it is equivalent to torch.sum(y*x).

Definition of inner product: https://en.wikipedia.org/wiki/Inner_product_space.

@mehrhardt
Copy link
Contributor

Sorry, I can't follow your argument regarding scale.

I don't know from the top of my head what y=y.flatten() gives you. Will this be a numpy array? Is it even defined?

In any case, if you want to have that ODL computes the adjoint with respect to the inner product of torch, you could just compute constant scaling factors alpha and beta and correct for it:

ray_trafo = odl.tomo.RayTransform(reco_space, geometry)
proj_data = ray_trafo(phantom)
backproj = ray_trafo.adjoint(proj_data)

alpha = backproj.inner(phantom) / torch.sum(backproj.to_array() * phantom.to_array())
beta = proj_data.inner(backproj) / torch.sum(proj_data.to_array() * backproj.to_array())

Op = beta * ray_trafo
Adj = alpha * ray_trafo.adjoint

Note, that I haven't tested the code above but hopefully you get the idea.

@hongtao-argmin
Copy link
Author

hongtao-argmin commented Jul 17, 2024

Thanks. y=y.flatten()'' (defined in PyTorch) will let y'' become a vector since images are represented in multi-dimension.

For my understanding, for any forward model A'', we should have <A^Tx,y> = <y,Ax>'' that A^T is the adjoint of A and the inner product <>'' will give us a scalar. However, x.inner(y) will not give us a scalar since images are represented in multi-dimension. that alpha'' or ``beta'' showed in the template code is not a scalar.

@mehrhardt
Copy link
Contributor

with "scale", you mean scalar https://en.wikipedia.org/wiki/Scalar_(mathematics)?

@hongtao-argmin
Copy link
Author

hongtao-argmin commented Jul 17, 2024

yeah, sorry about the typo error. I corrected the word.

@JevgenijaAksjonova
Copy link
Contributor

JevgenijaAksjonova commented Jul 18, 2024

Hi,

The definition of the inner product in L2 function space is <f, g> = \int f(x) g(x) dx.
Discretizing the above yields \sum f_i g_i * cell_volume.
Hence, for the ray transform it is <Ax, y > * A.range.cell_volume = <x, A^T y> * A.domain.cell_volume, where <a,b> = sum a_i * b_i

@hongtao-argmin
Copy link
Author

Thank you so much.

Suppose we worked on the 2D case and the image size is [m, n] and the size of the measurements is [nviews,detector_size]. Then, we should have <Ax,y>(nviewsdetector_size)= <x,A^T y>(mn). -- x represents the image and y denotes the measurements.

If my understanding is correct, then looks the ``='' is still not held. Or is it possible for you to show me one example that <Ax, y > * A.range.cell_volume = <x, A^T y> * A.domain.cell_volume is held for a given geometry but different image size, nviews, detector_size? I think this would help me a lot. Thank you so much.

@JevgenijaAksjonova
Copy link
Contributor

If A = odl.tomo.RayTransform(reco_space, geometry), then use A.range.cell_volume and A.domain.cell_volume. Here, domain and range are input and output spaces of your operator. When you define the ray transform, you provide the domain (reco_space), range is computed automatically. space.cell_volume = product of space.cell_sides. Space.cell_sides = ( space.max_pt - space.min_pt) / space.shape

@hongtao-argmin
Copy link
Author

It works now. Thank you so much. This really helps. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants