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

fixed output_list | now it has only 5 elements instead of 5*batchsize #2914

Merged
merged 4 commits into from
Apr 14, 2023

Conversation

moienr
Copy link
Contributor

@moienr moienr commented Apr 12, 2023

Fixes #2913

Description:

This pull request updates the code in ignite/metrics/ssim.py so it won't create extra torch.Tensors.
The changes involve modifying the list comprehension that creates the output_list, which has the wrong length of BatchSize*5 instead of it being 5 (the same as len([y_pred, y, y_pred * y_pred, y * y, y_pred * y])). The elements with index>4 are empty tensors with shape (0,C,H1,W1) in the output_list

Instead of concatenating the tensor inputs in torch.cat([y_pred, y, y_pred * y_pred, y * y, y_pred * y]), I have created a list of the input tensors as input_list= [y_pred, y, y_pred * y_pred, y * y, y_pred * y], which I believe was the actual intention of the author.
Now we have access to the Length of this list, which we will use to retrieve input_list elements after passing them through a conv2d layer.
I then passed the concatenated input tensor using torch.cat(input_list) which has a size of (B*5, C, H1, W1) to the F.conv2d function call.

After that, the outputs which is a tensor with shape (B*5, C, H, W) is sliced for every Batch element to return a list of 5 elements. this is where the previous code had a problem and instead of a list of 5 (or len(input_list)) tensors, it was returning a list of B*5 elements, where only its first five elements were actual tensors and the rest where empty when the Batch_size>1.
(the previous code was calling tesnor[index1:index2] where index1 and index2 were larger than tensor.size(0) which was returning empty tensors)

We can see in lines 167-173 that the elements with indices 0-4 are being used, which proves this issue.

I have tested these changes on my local machine and confirmed that they do not introduce any new issues. Please let me know if there are any concerns or feedback regarding these changes.

Thanks for considering this pull request!

Check list:

  • New tests are added (if a new feature is added)
  • New doc strings: description and/or example code are in RST format
  • Documentation is updated (if required)

ignite/metrics/ssim.py Outdated Show resolved Hide resolved
@vfdev-5
Copy link
Collaborator

vfdev-5 commented Apr 14, 2023

@moienr thanks for the explanation and the PR! Right now I see the problem, it should be basically :

output_list = [outputs[x * batch_size : (x + 1) * batch_size] for x in range(5)]

where 5 refers to the size of input_list.

I made a suggestion how to fix code style and introduced batch size variable. Please update your PR and make sure to run code style auto formatting to avoid formatting issues:

bash tests/run_code_style.sh

moienr and others added 3 commits April 14, 2023 15:53
@moienr
Copy link
Contributor Author

moienr commented Apr 14, 2023

Thank you dear @vfdev-5 I committed your code, and ran the style.
Also, the branch was behind the new updates, so I updated the branch as well.
Thanks again for all your time.

@vfdev-5 vfdev-5 enabled auto-merge (squash) April 14, 2023 23:17
@vfdev-5 vfdev-5 merged commit fa11c9a into pytorch:master Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: metrics Metrics module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minor Bug | metrics.ssim, produces empty tensors in update function
2 participants