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

MultiMasks Augmentation - Issue with torch.from_numpy() - Abumentation #1935

Open
FrsECM opened this issue Sep 18, 2024 · 3 comments
Open

MultiMasks Augmentation - Issue with torch.from_numpy() - Abumentation #1935

FrsECM opened this issue Sep 18, 2024 · 3 comments
Labels
bug Something isn't working Need more info

Comments

@FrsECM
Copy link

FrsECM commented Sep 18, 2024

Describe the bug

Hello !
I have a problem when i use HorizontalFlip combined with multiple masks and ToTensorV2 augmentation.

To Reproduce

You can use this piece of code to reproduce :

import albumentations as A
from albumentations.pytorch import ToTensorV2
import cv2
import numpy as np

transform = A.Compose([
        A.HorizontalFlip(p=1),
        A.ToFloat(max_value=255),
        ToTensorV2()
    ],is_check_shapes=False)


image = 255*np.ones((3096,2048,3),dtype=np.uint8)
masks = [np.ones((3096,2048),dtype=np.uint8) for i in range(2)]

transformed = transform(image=image,masks=masks)

Steps to reproduce the behavior:

  1. Python 3.10 - Albumentation 1.14.13
  2. Sample code that produces the bug
  3. Any error messages or incorrect outputs.

Expected behavior

I expect the augmentation to be applied without error

Actual behavior

I have an exception :

Traceback (most recent call last):
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/f296849/.vscode-server/extensions/ms-python.debugpy-2024.6.0/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/f296849/.vscode-server/extensions/ms-python.debugpy-2024.6.0/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/f296849/.vscode-server/extensions/ms-python.debugpy-2024.6.0/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/f296849/.vscode-server/extensions/ms-python.debugpy-2024.6.0/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/f296849/.vscode-server/extensions/ms-python.debugpy-2024.6.0/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/f296849/.vscode-server/extensions/ms-python.debugpy-2024.6.0/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "/home/f296849/code/2024/domf_iris2/notebooks/sandbox/debug_albumentation_1.4.13.py", line 27, in <module>
    transformed = transform(image=image,masks=masks)
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/site-packages/albumentations/core/composition.py", line 307, in __call__
    data = t(**data)
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/site-packages/albumentations/core/transforms_interface.py", line 123, in __call__
    return self.apply_with_params(params, **kwargs)
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/site-packages/albumentations/core/transforms_interface.py", line 141, in apply_with_params
    res[key] = target_function(arg, **params)
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/site-packages/albumentations/pytorch/transforms.py", line 52, in apply_to_masks
    return [self.apply_to_mask(mask, **params) for mask in masks]
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/site-packages/albumentations/pytorch/transforms.py", line 52, in <listcomp>
    return [self.apply_to_mask(mask, **params) for mask in masks]
  File "/home/default/miniconda/envs/domf_iris2/lib/python3.10/site-packages/albumentations/pytorch/transforms.py", line 49, in apply_to_mask
    return torch.from_numpy(mask)
ValueError: At least one stride in the given numpy array is negative, and tensors with negative strides are not currently supported. (You can probably work around this by making a copy of your array  with array.copy().) 

Screenshots

You can see bellow that it's working with v1.4.12
image

With v1.4.13, it is no longer working :
image

Additional context

I suspect this commit to be the cause of the problem :

Workarround :

import albumentations as A
from albumentations.pytorch import ToTensorV2
import numpy as np
import torch
from albumentations.core.types import NUM_MULTI_CHANNEL_DIMENSIONS

class ToTensor(ToTensorV2):
    def apply_to_mask(self, mask: np.ndarray, **params: A.Any) -> torch.Tensor:
        if self.transpose_mask and mask.ndim == NUM_MULTI_CHANNEL_DIMENSIONS:
            mask = mask.transpose(2, 0, 1)
        return torch.from_numpy(np.ascontiguousarray(mask))
@FrsECM FrsECM added the bug Something isn't working label Sep 18, 2024
@FrsECM FrsECM changed the title MultiMasks Augmentation - Issue with torch.from_numpy() - Abumentation >=1.4.13 MultiMasks Augmentation - Issue with torch.from_numpy() - Abumentation Sep 18, 2024
@ternaus
Copy link
Collaborator

ternaus commented Sep 18, 2024

Thanks, I can reproduce the bug.

Will take a look ASAP.

@ternaus
Copy link
Collaborator

ternaus commented Sep 18, 2024

In [4]: import albumentations as A
   ...: from albumentations.pytorch import ToTensorV2
   ...: import cv2
   ...: import numpy as np
   ...:
   ...: transform = A.Compose([
   ...:         A.HorizontalFlip(p=1),
   ...:         A.ToFloat(max_value=255),
   ...:         ToTensorV2()
   ...:     ],is_check_shapes=False)
   ...:
   ...:
   ...: image = 255*np.ones((3096,2048,3),dtype=np.uint8)
   ...: masks = [np.ones((3096,2048),dtype=np.uint8) for i in range(2)]
   ...:
   ...: transformed = transform(image=image,masks=masks)

In [5]: A.__version__
Out[5]: '1.4.15'

Could you please check with the latest version?

@FrsECM
Copy link
Author

FrsECM commented Sep 19, 2024

For me it's not fixed with the last version :

import albumentations as A
from albumentations.pytorch import ToTensorV2
import cv2
import numpy as np
import torch
from albumentations.core.types import NUM_MULTI_CHANNEL_DIMENSIONS

################### WORKARROUND THAT WORK
class ToTensor(ToTensorV2):
    def apply_to_mask(self, mask: np.ndarray, **params: A.Any) -> torch.Tensor:
        if self.transpose_mask and mask.ndim == NUM_MULTI_CHANNEL_DIMENSIONS:
            mask = mask.transpose(2, 0, 1)
        return torch.from_numpy(np.ascontiguousarray(mask))

transform_buggy = A.Compose([
        A.HorizontalFlip(p=1),
        A.ToFloat(max_value=255),
        ToTensorV2()
    ],is_check_shapes=False)


transform_fixed = A.Compose([
        A.HorizontalFlip(p=1),
        A.ToFloat(max_value=255),
        ToTensor()
    ],is_check_shapes=False)

image = 255*np.ones((3096,2048,3),dtype=np.uint8)
masks = [np.ones((3096,2048),dtype=np.uint8) for i in range(2)]

print(A.__version__)
try:
    transformed = transform_fixed(image=image,masks=masks)
except:
    print('transform_fixed failed')
try:
    transformed_buggy = transform_buggy(image=image,masks=masks)
except:
    print('transform_buggy failed')
print('The end')

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Need more info
Projects
None yet
Development

No branches or pull requests

2 participants