Very weird behavior when converting to torch.tensor

I’m pretty new to PyTorch so, please excuse me if this question is too remedial. I have the following code for inference of my trained model which takes an image, does some pre-processing on it, converts to a tensor and finally performs a forward pass through the network.

   img = np.array(Image.open(img_path))

        orig_size = img.shape[:2][::-1]

        img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)[None]).float()
        if has_cuda:
            img_inp = img_inp.cuda()

        segm = net(img_inp)[0].data.cpu().numpy().transpose(1, 2, 0)

I have successfully trained this segmentation model on my desktop machine. The inference code runs perfectly on it, but when I clone it to my laptop it gives me the following error:

       img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)[None]).float()
RuntimeError: unsupported operation: more than one element of the written-to tensor refers to a single memory location. Please clone() the tensor before performing the operation.

I can bypass this error on my laptop by passing the result of prepare_img as a copy but that leads to memory leakages.

PS. On my Laptop I have built PyTorch from source for GPU compute and the torch version is 1.5.0a0+55c382e. I have a hunch that this is the problem. My desktop machine has PyTorch installed simply from anaconda’s repo.

Any help on this will be greatly appreciated. Thanks

Hi,

The problem is most likely that you try ot modify inplace a Tensor that does not support it. These checks were added fairly recently to prevent users from seeing behavior that are usually unexpected.

Could you give a small code sample that reproduces the error please?

Hey, @albanD thanks for the reply. You can check out the code in this repo. This is what I’m trying to re-purpose. Try running any one of the example notebooks. on PyTorch 1.5.0a0+55c382e

Seems like the main problem is when the torch.tensor is converted to float. When it’s removed at least the Run-time error is gone(appears instead when tensor is converted to .cuda()) but now how do I change to torch.float dtype?(already tried .type(torch.float))

I think .float only works for scalarr, it’ll return the value as a python float. If u want to convert types for tensors, try a = a.to(dtype=tc.flaot)

@G.M Thanks for the reply. Still the same problem:

  img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)[None]).to(dtype=torch.float)
RuntimeError: unsupported operation: more than one element of the written-to tensor refers to a single memory location. Please clone() the tensor before performing the operation.

How come this works?

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a).float()   # because we change the dtype 'b' contains a copy of  'a' 
np.add(a, 1, out=a) 

print(a, a.dtype)
>>> [2. 2. 2. 2. 2.] float64

print(b, b.dtype)
>>> tensor([1., 1., 1., 1., 1.]) torch.float32

Secondly, this works as expected:

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a) # changed to np array, changes the torch.tensor

print(a, a.dtype)
>>> [2. 2. 2. 2. 2.] float64

print(b, )
>>> tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

Do u have any in-place operations or operations like expand in ur code? I’ve experienced the issue with the usage of expand.

Nope. Can’t find any expand commands. You can check out the repo here

Can u try creating the tensor like this?

img_inp = torch.from_numpy(prepare_img(img).transpose(2, 0, 1)[None]).to(dtype=torch.float,device=UR_DEVICE)

Hi,

The issue comes from the [None] I think. You change this line to first convert to a Tensor then .unsqueeze(0) to get the exact same result:

img_inp = torch.tensor(prepare_img(img).transpose(2, 0, 1)).float().unsqueeze(0)
1 Like

Thanks a bung @albanD got it working