|
7 | 7 | import numpy as np |
8 | 8 | import random |
9 | 9 | import dill |
| 10 | +import logging |
10 | 11 |
|
11 | 12 |
|
12 | 13 | # Credit - Bahar |
@@ -71,32 +72,46 @@ def add(self, mapReader): |
71 | 72 | for x in range(self.window_size[0])]) |
72 | 73 |
|
73 | 74 | #Generate image patches and write to data/output directory |
74 | | - def generate_patches(self, mapReader, image_groups=3): |
| 75 | + def generate_patches(self, mapReader, image_groups=3, outDirectory=None): |
75 | 76 | """_summary_ |
76 | 77 |
|
77 | 78 | Args: |
78 | 79 | mapReader (MapReader): reader for a single big map! |
79 | 80 | image_groups (int, optional): _description_. Defaults to 3. |
80 | 81 | """ |
81 | | - outDirectory = os.path.join(self.outputDir, mapReader.mapName, str(self.window_size)) |
| 82 | + if outDirectory is None: |
| 83 | + outDirectory = os.path.join(self.outputDir, mapReader.mapName, str(self.window_size)) |
82 | 84 | os.makedirs(outDirectory, exist_ok=True) |
| 85 | + |
83 | 86 | img_group_number = 0 |
84 | 87 | for i in range(0, mapReader.size[0] - self.window_size[0] + 1, self.step_size): |
85 | 88 | for j in range(0, mapReader.size[1] - self.window_size[1] + 1, self.step_size): |
86 | | - self.samples.append([[ |
87 | | - (self.converter.get_char(mapReader.data[i + x][j + y]) / (len(self.converter.char_groups) - 1)) * -2 + 1 |
88 | | - for y in range(self.window_size[1])] |
89 | | - for x in range(self.window_size[0])]) |
90 | 89 |
|
91 | | - if len(self.samples)==self.sample_group_size: |
| 90 | + sample = self.extractSample(mapReader, topLeft=(i, j)) |
| 91 | + |
| 92 | + self.samples.append(sample) |
| 93 | + |
| 94 | + if len(self.samples) == self.sample_group_size: |
92 | 95 | path = os.path.join(outDirectory, str(img_group_number) + ".dill") |
93 | 96 | with open(path, 'wb+') as f: |
94 | 97 | dill.dump(self.samples, f) |
95 | 98 | f.close() |
96 | | - print("Image group {} saved in data/output/ !".format(img_group_number)) |
| 99 | + logging.info(f"Image group {img_group_number} saved in {path}") |
97 | 100 | self.samples.clear() |
98 | 101 | img_group_number+=1 |
99 | | - |
| 102 | + |
| 103 | + def extractSample(self, mapReader, topLeft): |
| 104 | + i = topLeft[0] |
| 105 | + j = topLeft[1] |
| 106 | + sample = [ |
| 107 | + [ |
| 108 | + (self.converter.get_char(mapReader.data[i + x][j + y]) / (len(self.converter.char_groups) - 1)) * -2 + 1 # TODO this conversion should be done once in the original data instead of patches. |
| 109 | + for y in range(self.window_size[1]) |
| 110 | + ] |
| 111 | + for x in range(self.window_size[0]) |
| 112 | + ] |
| 113 | + return sample |
| 114 | + |
100 | 115 |
|
101 | 116 | def shuffle(self): |
102 | 117 | random.shuffle(self.samples) |
|
0 commit comments