summaryrefslogtreecommitdiff
path: root/text_recognizer/models
diff options
context:
space:
mode:
authorGustaf Rydholm <gustaf.rydholm@gmail.com>2022-06-15 00:17:07 +0200
committerGustaf Rydholm <gustaf.rydholm@gmail.com>2022-06-15 00:17:07 +0200
commitf616bb5e67361ac17b8a153a45863d02b788c861 (patch)
tree82b8deb5612c139b9e84ba7599e59aa03e66d65f /text_recognizer/models
parent2c9066b685d41ef0ab5ea94e938b8a30b4123656 (diff)
Calculate cer and acc in val step
Diffstat (limited to 'text_recognizer/models')
-rw-r--r--text_recognizer/models/transformer.py11
1 files changed, 6 insertions, 5 deletions
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.