From: François Fleuret Date: Sat, 29 Jun 2024 13:11:20 +0000 (+0300) Subject: Update. X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=commitdiff_plain;h=bee6e628aabc1380772409f6aabffb024c0e70ab;p=culture.git Update. --- diff --git a/main.py b/main.py index d7fb3d1..1565499 100755 --- a/main.py +++ b/main.py @@ -343,7 +343,6 @@ def run_tests(model, quizz_machine, deterministic_synthesis): n_epoch=n_epoch, model=model, result_dir=args.result_dir, - logger=log_string, deterministic_synthesis=deterministic_synthesis, ) @@ -397,7 +396,6 @@ def create_c_quizzes( min_ave_seq_logproba=min_ave_seq_logproba, n_epoch=n_epoch, result_dir=args.result_dir, - logger=log_string, ) sum_logits += new_c_quizzes.size(0) * ave_seq_logproba @@ -487,7 +485,8 @@ for n_epoch in range(args.nb_epochs): a = [(model.id, float(model.main_test_accuracy)) for model in models] a.sort(key=lambda p: p[0]) - log_string(f"current accuracies {a}") + s = " ".join([f"{p[1]*100:.02f}%" for p in a]) + log_string(f"current accuracies {s}") # select the model with lowest accuracy models.sort(key=lambda model: model.main_test_accuracy) diff --git a/quizz_machine.py b/quizz_machine.py index bf36d0b..49e7835 100755 --- a/quizz_machine.py +++ b/quizz_machine.py @@ -161,8 +161,8 @@ class QuizzMachine: nb_train_samples, nb_test_samples, batch_size, - result_dir=None, - logger=None, + result_dir, + logger, device=torch.device("cpu"), ): super().__init__() @@ -170,6 +170,7 @@ class QuizzMachine: self.problem = problem self.batch_size = batch_size self.device = device + self.logger = logger self.train_w_quizzes = self.problem.generate_token_sequences( nb_train_samples @@ -231,9 +232,9 @@ class QuizzMachine: return self.nb_codes def produce_results( - self, n_epoch, model, result_dir, logger, deterministic_synthesis, nmax=1000 + self, n_epoch, model, result_dir, deterministic_synthesis, nmax=1000 ): - def compute_accuracy(input, logger=None): + def compute_accuracy(input): input = input[:nmax] ar_mask = self.make_ar_mask(input) result = input.clone() * (1 - ar_mask) @@ -260,18 +261,18 @@ class QuizzMachine: train_nb_total, train_nb_correct = compute_accuracy(self.train_w_quizzes) - logger( + self.logger( f"accuracy_train {n_epoch} nb_total {train_nb_total} nb_correct {train_nb_correct} accuracy {(100.0*train_nb_correct)/train_nb_total:.02f}%" ) - test_nb_total, test_nb_correct = compute_accuracy(self.test_w_quizzes, logger) + test_nb_total, test_nb_correct = compute_accuracy(self.test_w_quizzes) - logger( + self.logger( f"accuracy_test {n_epoch} nb_total {test_nb_total} nb_correct {test_nb_correct} accuracy {(100.0*test_nb_correct)/test_nb_total:.02f}%" ) main_test_accuracy = test_nb_correct / test_nb_total - logger(f"main_test_accuracy {n_epoch} {main_test_accuracy}") + self.logger(f"main_test_accuracy {n_epoch} {main_test_accuracy}") ############################## @@ -418,7 +419,7 @@ class QuizzMachine: else: break - logger(f"changing temperature to {temperature}") + self.logger(f"changing temperature to {temperature}") return c_quizzes, seq_logproba.mean() @@ -432,7 +433,6 @@ class QuizzMachine: min_ave_seq_logproba, n_epoch, result_dir, - logger, ): c_quizzes, ave_seq_logproba = self.generate_quizzes( nb, model_for_generation, min_ave_seq_logproba @@ -453,7 +453,6 @@ class QuizzMachine: min_ave_seq_logproba, n_epoch, result_dir, - logger, ): model_for_generation = Gang(models, nb_models_for_generation, mode) models_for_validation = models @@ -464,5 +463,4 @@ class QuizzMachine: min_ave_seq_logproba, n_epoch, result_dir, - logger, ) diff --git a/sky.py b/sky.py index e93c88a..abcd394 100755 --- a/sky.py +++ b/sky.py @@ -44,12 +44,21 @@ class Sky(problem.Problem): "_" + "".join([chr(ord("A") + n) for n in range(len(colors) - 1)]) + "><" ) - def __init__(self, height=6, width=8, nb_birds=3, speed=1, nb_iterations=4): + def __init__( + self, + height=6, + width=8, + nb_birds=3, + speed=2, + nb_iterations=2, + avoid_collision=True, + ): self.height = height self.width = width self.nb_birds = nb_birds self.speed = speed self.nb_iterations = nb_iterations + self.avoid_collision = avoid_collision def direction_tokens(self): return self.token_forward, self.token_backward @@ -58,10 +67,6 @@ class Sky(problem.Problem): frame_sequences = [] for _ in tqdm.tqdm(range(nb), dynamic_ncols=True, desc="world generation"): - result = torch.zeros( - self.nb_iterations, self.height, self.width, dtype=torch.int64 - ) - i, j, vi, vj = ( torch.empty(self.nb_birds, dtype=torch.int64), torch.empty(self.nb_birds, dtype=torch.int64), @@ -69,49 +74,77 @@ class Sky(problem.Problem): torch.empty(self.nb_birds, dtype=torch.int64), ) + def collision_okay(): + if not self.avoid_collision: + return True + + count = torch.zeros(self.height, self.width, dtype=torch.int64) + + for n in range(self.nb_birds): + count[i[n], j[n]] += 1 + count[i[n] - vi[n], j[n]] += 1 + count[i[n], j[n] - vj[n]] += 1 + + return count.max() <= 1 + col = ( torch.randperm(self.colors.size(0) - 1)[: self.nb_birds].sort().values + 1 ) - for n in range(self.nb_birds): + while True: while True: - i[n] = torch.randint(self.height, (1,)) - j[n] = torch.randint(self.width, (1,)) - vm = torch.randint(4, (1,)) - vi[n], vj[n] = (vm % 2) * 2 - 1, (vm // 2) * 2 - 1 - if ( - i[n] - vi[n] >= 0 - and i[n] - vi[n] < self.height - and j[n] - vj[n] >= 0 - and j[n] - vj[n] < self.width - ): + for n in range(self.nb_birds): + while True: + i[n] = torch.randint(self.height, (1,)) + j[n] = torch.randint(self.width, (1,)) + vm = torch.randint(4, (1,)) + vi[n], vj[n] = (vm % 2) * 2 - 1, (vm // 2) * 2 - 1 + if ( + i[n] - vi[n] >= 0 + and i[n] - vi[n] < self.height + and j[n] - vj[n] >= 0 + and j[n] - vj[n] < self.width + ): + break + + if collision_okay(): break - for l in range(self.nb_iterations): - for n in range(self.nb_birds): - c = col[n] - result[l, i[n], j[n]] = c - result[l, i[n] - vi[n], j[n]] = c - result[l, i[n], j[n] - vj[n]] = c + result = torch.zeros( + self.nb_iterations, self.height, self.width, dtype=torch.int64 + ) + + for l in range(self.nb_iterations): + fine = collision_okay() + for n in range(self.nb_birds): + c = col[n] + result[l, i[n], j[n]] = c + result[l, i[n] - vi[n], j[n]] = c + result[l, i[n], j[n] - vj[n]] = c - if (i[n] == 0 and vi[n] == -1) or ( - i[n] == self.height - 1 and vi[n] == 1 - ): - vi[n] = -vi[n] + if (i[n] == 0 and vi[n] == -1) or ( + i[n] == self.height - 1 and vi[n] == 1 + ): + vi[n] = -vi[n] - if (j[n] == 0 and vj[n] == -1) or ( - j[n] == self.width - 1 and vj[n] == 1 - ): - vj[n] = -vj[n] + if (j[n] == 0 and vj[n] == -1) or ( + j[n] == self.width - 1 and vj[n] == 1 + ): + vj[n] = -vj[n] - i[n] += vi[n] - j[n] += vj[n] + i[n] += vi[n] + j[n] += vj[n] + + if fine: + break frame_sequences.append(result) return frame_sequences + ###################################################################### + def generate_token_sequences(self, nb): frame_sequences = self.generate_frame_sequences(nb) @@ -256,12 +289,12 @@ class Sky(problem.Problem): if __name__ == "__main__": import time - sky = Sky(height=6, width=8, speed=1, nb_iterations=4) + sky = Sky(height=6, width=8, speed=2, nb_iterations=2) start_time = time.perf_counter() - seq = sky.generate_frame_sequences(nb=64) + token_sequences = sky.generate_token_sequences(nb=64) delay = time.perf_counter() - start_time - print(f"{seq.size(0)/delay:02f} seq/s") + print(f"{token_sequences.size(0)/delay:02f} seq/s") # print(sky.seq2str(seq[:4])) @@ -278,9 +311,9 @@ if __name__ == "__main__": # m = (torch.rand(seq.size()) < 0.05).long() # seq = (1 - m) * seq + m * 23 - print(seq.size()) - img = sky.seq2img(seq) - print(img.size()) + # print(seq.size()) + img = sky.seq2img(token_sequences) + # print(img.size()) torchvision.utils.save_image( img.float() / 255.0, "/tmp/world.png", nrow=6, padding=6, pad_value=0