I am using Colab, Torch, in GPU env. But when i start to merge images that are produced by Cycle-Gan, error occurs.
TypeError: can’t convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
So i tried to solve problem by use .cpu…
but i don’t know where to use it. because i don’t know what is the exact problem.
def merge_images(self, sources, targets, k=10):
_, _, h, w = sources.shape
row = int(np.sqrt(self.batch_size))
merged = np.zeros([3, row * h, row * w * 2])
for idx, (s, t) in enumerate(zip(sources, targets)):
i = idx // row
j = idx % row
merged[:, i * h:(i + 1) * h, (j * 2) * h:(j * 2 + 1) * h] = s
merged[:, i * h:(i + 1) * h, (j * 2 + 1) * h:(j * 2 + 2) * h] = t
return merged.transpose(1, 2, 0)
I want to know why this kind of problem occurs and how to solve this.
(sorry to write raw)
When you compute something in the gpu you have a cuda tensor. Pytorch can only cast tensors into numpy arrays.
Pick the image produced by cycle gan and move it to cpu by using
thank you for your response but i have got another problem.
–> 204 merged = self.merge_images(mnist.cpu().numpy(), fake_svhn.cpu().numpy())
205 path = os.path.join(self.sample_path, ‘sample-%d-m-s.png’ %(step+1))
206 scipy.misc.imsave(path, merged)
AttributeError: ‘numpy.ndarray’ object has no attribute ‘cpu’
i changed input image to .cpu().numpy() but it still doesn’t work.
do you know how to solve this???
Please, it’s not a matter of random using that function.
Inputs to neworks must be tensors. Once you get the output or any feature you want to work over using numpy (if and only if it’s a torch tensor), you can apply that operator to convert it to a numpy array.
torchtensor.numpy() if it’s already allocated in cpu