3 # Any copyright is dedicated to the Public Domain.
4 # https://creativecommons.org/publicdomain/zero/1.0/
6 # Written by Francois Fleuret <francois@fleuret.org>
9 import torch, torchvision
10 import torch.nn.functional as F
12 name_shapes = ["A", "B", "C", "D", "E", "F"]
14 name_colors = ["red", "yellow", "blue", "green", "white", "purple"]
16 ######################################################################
24 max_nb_transformations=3,
29 self.max_nb_items = max_nb_items
30 self.max_nb_transformations = max_nb_transformations
31 self.nb_questions = nb_questions
33 def generate_scene(self):
34 nb_items = torch.randint(self.max_nb_items - 1, (1,)).item() + 2
35 col = torch.full((self.size * self.size,), -1)
36 shp = torch.full((self.size * self.size,), -1)
37 a = torch.randperm(len(name_colors) * len(name_shapes))[:nb_items]
38 col[:nb_items] = a % len(name_colors)
39 shp[:nb_items] = a // len(name_colors)
40 i = torch.randperm(self.size * self.size)
43 return col.reshape(self.size, self.size), shp.reshape(self.size, self.size)
45 def random_transformations(self, scene):
49 nb_transformations = torch.randint(self.max_nb_transformations + 1, (1,)).item()
50 transformations = torch.randint(5, (nb_transformations,))
52 for t in transformations:
54 col, shp = col.flip(0), shp.flip(0)
55 descriptions += ["<chg> vertical flip"]
57 col, shp = col.flip(1), shp.flip(1)
58 descriptions += ["<chg> horizontal flip"]
60 col, shp = col.flip(0).t(), shp.flip(0).t()
61 descriptions += ["<chg> rotate 90 degrees"]
63 col, shp = col.flip(0).flip(1), shp.flip(0).flip(1)
64 descriptions += ["<chg> rotate 180 degrees"]
66 col, shp = col.flip(1).t(), shp.flip(1).t()
67 descriptions += ["<chg> rotate 270 degrees"]
69 col, shp = col.contiguous(), shp.contiguous()
71 return (col, shp), descriptions
73 def print_scene(self, scene):
76 # for i in range(self.size):
77 # for j in range(self.size):
79 # print(f"at ({i},{j}) {name_colors[col[i,j]]} {name_shapes[shp[i,j]]}")
81 for i in range(self.size):
82 for j in range(self.size):
84 print(f"{name_colors[col[i,j]][0]}{name_shapes[shp[i,j]]}", end="")
94 for j in range(self.size - 1):
98 def grid_positions(self, scene):
103 for i in range(self.size):
104 for j in range(self.size):
106 n = f"{name_colors[col[i,j]]} {name_shapes[shp[i,j]]}"
107 properties += [f"a {n} at {i} {j}"]
111 def all_properties(self, scene):
116 for i1 in range(self.size):
117 for j1 in range(self.size):
119 n1 = f"{name_colors[col[i1,j1]]} {name_shapes[shp[i1,j1]]}"
120 properties += [f"there is a {n1}"]
121 if i1 < self.size // 2:
122 properties += [f"a {n1} is in the top half"]
123 if i1 >= self.size // 2:
124 properties += [f"a {n1} is in the bottom half"]
125 if j1 < self.size // 2:
126 properties += [f"a {n1} is in the left half"]
127 if j1 >= self.size // 2:
128 properties += [f"a {n1} is in the right half"]
129 for i2 in range(self.size):
130 for j2 in range(self.size):
132 n2 = f"{name_colors[col[i2,j2]]} {name_shapes[shp[i2,j2]]}"
134 properties += [f"a {n1} is below a {n2}"]
136 properties += [f"a {n1} is above a {n2}"]
138 properties += [f"a {n1} is right of a {n2}"]
140 properties += [f"a {n1} is left of a {n2}"]
141 if abs(i1 - i2) + abs(j1 - j2) == 1:
142 properties += [f"a {n1} is next to a {n2}"]
146 def generate_scene_and_questions(self):
149 start_scene = self.generate_scene()
150 scene, transformations = self.random_transformations(start_scene)
151 true = self.all_properties(scene)
152 if len(true) >= self.nb_questions:
157 col, shp = col.view(-1), shp.view(-1)
158 p = torch.randperm(col.size(0))
159 col, shp = col[p], shp[p]
161 col.view(self.size, self.size),
162 shp.view(self.size, self.size),
165 false = self.all_properties(other_scene)
167 # We sometime add properties from a totally different
168 # scene to have negative "there is a xxx xxx"
170 if torch.rand(1).item() < 0.2:
171 other_scene = self.generate_scene()
172 false += self.all_properties(other_scene)
174 false = list(set(false) - set(true))
175 if len(false) >= self.nb_questions:
181 true = [true[k] for k in torch.randperm(len(true))[: self.nb_questions]]
182 false = [false[k] for k in torch.randperm(len(false))[: self.nb_questions]]
183 true = ["<prop> " + q + " <ans> true" for q in true]
184 false = ["<prop> " + q + " <ans> false" for q in false]
187 questions = [union[k] for k in torch.randperm(len(union))[: self.nb_questions]]
190 ["<obj> " + x for x in self.grid_positions(start_scene)]
195 return start_scene, scene, result
197 def generate_samples(self, nb, progress_bar=None):
201 if progress_bar is not None:
205 result.append(self.generate_scene_and_questions()[2])
210 ######################################################################
212 if __name__ == "__main__":
215 grid_factory = GridFactory()
217 # start_time = time.perf_counter()
218 # samples = grid_factory.generate_samples(10000)
219 # end_time = time.perf_counter()
220 # print(f"{len(samples) / (end_time - start_time):.02f} samples per second")
222 start_scene, scene, questions = grid_factory.generate_scene_and_questions()
224 print("-- Original scene -----------------------------")
226 grid_factory.print_scene(start_scene)
228 print("-- Transformed scene --------------------------")
230 grid_factory.print_scene(scene)
232 print("-- Sequence -----------------------------------")
236 ######################################################################