-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
executable file
·74 lines (56 loc) · 2.33 KB
/
visualize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import torch
import os
import imageio
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def plot_loss(losses, num_epoch, epoches, save_dir):
losses = np.array(losses)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
fig, ax = plt.subplots()
ax.set_xlim(0,epoches + 1)
ax.set_ylim(0, max(np.max(losses.T[1]), np.max(losses.T[0])) * 1.1)
plt.xlabel('Epoch {}'.format(num_epoch))
plt.ylabel('Loss')
plt.plot(losses.T[0], label='Discriminator', alpha=0.5)
plt.plot(losses.T[1], label='Generator', alpha=0.5)
plt.legend()
plt.savefig(os.path.join(save_dir, 'DCGAN_loss_epoch_{}.png'.format(num_epoch)))
plt.close()
def plot_result(G, fixed_noise, image_size, num_epoch, save_dir, fig_size=(8, 8), is_gray=False):
if not os.path.exists(save_dir):
os.makedirs(save_dir)
G.eval()
train_on_gpu = torch.cuda.is_available()
if train_on_gpu:
fixed_noise = fixed_noise.cuda()
generate_images = G(fixed_noise)
G.train()
n_rows = 8
n_cols = 8
fig, axes = plt.subplots(n_rows, n_cols, figsize=fig_size)
for ax, img in zip(axes.flatten(), generate_images):
ax.axis('off')
ax.set_adjustable('box-forced')
if is_gray:
img = img.cpu().data.view(image_size, image_size).numpy()
ax.imshow(img, cmap='gray', aspect='equal')
else:
img = (((img - img.min()) * 255) / (img.max() - img.min())).cpu().data.numpy().transpose(1, 2, 0).astype(np.uint8)
ax.imshow(img, cmap=None, aspect='equal')
plt.subplots_adjust(wspace=0, hspace=0)
title = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, title, ha='center')
plt.savefig(os.path.join(save_dir, 'DCGAN_epoch_{}.png'.format(num_epoch)))
plt.close()
def create_gif(epoches, save_dir):
images = []
for i in range(1, epoches + 1):
images.append(imageio.imread(os.path.join(save_dir, 'DCGAN_epoch_{}.png'.format(i))))
imageio.mimsave(os.path.join(save_dir, 'result.gif'), images, fps=5)
images = []
for i in range(1, epoches + 1):
images.append(imageio.imread(os.path.join(save_dir, 'DCGAN_loss_epoch_{}.png'.format(i))))
imageio.mimsave(os.path.join(save_dir, 'result_loss.gif'), images, fps=5)