forked from lolipopshock/Seq_Scene_Gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
72 lines (56 loc) · 2.41 KB
/
utils.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
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import itertools
import numpy as np
import torch
def show_result(num_epoch,test_images, show = False, save = False, path = 'result.png'):
size_figure_grid = int(np.sqrt(test_images.size()[0]))
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(size_figure_grid*size_figure_grid):
i = k // size_figure_grid
j = k % size_figure_grid
ax[i, j].cla()
if test_images.size()[1] == 1:
ax[i, j].imshow((test_images[k].cpu().data.numpy().transpose(1, 2, 0).squeeze() + 1) / 2, cmap='gray')
else:
ax[i, j].imshow((test_images[k].cpu().data.numpy().transpose(1, 2, 0) + 1) / 2)
label = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, label, ha='center')
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def show_result_rgb(num_epoch, test_images, show=False, save=False, path='result.png'):
# size_figure_grid = int(np.sqrt(test_images.size()[0]))
size_figure_grid = 2
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(size_figure_grid):
i = k // size_figure_grid
j = k % size_figure_grid
ax[i, j].cla()
if test_images.size()[1] == 1:
ax[i, j].imshow((test_images[k].cpu().data.numpy().transpose(1, 2, 0).squeeze() + 1) / 2, cmap='gray')
else:
ax[i, j].imshow((test_images[k].cpu().data.numpy().transpose(1, 2, 0) + 1) / 2)
label = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, label, ha='center')
if save:
plt.savefig(path)
if show:
plt.show()
else:
plt.close()
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
def mse_loss(input, target):
return torch.sum((input - target)**2) / input.data.nelement()