text stringlengths 0 4.99k |
|---|
assert len(words_list) == len(train_samples) + len(validation_samples) + len( |
test_samples |
) |
print(f\"Total training samples: {len(train_samples)}\") |
print(f\"Total validation samples: {len(validation_samples)}\") |
print(f\"Total test samples: {len(test_samples)}\") |
Total training samples: 86810 |
Total validation samples: 4823 |
Total test samples: 4823 |
Data input pipeline |
We start building our data input pipeline by first preparing the image paths. |
base_image_path = os.path.join(base_path, \"words\") |
def get_image_paths_and_labels(samples): |
paths = [] |
corrected_samples = [] |
for (i, file_line) in enumerate(samples): |
line_split = file_line.strip() |
line_split = line_split.split(\" \") |
# Each line split will have this format for the corresponding image: |
# part1/part1-part2/part1-part2-part3.png |
image_name = line_split[0] |
partI = image_name.split(\"-\")[0] |
partII = image_name.split(\"-\")[1] |
img_path = os.path.join( |
base_image_path, partI, partI + \"-\" + partII, image_name + \".png\" |
) |
if os.path.getsize(img_path): |
paths.append(img_path) |
corrected_samples.append(file_line.split(\"\n\")[0]) |
return paths, corrected_samples |
train_img_paths, train_labels = get_image_paths_and_labels(train_samples) |
validation_img_paths, validation_labels = get_image_paths_and_labels(validation_samples) |
test_img_paths, test_labels = get_image_paths_and_labels(test_samples) |
Then we prepare the ground-truth labels. |
# Find maximum length and the size of the vocabulary in the training data. |
train_labels_cleaned = [] |
characters = set() |
max_len = 0 |
for label in train_labels: |
label = label.split(\" \")[-1].strip() |
for char in label: |
characters.add(char) |
max_len = max(max_len, len(label)) |
train_labels_cleaned.append(label) |
print(\"Maximum length: \", max_len) |
print(\"Vocab size: \", len(characters)) |
# Check some label samples. |
train_labels_cleaned[:10] |
Maximum length: 21 |
Vocab size: 78 |
['sure', |
'he', |
'during', |
'of', |
'booty', |
'gastronomy', |
'boy', |
'The', |
'and', |
'in'] |
Now we clean the validation and the test labels as well. |
def clean_labels(labels): |
cleaned_labels = [] |
for label in labels: |
label = label.split(\" \")[-1].strip() |
cleaned_labels.append(label) |
return cleaned_labels |
validation_labels_cleaned = clean_labels(validation_labels) |
test_labels_cleaned = clean_labels(test_labels) |
Building the character vocabulary |
Keras provides different preprocessing layers to deal with different modalities of data. This guide provids a comprehensive introduction. Our example involves preprocessing labels at the character level. This means that if there are two labels, e.g. \"cat\" and \"dog\", then our character vocabulary should be {a, c, d,... |
AUTOTUNE = tf.data.AUTOTUNE |
# Mapping characters to integers. |
char_to_num = StringLookup(vocabulary=list(characters), mask_token=None) |
# Mapping integers back to original characters. |
num_to_char = StringLookup( |
vocabulary=char_to_num.get_vocabulary(), mask_token=None, invert=True |
) |
Resizing images without distortion |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.