|
1 | 1 |
|
2 |
| -import math |
3 | 2 | import os
|
4 | 3 | import random
|
5 | 4 | import time
|
6 |
| -from threading import Event, Thread |
| 5 | +from multiprocessing import Pool |
7 | 6 |
|
8 | 7 | import numpy as np
|
9 | 8 | from PIL import Image, ImageEnhance
|
10 | 9 |
|
11 |
| -from queue import Queue |
12 |
| - |
13 | 10 |
|
14 | 11 | class ImageVariations():
|
15 |
| - def __init__(self, image_size=64, batch_size=64, colored=True, |
16 |
| - pools=8, pool_renew=1, |
| 12 | + def __init__(self, image_size=64, colored=True, pool_size=10000, |
17 | 13 | in_directory='input', out_directory='output',
|
18 | 14 | rotation_range=(-15, 15), brightness_range=(0.7, 1.2),
|
19 | 15 | saturation_range=(0.7, 1.), contrast_range=(0.9, 1.3),
|
20 | 16 | size_range=(0.6, 0.8)):
|
21 | 17 | #Parameters
|
22 | 18 | self.image_size = image_size
|
23 |
| - self.batch_size = batch_size |
24 | 19 | self.in_directory = in_directory
|
25 | 20 | self.out_directory = out_directory
|
26 |
| - self.pools = pools |
27 |
| - self.pool_renew = pool_renew |
| 21 | + self.images_count = pool_size |
28 | 22 | #Variation Config
|
29 | 23 | self.rotation_range = rotation_range
|
30 | 24 | self.brightness_range = brightness_range
|
31 | 25 | self.saturation_range = saturation_range
|
32 | 26 | self.contrast_range = contrast_range
|
33 | 27 | self.size_range = size_range
|
34 | 28 | self.colored = colored
|
35 |
| - #Thread variables |
36 |
| - self.pool = [] |
37 |
| - self.pool_index = 0 |
38 |
| - self.pool_iteration = 0 |
39 |
| - self.queue = Queue() |
40 |
| - self.files = [] |
41 |
| - self.threads = [] |
42 |
| - self.event = Event() |
43 |
| - self.closing = True |
44 |
| - |
45 |
| - def start_threads(self): |
46 |
| - """Start the threads that are generating image variations""" |
47 |
| - self.closing = True |
48 |
| - self.event.set() |
49 |
| - self.files = [f for f in os.listdir(self.in_directory) if os.path.isfile(os.path.join(self.in_directory, f))] |
50 |
| - num_threads = os.cpu_count() |
51 |
| - if num_threads is None: |
52 |
| - num_threads = 4 |
53 |
| - self.threads = [Thread(target=self.__thread__, args=(self.files[i::num_threads],), daemon=True) |
54 |
| - for i in range(num_threads)] |
55 |
| - self.event.clear() |
56 |
| - self.closing = False |
57 |
| - for t in self.threads: |
58 |
| - t.start() |
59 |
| - if(self.pools > 1): |
60 |
| - print('Processing input images') |
61 |
| - self.pool = [[] for _ in range(self.pools)] |
62 |
| - |
63 |
| - def stop_threads(self): |
64 |
| - """Stop the threads that are generating image variations (freeing memory)""" |
65 |
| - self.closing = True |
66 |
| - self.event.set() |
67 |
| - |
68 |
| - def get_batch(self): |
69 |
| - """Get a batch of images as arrays""" |
70 |
| - if self.closing: #Start threads |
71 |
| - self.start_threads() |
72 |
| - self.event.set() |
73 |
| - if len(self.pool[self.pool_index]) == 0: #Check and fill image pool |
74 |
| - self.pool[self.pool_index] = [self.queue.get() for _ in range(self.batch_size)] |
75 |
| - np.random.shuffle(self.pool[self.pool_index]) |
76 |
| - images = self.pool[self.pool_index] |
77 |
| - for i in range(self.pool_renew): #Replace old images |
78 |
| - self.pool[self.pool_index][(self.pool_iteration+i)%self.batch_size] = self.queue.get() |
79 |
| - self.pool_index += 1 |
80 |
| - if self.pool_index == self.pools: #Cycle indexes |
81 |
| - self.pool_index = 0 |
82 |
| - self.pool_iteration = (self.pool_iteration+self.pool_renew)%self.batch_size |
83 |
| - self.event.clear() |
84 |
| - return images |
85 |
| - |
86 |
| - def get_old_batch(self): |
87 |
| - if self.closing or len(self.pool[self.pool_index]) == 0: |
88 |
| - return self.get_batch() |
89 |
| - return self.pool[self.pool_index-1] |
| 29 | + #Generate Images |
| 30 | + self.index = 0 |
| 31 | + if self.images_count > 0: |
| 32 | + if self.images_count > 20: |
| 33 | + print("Processing Images") |
| 34 | + files = [f for f in os.listdir(self.in_directory) if os.path.isfile(os.path.join(self.in_directory, f))] |
| 35 | + np.random.shuffle(files) |
| 36 | + mp = self.images_count//len(files) |
| 37 | + rest = self.images_count%len(files) |
| 38 | + if mp > 0: |
| 39 | + pool = Pool() |
| 40 | + images = pool.starmap(self.__generate_images__, [(f, mp) for f in files]) |
| 41 | + self.pool = [img for sub in images for img in sub] |
| 42 | + pool.close() |
| 43 | + else: |
| 44 | + self.pool = [] |
| 45 | + self.pool += [img for sub in [self.__generate_images__(f, 1) for f in files[:rest]] for img in sub] |
| 46 | + np.random.shuffle(self.pool) |
90 | 47 |
|
91 |
| - def __thread__(self, files): |
| 48 | + def __generate_images__(self, image_file, iterations): |
92 | 49 | if self.colored:
|
93 |
| - images = [Image.open(os.path.join(self.in_directory, file)) for file in files] |
| 50 | + image = Image.open(os.path.join(self.in_directory, image_file)) |
94 | 51 | else:
|
95 |
| - images = [Image.open(os.path.join(self.in_directory, file)).convert("L") for file in files] |
96 |
| - index = 0 |
97 |
| - while not self.closing: |
98 |
| - image = images[index] |
99 |
| - index = (index+1)%len(images) |
| 52 | + image = Image.open(os.path.join(self.in_directory, image_file)).convert("L") |
| 53 | + def variation_to_numpy(): |
100 | 54 | arr = np.asarray(self.get_variation(image), dtype=np.float)
|
101 | 55 | if not self.colored:
|
102 | 56 | arr.shape = arr.shape+(1,)
|
103 |
| - self.queue.put(arr) |
104 |
| - while self.queue.qsize() >= self.batch_size and not self.closing: |
105 |
| - self.event.wait() |
| 57 | + return arr |
| 58 | + return [variation_to_numpy() for _ in range(iterations)] |
| 59 | + |
| 60 | + |
| 61 | + def get_batch(self, count): |
| 62 | + """Get a batch of images as arrays""" |
| 63 | + if self.index + count < len(self.pool): |
| 64 | + batch = self.pool[self.index:self.index+count] |
| 65 | + self.index += count |
| 66 | + return batch |
| 67 | + else: |
| 68 | + batch = self.pool[self.index:] |
| 69 | + self.index = 0 |
| 70 | + np.random.shuffle(self.pool) |
| 71 | + return batch + self.get_batch(count - len(batch)) |
| 72 | + |
| 73 | + def get_rnd_batch(self, count): |
| 74 | + if count > len(self.pool): |
| 75 | + return self.get_batch(count) |
| 76 | + index = np.random.randint(0, len(self.pool)-count) |
| 77 | + return self.pool[index:index+count] |
106 | 78 |
|
107 | 79 | def get_variation(self, image):
|
108 | 80 | """Get an variation of the image according to the object config"""
|
@@ -155,19 +127,14 @@ def save_image(self, image, name=None):
|
155 | 127 |
|
156 | 128 | if __name__ == "__main__":
|
157 | 129 | if len(os.sys.argv) > 1:
|
158 |
| - imgvariations = ImageVariations(pools=1, batch_size=int(os.sys.argv[1])) |
159 |
| - imgvariations.start_threads() |
160 |
| - images_batch = imgvariations.get_batch() |
161 |
| - imgvariations.stop_threads() |
162 |
| - for variant_id in range(int(os.sys.argv[1])): |
| 130 | + num_imgs = int(os.sys.argv[1]) |
| 131 | + imgvariations = ImageVariations(pool_size=num_imgs) |
| 132 | + images_batch = imgvariations.get_batch(num_imgs) |
| 133 | + for variant_id in range(num_imgs): |
163 | 134 | imgvariations.save_image(images_batch[variant_id], name="variant_%d"%variant_id)
|
164 |
| - print("Generated %s image variations as they are when fed to the network"%os.sys.argv[1]) |
| 135 | + print("Generated %i image variations as they are when fed to the network"%num_imgs) |
165 | 136 | else:
|
166 | 137 | print("Testing memory requiremens")
|
167 |
| - imgvariations = ImageVariations() |
168 |
| - input("Press Enter to continue... (all images loaded and pools filled)") |
169 |
| - iml1 = imgvariations.get_batch() |
170 |
| - iml2 = imgvariations.get_batch() |
171 |
| - iml3 = imgvariations.get_batch() |
172 |
| - iml4 = imgvariations.get_batch() |
173 |
| - input("Press Enter to continue... (also four batches)") |
| 138 | + num_imgs = 10000 |
| 139 | + imgvariations = ImageVariations(pool_size=num_imgs) |
| 140 | + input("Press Enter to continue... (Pool countains %i images)"%num_imgs) |
0 commit comments