Example #1
0
    def evaluate(self):
        game = self.trainer.game
        game.eval()
        old_loss = game.loss

        for loader_name, loader, metric in self.loaders_metrics:

            acc_or, acc = 0.0, 0.0
            n_batches = 0
            game.loss = metric

            for batch in loader:
                n_batches += 1

                batch = core.move_to(batch, self.device)
                with torch.no_grad():
                    _, rest = game(*batch)
                acc += rest['acc']

                acc_or += rest['acc_or']
            self.results[loader_name] = {
                'acc': acc / n_batches,
                'acc_or': acc_or / n_batches
            }

        self.results['epoch'] = self.epoch
        output_json = json.dumps(self.results)
        print(output_json, flush=True)

        game.loss = old_loss
        game.train()
Example #2
0
    def on_epoch_end(self, loss, logs, epoch):
        dump_dir = pathlib.Path.cwd() / "dump" / str(epoch)
        dump_dir.mkdir(exist_ok=True, parents=True)

        self.trainer.game.eval()

        len_dataset = len(self.eval_dataset)

        for i in range(5):
            example_id = np.random.randint(0, len_dataset)
            example = self.eval_dataset[example_id]

            example = (
                example[0].unsqueeze(0),
                example[1].unsqueeze(0),
                example[2].unsqueeze(0),
            )

            device = torch.device(
                "cuda" if torch.cuda.is_available() else "cpu")

            example = core.move_to(example, device)
            _, interaction = self.trainer.game(*example)

            image = example[0][0]

            output = interaction.receiver_output.view(*self.image_shape)
            image = image.view(*self.image_shape)
            utils.save_image(torch.cat([image, output], dim=1),
                             dump_dir / (str(i) + ".png"))
        self.trainer.game.train()
Example #3
0
    def on_epoch_end(self, loss, logs, epoch):
        dump_dir = pathlib.Path.cwd() / "dump" / str(epoch)
        dump_dir.mkdir(exist_ok=True, parents=True)

        self.trainer.game.eval()

        for i in range(5):
            example = self.eval_dataset[i]
            example = core.move_to(example, self.device)
            _, interaction = self.trainer.game(*example)

            image = example[0][0]

            output = interaction.receiver_output.view(28, 28)
            image = image.view(28, 28)
            utils.save_image(
                torch.cat([image, output], dim=1), dump_dir / (str(i) + ".png")
            )
        self.trainer.game.train()