-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
72 lines (54 loc) · 1.72 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
# coding: utf-8
# In[2]:
from __future__ import division
import math
import json
import random
import pprint
import scipy.misc
import numpy as np
from time import gmtime, strftime
from six.moves import xrange
# In[6]:
def imsave(images, size, path):
return scipy.misc.imsave(path, merge(images, size))
# In[4]:
def merge(images, size):
"""Args:
images: imshow images of shape [batch_size, height, weight, 3]
size: imshow image contain how much images
Returns:
imshow images of size [h * size[0], w * size[1], 3]"""
h, w = images.shape[1], images.shape[2]
img = np.zeros((h * size[0], w * size[1], 3))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[j * h : j * h + h, i * w : i * w + w, :] = image
return img
# In[5]:
def save_images(images, size, image_path):
"""Args: images of shape[batch_size, height, weight, 3]
size of shape [n_h, n_w]
path: name of imsaved images
"""
return imsave(images, size, image_path)
# In[ ]:
def transform(X, height = 64, weight = 64):
assert X[0].shape == (height, weight, 3) or X[0].shape == (3, height, weight)
if X[0].shape == (3, height, weight):
X = X.transpose(0, 2, 3, 1)
return np.array(X) / 127.5 - 1
def inverse_transform(images):
return (images + 1.) / 2.0
def imsize(X):
"""Args:
X is batch of images[batch_size, 64, 64, 3]
Return:
resize images([batch_size, 32, 32, 3)
"""
batch_size = X.shape[0]
sample_outs = np.zeros((batch_size, 32, 32, 3))
for i in range(batch_size):
sample_outs[i] = scipy.misc.imresize(X[i], (32, 32))
return sample_outs