Reputation: 35
I am trying to show results of GAN network on some specified epochs. The function for printing the current result was used previously with TF. I need to change in to pytorch.
def show_result(G_net, z_, num_epoch, show=False, save=False, path='result.png'):
#test_images = sess.run(G_z, {z: z_, drop_out: 0.0})
test_images = G_net(z_)
size_figure_grid = 5
fig, ax = plt.subplots(size_figure_grid, size_figure_grid, figsize=(5, 5))
for i, j in itertools.product(range(size_figure_grid), range(size_figure_grid)):
ax[i, j].get_xaxis().set_visible(False)
ax[i, j].get_yaxis().set_visible(False)
for k in range(5*5):
i = k // 5
j = k % 5
ax[i, j].cla()
ax[i, j].imshow(np.reshape(test_images[k], (28, 28)), cmap='gray')
label = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, label, ha='center')
plt.savefig(name)
file = drive.CreateFile({'title': label, "parents": [{"kind": "https://drive.google.com/drive/u/0/folders/", "id": folder_id}]})
file.SetContentFile(name)
file.Upload()
if num_epoch == 10 or num_epoch == 20 or num_epoch == 50 or num_epoch == 100:
plt.show()
plt.close()
The results I need to obtain looks like that: result img
I am getting this error, however I am not sure what I did incorrectly
Can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first
Upvotes: 3
Views: 12420
Reputation: 2496
I am assuming G_net is your network. It looks like you store the network in the GPU, hence the returned result test_images will also be in GPU. You will need to move it to cpu and convert to numpy:
#test_images = G_net(z_)
test_images = G_net(z_).detach().cpu().numpy()
This will detach the tensor from the graph, move to cpu and then convert to numpy.
Upvotes: 8