From f616bb5e67361ac17b8a153a45863d02b788c861 Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Wed, 15 Jun 2022 00:17:07 +0200 Subject: Calculate cer and acc in val step --- text_recognizer/models/transformer.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'text_recognizer/models') diff --git a/text_recognizer/models/transformer.py b/text_recognizer/models/transformer.py index b511947..7afe9bd 100644 --- a/text_recognizer/models/transformer.py +++ b/text_recognizer/models/transformer.py @@ -54,11 +54,11 @@ class LitTransformer(LitBase): def validation_step(self, batch: Tuple[Tensor, Tensor], batch_idx: int) -> None: """Validation step.""" data, targets = batch - - # Compute the loss. - logits = self.network(data, targets[:, :-1]) - loss = self.loss_fn(logits, targets[:, 1:]) - self.log("val/loss", loss, prog_bar=True) + preds = self.predict(data) + self.val_acc(preds, targets) + self.log("val/acc", self.val_acc, on_step=False, on_epoch=True) + self.val_cer(preds, targets) + self.log("val/cer", self.val_cer, on_step=False, on_epoch=True, prog_bar=True) def test_step(self, batch: Tuple[Tensor, Tensor], batch_idx: int) -> None: """Test step.""" @@ -71,6 +71,7 @@ class LitTransformer(LitBase): self.test_acc(pred, targets) self.log("test/acc", self.test_acc, on_step=False, on_epoch=True) + @torch.no_grad() def predict(self, x: Tensor) -> Tensor: """Predicts text in image. -- cgit v1.2.3-70-g09d2