-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
67 lines (51 loc) · 2.18 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
import os
import numpy as np
from cntk.io import (MinibatchSource, CTFDeserializer, StreamDef, StreamDefs,
INFINITELY_REPEAT, FULL_DATA_SWEEP)
import matplotlib.pyplot as plt
import imageio
def check_path(path):
if not os.path.exists(path):
readme_file = os.path.normpath(os.path.join(
os.path.dirname(path), "..", "README.md"))
raise RuntimeError(
"File '%s' does not exist. Please follow the instructions at %s to download and prepare it." % (path, readme_file))
def create_reader(path, is_training, input_dim):
return MinibatchSource(CTFDeserializer(path, StreamDefs(
features = StreamDef(field='features', shape=input_dim, is_sparse=False),
)), randomize=is_training, max_sweeps = INFINITELY_REPEAT if is_training else 1)
np.random.seed(np.random.randint(100))
def noise_sample(num_samples, embedding):
return np.random.uniform(
low = -1.0,
high = 1.0,
size = [num_samples, embedding]
).astype(np.float32)
def plot_images(images, subplot_shape):
plt.style.use('ggplot')
fig, axes = plt.subplots(*subplot_shape)
for image, ax in zip(images, axes.flatten()):
ax.imshow(image.reshape(28, 28), vmin=0, vmax=1.0, cmap='gray')
ax.axis('off')
plt.show()
def save_images(images, size, image_path=''):
if len(images.shape) == 3: # Greyscale [batch, h, w] --> [batch, h, w, 1]
images = images[:, :, :, np.newaxis]
def merge(images, size):
h, w = images.shape[1], images.shape[2]
img = np.zeros((h * size[0], w * size[1], 3), dtype=images.dtype)
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 imsave(images, size, path):
return imageio.imwrite(path, merge(images, size))
assert len(images) <= size[0] * size[1], "number of images should be equal or less than size[0] * size[1] {}".format(len(images))
return imsave(images, size, image_path)
def exists_or_mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
return False
else:
return True