You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
def convert_tensors_to_scalars(data: Any) -> Any:
"""Recursively walk through a collection and convert single-item tensors to scalar values.
Raises:
ValueError:
If tensors inside ``metrics`` contains multiple elements, hence preventing conversion to a scalar.
"""
def to_item(value: Tensor) -> Union[int, float, bool]:
if value.numel() != 1:
raise ValueError(
f"The metric `{value}` does not contain a single element, thus it cannot be converted to a scalar."
)
return value.item()
return apply_to_collection(data, Tensor, to_item)
PS: I am using Anomalib library which is based on pytorch lightning.
How do I resolve this or is there another way to get this? Any help would be greatly appreciated.
As the error suggests, if logging a metric it must be a singe value. You could try logging the individual elements of the confusion matrix eg. the true positives, false positives, true negatives, and false negatives
bcm=BinaryConfusionMatrix(normalize='all')
matrix=bcm(torch.randint(0, 2, (1, 50)), torch.randint(0, 2, (1, 50))) # just making some random predictions and labels to compute BCMlogger.experiment.add_image("bcm", matrix[None], global_step=self.global_step)
or you could use other functions of your logger directly to save the tensor
Bug description
Hi team,
During testing, I would like to extract Binaryconfusion matrix as a metric from the trainer.
I can see the value being calculated successfully but it is failing in trainer where the number of elements in result is greater than 1.
pytorch-lightning/src/lightning/fabric/utilities/apply_func.py
Line 123 in d194976
PS: I am using Anomalib library which is based on pytorch lightning.
How do I resolve this or is there another way to get this? Any help would be greatly appreciated.
Thanks
What version are you seeing the problem on?
v2.2
How to reproduce the bug
Error messages and logs
Environment
Current environment
More info
No response
The text was updated successfully, but these errors were encountered: