forked from Naresh1318/GANs_N_Roses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
86 lines (76 loc) · 3.61 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import os
import sys
from PIL import Image, ImageOps
def merge(images, size):
"""
Generates a merged image from all the input images.
:param images: Images to be merged.
:param size: [number of rows, number of columns]
:return: Merged image.
"""
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[int(j) * h:int(j) * h + h, int(i) * w:int(i) * w + w, :] = image
return img
def load_dataset(path, data_set='birds', image_size=64):
"""
Loads the images from the specified path
:param path: string indicating the dataset path.
:param data_set: 'birds' -> loads data from birds directory, 'flowers' -> loads data from the flowers directory.
:param image_size: size of images in the returned array
:return: numpy array, shape : [number of images, image_size, image_size, 3]
"""
if data_set == 'birds':
all_dirs = os.listdir(path)[0]
image_dirs = [i for i in all_dirs if i.endswith(".jpg") or i.endswith(".jpeg") or i.endswith(".png")]
number_of_images = len(image_dirs)
images = []
print("{} images are being loaded...".format(data_set[:-1]))
for i in image_dirs:
for c, j in enumerate(os.listdir(path + i)):
images.append(np.array(ImageOps.fit(Image.open(path + i + '/' + j),
(image_size, image_size), Image.ANTIALIAS))/127.5 - 1.)
sys.stdout.write("\r Loading : {}/{}"
.format(c + 1, number_of_images))
print("\n")
images = np.reshape(images, [-1, image_size, image_size, 3])
return images.astype(np.float32)
elif data_set == 'roses':
all_dirs = os.listdir(path)
image_dirs = [i for i in all_dirs if i.endswith(".jpg") or i.endswith(".jpeg") or i.endswith(".png")]
number_of_images = len(image_dirs)
images = []
print("{} images are being loaded...".format(data_set[:-1]))
for c, i in enumerate(image_dirs):
images.append(np.array(ImageOps.fit(Image.open(path + '/' + i),
(image_size, image_size), Image.ANTIALIAS))/127.5 - 1.)
sys.stdout.write("\r Loading : {}/{}"
.format(c + 1, number_of_images))
print("\n")
images = np.reshape(images, [-1, image_size, image_size, 3])
return images.astype(np.float32)
elif data_set == 'black_birds':
all_dirs = os.listdir(path)
image_dirs = [i for i in all_dirs if i.endswith(".jpg") or i.endswith(".jpeg") or i.endswith(".png")]
number_of_images = len(image_dirs)
images = []
print("{} images are being loaded...".format(data_set[:-1]))
for c, i in enumerate(image_dirs):
images.append(np.array(ImageOps.fit(Image.open(path + i),
(image_size, image_size), Image.ANTIALIAS))/127.5 - 1.)
sys.stdout.write("\r Loading : {}/{}".format(c + 1, number_of_images))
print("\n")
images = np.reshape(images, [-1, image_size, image_size, 3])
return images.astype(np.float32)
def next_batch(data, batch_size):
"""
Returns a random chosen batch from the data array.
:param data: numpy array consisting the entire dataset
:param batch_size: should I even explain.
:return: [batch_size, default image size, default image size, 3]
"""
return np.random.permutation(data)[:batch_size]