-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualizer.py
52 lines (40 loc) · 1.42 KB
/
visualizer.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
import os
import torch
import imageio
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
try:
__IPYTHON__
ipython = True
except NameError:
ipython = False
# load image as tensor
def load_img(path):
return torch.FloatTensor(np.array(Image.open(path))) / 255.0
# save a tensor batch of images
def save_img(tensorbatch, savepath, name='', savegif=False):
os.makedirs(savepath, exist_ok=True)
n_images = len(tensorbatch)
digits = int( np.floor(np.log10(n_images)) + 2 )
tensorbatch = (tensorbatch * 255).detach().cpu().numpy().astype(np.uint8)
for i in range(n_images):
filename = name + '%s.png' % str(i).zfill(digits)
filepath = os.path.join(savepath, filename)
img = Image.fromarray(tensorbatch[i])
img.save(filepath)
if savegif:
gifname = os.path.basename( os.path.normpath(savepath) )
gifpath = os.path.join(savepath, '%s.gif' % gifname)
imageio.mimsave(gifpath, tensorbatch)
# visualize a tensor or a sequence of tensors
def visualize(images, size=(8,8), savefile=None):
if type(images) is not torch.Tensor: images = torch.hstack(images)
images = images.detach().cpu()
plt.figure(plt.gcf(), figsize=size)
plt.imshow(images)
plt.axis('off')
plt.show() if ipython else plt.pause(1)
plt.clf()
saveimg = (images * 255).numpy().astype(np.uint8)
if savefile: plt.imsave(savefile, saveimg)