X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=grid.py;h=268f4eed9ba64a57899a575828ba98cde55e1e13;hb=76e62a5782fc2509ce989fcfc0d0aedc17322b3a;hp=f72c8e34ce417e50daf6135d4dc73d6af44fca04;hpb=0e1e208852b83f6a3d59e5caabd2f0f1f4bde94e;p=picoclvr.git diff --git a/grid.py b/grid.py index f72c8e3..268f4ee 100755 --- a/grid.py +++ b/grid.py @@ -19,11 +19,12 @@ name_colors = ["red", "yellow", "blue", "green", "white", "purple"] class GridFactory: def __init__( self, - size=4, + size=6, max_nb_items=4, max_nb_transformations=3, nb_questions=4, ): + assert size % 2 == 0 self.size = size self.max_nb_items = max_nb_items self.max_nb_transformations = max_nb_transformations @@ -137,23 +138,20 @@ class GridFactory: properties += [f"a {n1} is right of a {n2}"] if j1 < j2: properties += [f"a {n1} is left of a {n2}"] + if abs(i1 - i2) + abs(j1 - j2) == 1: + properties += [f"a {n1} is next to a {n2}"] return properties def generate_scene_and_questions(self): while True: while True: - scene = self.generate_scene() + start_scene = self.generate_scene() + scene, transformations = self.random_transformations(start_scene) true = self.all_properties(scene) if len(true) >= self.nb_questions: break - start = self.grid_positions(scene) - - scene, transformations = self.random_transformations(scene) - - # transformations=[] - for a in range(10): col, shp = scene col, shp = col.view(-1), shp.view(-1) @@ -163,8 +161,17 @@ class GridFactory: col.view(self.size, self.size), shp.view(self.size, self.size), ) - # other_scene = self.generate_scene() - false = list(set(self.all_properties(other_scene)) - set(true)) + + false = self.all_properties(other_scene) + + # We sometime add properties from a totally different + # scene to have negative "there is a xxx xxx" + # properties + if torch.rand(1).item() < 0.2: + other_scene = self.generate_scene() + false += self.all_properties(other_scene) + + false = list(set(false) - set(true)) if len(false) >= self.nb_questions: break @@ -173,19 +180,19 @@ class GridFactory: true = [true[k] for k in torch.randperm(len(true))[: self.nb_questions]] false = [false[k] for k in torch.randperm(len(false))[: self.nb_questions]] - true = [" " + q + " " for q in true] - false = [" " + q + " " for q in false] + true = [" " + q + " true" for q in true] + false = [" " + q + " false" for q in false] union = true + false questions = [union[k] for k in torch.randperm(len(union))[: self.nb_questions]] result = " ".join( - [" " + x for x in self.grid_positions(scene)] + [" " + x for x in self.grid_positions(start_scene)] + transformations + questions ) - return scene, result + return start_scene, scene, result def generate_samples(self, nb, progress_bar=None): result = [] @@ -195,7 +202,7 @@ class GridFactory: r = progress_bar(r) for _ in r: - result.append(self.generate_scene_and_questions()[1]) + result.append(self.generate_scene_and_questions()[2]) return result @@ -207,13 +214,23 @@ if __name__ == "__main__": grid_factory = GridFactory() - start_time = time.perf_counter() - samples = grid_factory.generate_samples(10000) - end_time = time.perf_counter() - print(f"{len(samples) / (end_time - start_time):.02f} samples per second") - - scene, questions = grid_factory.generate_scene_and_questions() + # start_time = time.perf_counter() + # samples = grid_factory.generate_samples(10000) + # end_time = time.perf_counter() + # print(f"{len(samples) / (end_time - start_time):.02f} samples per second") + + start_scene, scene, questions = grid_factory.generate_scene_and_questions() + print() + print("-- Original scene -----------------------------") + print() + grid_factory.print_scene(start_scene) + print() + print("-- Transformed scene --------------------------") + print() grid_factory.print_scene(scene) + print() + print("-- Sequence -----------------------------------") + print() print(questions) ######################################################################