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

I got RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn using CTLIB library. #12

Open
2sjkim opened this issue Feb 6, 2023 · 1 comment

Comments

@2sjkim
Copy link

2sjkim commented Feb 6, 2023

Hello Teacher:
I really appreciate your great CT geometry library.

I am now implementing "deep image prior" using ctlib library, but I had some errors.
(exactly, I am doing sparse-view CT reconstruction)

For simplicity, I attached my code (Training network parts)
...
input_train = data['input'].to(device) # gaussian
target_train = data['target'].to(device) # sparse-view image

output = net(input_train) # simple UNet

out_for_loss = ctlib.projection(output, option_sparse) # forward projection with sparse view (num of projection views)
out_for_loss = ctlib.fbp(output, option_sparse)

optim.zero_grad()
loss = fn_loss(out_for_loss , target_train )
loss.backward()
optim.step()

train_total_loss += [loss.item()]
....

What I want to do is, the output of network should be reconstructed into sparse-view CT images by applying forward projection and back-projection with option_sparse sequentially and then calculate the MSE loss with target image.

But I encountered a problem with
"RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn"
thus, loss.backward() is not calculated.

For other solutions, people said that write " loss = Variable(loss, requires_grad=True)".

I also did that and aforementioned RuntimeError disappeared, but the loss was not updated (constant for every epoch).

I think the error was caused by directly calculating loss after using 'ctlib.projection' and 'ctlib.fbp' code.

How can I solve this problem?

@xiawj-hub
Copy link
Owner

xiawj-hub commented Feb 6, 2023 via email

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

2 participants