fastai / course-v3

The 3rd edition of course.fast.ai
https://course.fast.ai/
Apache License 2.0
4.9k stars 3.55k forks source link

TypeError: can't convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first. #509

Open rraminen opened 4 years ago

rraminen commented 4 years ago

In cyclegan.ipynb and cyclegan_ws.ipynb, show_best(fnames, losses, gen, n): image

Would this be the right fix:

def show_best(fnames, losses, gen, n=8):
    sort_idx = losses.argsort().cpu()
    _,axs = plt.subplots(n//2, 4, figsize=(12,2*n))
    xb = get_batch(fnames[sort_idx][:n], tfms, size=128)
    with torch.no_grad():
        fakes = gen(xb)
    xb,fakes = (1+xb.cpu())/2,(1+fakes.cpu())/2
    for i in range(n):
        axs.flatten()[2*i].imshow(xb[i].permute(1,2,0))
        axs.flatten()[2*i].axis('off')
        axs.flatten()[2*i+1].imshow(fakes[i].permute(1,2,0))
        axs.flatten()[2*i+1].set_title(losses[sort_idx][i].item())
        axs.flatten()[2*i+1].axis('off')