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.
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
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))
@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.