From 7e8e54e84c63171e748bbf09516fd517e6821ace Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Sat, 20 Mar 2021 18:09:06 +0100 Subject: Inital commit for refactoring to lightning --- src/text_recognizer/tests/__init__.py | 1 - src/text_recognizer/tests/support/__init__.py | 2 - .../support/create_emnist_lines_support_files.py | 51 --------------------- .../tests/support/create_emnist_support_files.py | 30 ------------ .../support/create_iam_lines_support_files.py | 50 -------------------- src/text_recognizer/tests/support/emnist/8.png | Bin 498 -> 0 bytes src/text_recognizer/tests/support/emnist/U.png | Bin 524 -> 0 bytes src/text_recognizer/tests/support/emnist/e.png | Bin 563 -> 0 bytes .../tests/support/emnist_lines/Knox Ky.png | Bin 2301 -> 0 bytes .../emnist_lines/ancillary beliefs and.png | Bin 5424 -> 0 bytes .../tests/support/emnist_lines/they.png | Bin 1391 -> 0 bytes .../He rose from his breakfast-nook bench.png | Bin 5170 -> 0 bytes .../and came into the livingroom, where.png | Bin 3617 -> 0 bytes .../his entrance. He came, almost falling.png | Bin 3923 -> 0 bytes .../tests/support/iam_paragraphs/a01-000u.jpg | Bin 14890 -> 0 bytes .../tests/test_character_predictor.py | 31 ------------- src/text_recognizer/tests/test_line_predictor.py | 35 -------------- .../tests/test_paragraph_text_recognizer.py | 37 --------------- 18 files changed, 237 deletions(-) delete mode 100644 src/text_recognizer/tests/__init__.py delete mode 100644 src/text_recognizer/tests/support/__init__.py delete mode 100644 src/text_recognizer/tests/support/create_emnist_lines_support_files.py delete mode 100644 src/text_recognizer/tests/support/create_emnist_support_files.py delete mode 100644 src/text_recognizer/tests/support/create_iam_lines_support_files.py delete mode 100644 src/text_recognizer/tests/support/emnist/8.png delete mode 100644 src/text_recognizer/tests/support/emnist/U.png delete mode 100644 src/text_recognizer/tests/support/emnist/e.png delete mode 100644 src/text_recognizer/tests/support/emnist_lines/Knox Ky.png delete mode 100644 src/text_recognizer/tests/support/emnist_lines/ancillary beliefs and.png delete mode 100644 src/text_recognizer/tests/support/emnist_lines/they.png delete mode 100644 src/text_recognizer/tests/support/iam_lines/He rose from his breakfast-nook bench.png delete mode 100644 src/text_recognizer/tests/support/iam_lines/and came into the livingroom, where.png delete mode 100644 src/text_recognizer/tests/support/iam_lines/his entrance. He came, almost falling.png delete mode 100644 src/text_recognizer/tests/support/iam_paragraphs/a01-000u.jpg delete mode 100644 src/text_recognizer/tests/test_character_predictor.py delete mode 100644 src/text_recognizer/tests/test_line_predictor.py delete mode 100644 src/text_recognizer/tests/test_paragraph_text_recognizer.py (limited to 'src/text_recognizer/tests') diff --git a/src/text_recognizer/tests/__init__.py b/src/text_recognizer/tests/__init__.py deleted file mode 100644 index 18ff212..0000000 --- a/src/text_recognizer/tests/__init__.py +++ /dev/null @@ -1 +0,0 @@ -"""Test modules for the text text recognizer.""" diff --git a/src/text_recognizer/tests/support/__init__.py b/src/text_recognizer/tests/support/__init__.py deleted file mode 100644 index a265ede..0000000 --- a/src/text_recognizer/tests/support/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -"""Support file modules.""" -from .create_emnist_support_files import create_emnist_support_files diff --git a/src/text_recognizer/tests/support/create_emnist_lines_support_files.py b/src/text_recognizer/tests/support/create_emnist_lines_support_files.py deleted file mode 100644 index 9abe143..0000000 --- a/src/text_recognizer/tests/support/create_emnist_lines_support_files.py +++ /dev/null @@ -1,51 +0,0 @@ -"""Module for creating EMNIST Lines test support files.""" -# flake8: noqa: S106 - -from pathlib import Path -import shutil - -import numpy as np - -from text_recognizer.datasets import EmnistLinesDataset -import text_recognizer.util as util - - -SUPPORT_DIRNAME = Path(__file__).parents[0].resolve() / "emnist_lines" - - -def create_emnist_lines_support_files() -> None: - """Create EMNIST Lines test images.""" - shutil.rmtree(SUPPORT_DIRNAME, ignore_errors=True) - SUPPORT_DIRNAME.mkdir() - - # TODO: maybe have to add args to dataset. - dataset = EmnistLinesDataset( - init_token="", - pad_token="_", - eos_token="", - transform=[{"type": "ToTensor", "args": {}}], - target_transform=[ - { - "type": "AddTokens", - "args": {"init_token": "", "pad_token": "_", "eos_token": ""}, - } - ], - ) # nosec: S106 - dataset.load_or_generate_data() - - for index in [5, 7, 9]: - image, target = dataset[index] - if len(image.shape) == 3: - image = image.squeeze(0) - print(image.sum(), image.dtype) - - label = "".join(dataset.mapper(label) for label in target[1:]).strip( - dataset.mapper.pad_token - ) - print(label) - image = image.numpy() - util.write_image(image, str(SUPPORT_DIRNAME / f"{label}.png")) - - -if __name__ == "__main__": - create_emnist_lines_support_files() diff --git a/src/text_recognizer/tests/support/create_emnist_support_files.py b/src/text_recognizer/tests/support/create_emnist_support_files.py deleted file mode 100644 index f9ff030..0000000 --- a/src/text_recognizer/tests/support/create_emnist_support_files.py +++ /dev/null @@ -1,30 +0,0 @@ -"""Module for creating EMNIST test support files.""" -from pathlib import Path -import shutil - -from text_recognizer.datasets import EmnistDataset -from text_recognizer.util import write_image - -SUPPORT_DIRNAME = Path(__file__).parents[0].resolve() / "emnist" - - -def create_emnist_support_files() -> None: - """Create support images for test of CharacterPredictor class.""" - shutil.rmtree(SUPPORT_DIRNAME, ignore_errors=True) - SUPPORT_DIRNAME.mkdir() - - dataset = EmnistDataset(train=False) - dataset.load_or_generate_data() - - for index in [5, 7, 9]: - image, label = dataset[index] - if len(image.shape) == 3: - image = image.squeeze(0) - image = image.numpy() - label = dataset.mapper(int(label)) - print(index, label) - write_image(image, str(SUPPORT_DIRNAME / f"{label}.png")) - - -if __name__ == "__main__": - create_emnist_support_files() diff --git a/src/text_recognizer/tests/support/create_iam_lines_support_files.py b/src/text_recognizer/tests/support/create_iam_lines_support_files.py deleted file mode 100644 index 50f9e3d..0000000 --- a/src/text_recognizer/tests/support/create_iam_lines_support_files.py +++ /dev/null @@ -1,50 +0,0 @@ -"""Module for creating IAM Lines test support files.""" -# flake8: noqa -from pathlib import Path -import shutil - -import numpy as np - -from text_recognizer.datasets import IamLinesDataset -import text_recognizer.util as util - - -SUPPORT_DIRNAME = Path(__file__).parents[0].resolve() / "iam_lines" - - -def create_emnist_lines_support_files() -> None: - """Create IAM Lines test images.""" - shutil.rmtree(SUPPORT_DIRNAME, ignore_errors=True) - SUPPORT_DIRNAME.mkdir() - - # TODO: maybe have to add args to dataset. - dataset = IamLinesDataset( - init_token="", - pad_token="_", - eos_token="", - transform=[{"type": "ToTensor", "args": {}}], - target_transform=[ - { - "type": "AddTokens", - "args": {"init_token": "", "pad_token": "_", "eos_token": ""}, - } - ], - ) - dataset.load_or_generate_data() - - for index in [0, 1, 3]: - image, target = dataset[index] - if len(image.shape) == 3: - image = image.squeeze(0) - print(image.sum(), image.dtype) - - label = "".join(dataset.mapper(label) for label in target[1:]).strip( - dataset.mapper.pad_token - ) - print(label) - image = image.numpy() - util.write_image(image, str(SUPPORT_DIRNAME / f"{label}.png")) - - -if __name__ == "__main__": - create_emnist_lines_support_files() diff --git a/src/text_recognizer/tests/support/emnist/8.png b/src/text_recognizer/tests/support/emnist/8.png deleted file mode 100644 index faa29aa..0000000 Binary files a/src/text_recognizer/tests/support/emnist/8.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/emnist/U.png b/src/text_recognizer/tests/support/emnist/U.png deleted file mode 100644 index 304eaec..0000000 Binary files a/src/text_recognizer/tests/support/emnist/U.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/emnist/e.png b/src/text_recognizer/tests/support/emnist/e.png deleted file mode 100644 index a03ecd4..0000000 Binary files a/src/text_recognizer/tests/support/emnist/e.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/emnist_lines/Knox Ky.png b/src/text_recognizer/tests/support/emnist_lines/Knox Ky.png deleted file mode 100644 index b7d0618..0000000 Binary files a/src/text_recognizer/tests/support/emnist_lines/Knox Ky.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/emnist_lines/ancillary beliefs and.png b/src/text_recognizer/tests/support/emnist_lines/ancillary beliefs and.png deleted file mode 100644 index 14a8cf3..0000000 Binary files a/src/text_recognizer/tests/support/emnist_lines/ancillary beliefs and.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/emnist_lines/they.png b/src/text_recognizer/tests/support/emnist_lines/they.png deleted file mode 100644 index 7f05951..0000000 Binary files a/src/text_recognizer/tests/support/emnist_lines/they.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/iam_lines/He rose from his breakfast-nook bench.png b/src/text_recognizer/tests/support/iam_lines/He rose from his breakfast-nook bench.png deleted file mode 100644 index 6eeb642..0000000 Binary files a/src/text_recognizer/tests/support/iam_lines/He rose from his breakfast-nook bench.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/iam_lines/and came into the livingroom, where.png b/src/text_recognizer/tests/support/iam_lines/and came into the livingroom, where.png deleted file mode 100644 index 4974cf8..0000000 Binary files a/src/text_recognizer/tests/support/iam_lines/and came into the livingroom, where.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/iam_lines/his entrance. He came, almost falling.png b/src/text_recognizer/tests/support/iam_lines/his entrance. He came, almost falling.png deleted file mode 100644 index a731245..0000000 Binary files a/src/text_recognizer/tests/support/iam_lines/his entrance. He came, almost falling.png and /dev/null differ diff --git a/src/text_recognizer/tests/support/iam_paragraphs/a01-000u.jpg b/src/text_recognizer/tests/support/iam_paragraphs/a01-000u.jpg deleted file mode 100644 index d9753b6..0000000 Binary files a/src/text_recognizer/tests/support/iam_paragraphs/a01-000u.jpg and /dev/null differ diff --git a/src/text_recognizer/tests/test_character_predictor.py b/src/text_recognizer/tests/test_character_predictor.py deleted file mode 100644 index 01bda78..0000000 --- a/src/text_recognizer/tests/test_character_predictor.py +++ /dev/null @@ -1,31 +0,0 @@ -"""Test for CharacterPredictor class.""" -import importlib -import os -from pathlib import Path -import unittest - -from loguru import logger - -from text_recognizer.character_predictor import CharacterPredictor -from text_recognizer.networks import MLP - -SUPPORT_DIRNAME = Path(__file__).parents[0].resolve() / "support" / "emnist" - -os.environ["CUDA_VISIBLE_DEVICES"] = "" - - -class TestCharacterPredictor(unittest.TestCase): - """Tests for the CharacterPredictor class.""" - - def test_filename(self) -> None: - """Test that CharacterPredictor correctly predicts on a single image, for serveral test images.""" - network_fn_ = MLP - predictor = CharacterPredictor(network_fn=network_fn_) - - for filename in SUPPORT_DIRNAME.glob("*.png"): - pred, conf = predictor.predict(str(filename)) - logger.info( - f"Prediction: {pred} at confidence: {conf} for image with character {filename.stem}" - ) - self.assertEqual(pred, filename.stem) - self.assertGreater(conf, 0.7) diff --git a/src/text_recognizer/tests/test_line_predictor.py b/src/text_recognizer/tests/test_line_predictor.py deleted file mode 100644 index eede4d4..0000000 --- a/src/text_recognizer/tests/test_line_predictor.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Tests for LinePredictor.""" -import os -from pathlib import Path -import unittest - - -import editdistance -import numpy as np - -from text_recognizer.datasets import IamLinesDataset -from text_recognizer.line_predictor import LinePredictor -import text_recognizer.util as util - -SUPPORT_DIRNAME = Path(__file__).parents[0].resolve() / "support" - -os.environ["CUDA_VISIBLE_DEVICES"] = "" - - -class TestEmnistLinePredictor(unittest.TestCase): - """Test LinePredictor class on the EmnistLines dataset.""" - - def test_filename(self) -> None: - """Test that LinePredictor correctly predicts on single images, for several test images.""" - predictor = LinePredictor( - dataset="EmnistLineDataset", network_fn="CNNTransformer" - ) - - for filename in (SUPPORT_DIRNAME / "emnist_lines").glob("*.png"): - pred, conf = predictor.predict(str(filename)) - true = str(filename.stem) - edit_distance = editdistance.eval(pred, true) / len(pred) - print( - f'Pred: "{pred}" | Confidence: {conf} | True: {true} | Edit distance: {edit_distance}' - ) - self.assertLess(edit_distance, 0.2) diff --git a/src/text_recognizer/tests/test_paragraph_text_recognizer.py b/src/text_recognizer/tests/test_paragraph_text_recognizer.py deleted file mode 100644 index 3e280b9..0000000 --- a/src/text_recognizer/tests/test_paragraph_text_recognizer.py +++ /dev/null @@ -1,37 +0,0 @@ -"""Test for ParagraphTextRecognizer class.""" -import os -from pathlib import Path -import unittest - -from text_recognizer.paragraph_text_recognizer import ParagraphTextRecognizor -import text_recognizer.util as util - - -SUPPORT_DIRNAME = Path(__file__).parents[0].resolve() / "support" / "iam_paragraph" - -# Prevent using GPU. -os.environ["CUDA_VISIBLE_DEVICES"] = "" - - -class TestParagraphTextRecognizor(unittest.TestCase): - """Test that it can take non-square images of max dimension larger than 256px.""" - - def test_filename(self) -> None: - """Test model on support image.""" - line_predictor_args = { - "dataset": "EmnistLineDataset", - "network_fn": "CNNTransformer", - } - line_detector_args = {"dataset": "EmnistLineDataset", "network_fn": "UNet"} - model = ParagraphTextRecognizor( - line_predictor_args=line_predictor_args, - line_detector_args=line_detector_args, - ) - num_text_lines_by_name = {"a01-000u-cropped": 7} - for filename in (SUPPORT_DIRNAME).glob("*.jpg"): - full_image = util.read_image(str(filename), grayscale=True) - predicted_text, line_region_crops = model.predict(full_image) - print(predicted_text) - self.assertTrue( - len(line_region_crops), num_text_lines_by_name[filename.stem] - ) -- cgit v1.2.3-70-g09d2