Bugs about torch.from_numpy(array)?

I want to train a 3D network by using 3D images (.nii.gz format). So the first step, I use the nibabel lib to load 3d images in my dataloader class (see following codes).

import nibabel as nib
class Data(Dataset):
    def __init__(...):
    def __getitem__(self, idx):
        image = nib.load(img_path)
        label = nib.load(label_path)
        image = torch.from_numpy(image).unsqueeze(0)
        label = torch.from_numpy(label).unsqueeze(0)
        return image, label

The I imported the Data class to load training sets in my model training script. However, magical bug have appeared. That is, I got ValueError: some of the strides of a given numpy array are negative. This is currently not supported, but will be added in future releases. The location where the bug appears is located to:

image = torch.from_numpy(image).unsqueeze(0)

This bug may appear when I first train and when I train some iterations, which means it doesn’t fix. My PyTorch version is 0.4.1. I am grateful for any suggestions.

You can try: torch.from_numpy(np.ascontiguousarray(image)) to make sure that the numpy array does not have negative strides.

1 Like

Many thanks for your reply. I can train my network now. Thanks again!