Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Clean up iam_lines dataset from " #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lab5/text_recognizer/datasets/iam_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from text_recognizer.datasets.base import Dataset
from text_recognizer.datasets.emnist import EmnistDataset

import numpy as np


DATA_DIRNAME = pathlib.Path(__file__).parents[2].resolve() / 'data'
PROCESSED_DATA_DIRNAME = DATA_DIRNAME / 'processed' / 'iam_lines'
Expand Down Expand Up @@ -39,16 +41,25 @@ def __init__(self):
self.input_shape = (28, 952)
self.output_shape = (97, self.num_classes)

def cleanup(self, labels):
labels_clean = []
for label in labels:
s = ''.join([self.mapping.get(i, '') for i in label])
t = s.replace('"', '"')
t = t + '_' * (self.output_shape[0] - len(t))
labels_clean.append([self.inverse_mapping[c] for c in t])
return np.array(labels_clean)

def load_or_generate_data(self):
if not PROCESSED_DATA_FILENAME.exists():
PROCESSED_DATA_DIRNAME.mkdir(parents=True, exist_ok=True)
print('Downloading IAM lines...')
urlretrieve(PROCESSED_DATA_URL, PROCESSED_DATA_FILENAME)
with h5py.File(PROCESSED_DATA_FILENAME, 'r') as f:
self.x_train = f['x_train'][:]
self.y_train_int = f['y_train'][:]
self.y_train_int = self.cleanup(f['y_train'][:])
self.x_test = f['x_test'][:]
self.y_test_int = f['y_test'][:]
self.y_test_int = self.cleanup(f['y_test'][:])

@cachedproperty
def y_train(self):
Expand All @@ -72,4 +83,3 @@ def __repr__(self):
dataset = IamLinesDataset()
dataset.load_or_generate_data()
print(dataset)