-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.py
More file actions
106 lines (73 loc) · 2.46 KB
/
utils.py
File metadata and controls
106 lines (73 loc) · 2.46 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import numpy as np
import scipy
def add_parameter(class_object, kwargs, parameter, default=None):
""" I find the typical way of adding parameters to classes a little opaque,
so I added this method.
Parameters
----------
class_object : Object
Object to assign attribute to.
kwargs : dict
Parameters passed to object.
parameter : str
Name of attribute
default : None, optional
Default value of attribute.
"""
if parameter in kwargs:
setattr(class_object, parameter, kwargs.get(parameter))
else:
setattr(class_object, parameter, default)
def merge(images, size, channels=3):
"""I grabbed this code from someone else, but now I don't remember where :(.
Parameters
----------
images : array
[batch_size x row x column x RGB] input array
size : tuple
row x column input tuple, specifying dimensions of the mosaic
channels : int, optional
Number of channels in image.
Returns
-------
array
Description
"""
h, w = images.shape[1], images.shape[2]
img = np.zeros((h * size[0], w * size[1], channels))
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
def save_grid_images(images, size, path):
"""
"""
if images.shape[-1] == 3:
return scipy.misc.imsave(path, merge(images, size))
elif images.shape[-1] == 1:
scipy.misc.imsave(path, np.squeeze(merge(images[..., 0][..., np.newaxis], size, channels=1)))
def inverse_transform(image):
""" Reverse intensity normalization for image rendering purposes.
"""
return ((image + 1.) * 127.5).astype(np.uint8)
def save_images(images, size, image_path):
"""Saves a batch of images in [batch_size, row, column, RGB] format into
a composite mosaic image.
Parameters
----------
images : array
[batch_size x row x column x RGB] input array
size : tuple
row x column input tuple, specifying dimensions of the mosaic
image_path : str
Output image filepath to save to.
"""
data = inverse_transform(images)
save_grid_images(data, size, image_path)
def save_image(data, image_path):
""" Just a wrapper around scipy /shrug
"""
return scipy.misc.imsave(image_path, data)
if __name__ == '__main__':
pass