<gh_stars>0
from data import preproc as pp
import os
import datetime
import string
from data.generator import DataGenerator
from network.model import HTRModel
from keras.models import load_model

from data import preproc as pp
# create and compile HTRModel
# note: `learning_rate=None` will get architecture default value
weights_path = '/home/tmt/Documents/teko/handwritten-text-recognition/output/bentham/flor/checkpoint_weights.hdf5'

# define parameters
source = "bentham"
arch = "flor"
epochs = 5
batch_size = 16

# define paths
source_path = os.path.join("..", "data", f"{source}.hdf5")
output_path = os.path.join("..", "output", source, arch)
target_path = os.path.join(output_path, "checkpoint_weights.hdf5")
os.makedirs(output_path, exist_ok=True)

# define input size, number max of chars per line and list of valid chars
input_size = (1024, 128, 1)
max_text_length = 128
charset_base = string.printable[:95]

print("source:", source_path)
print("output", output_path)
print("target", target_path)
print("charset:", charset_base)


dtgen = DataGenerator(source=source_path,
                      batch_size=batch_size,
                      charset=charset_base,
                      max_text_length=max_text_length)

print(f"Train images: {dtgen.size['train']}")
print(f"Validation images: {dtgen.size['valid']}")
print(f"Test images: {dtgen.size['test']}")


model = HTRModel(architecture=arch, input_size=input_size,
                 vocab_size=dtgen.tokenizer.vocab_size)
model.load_checkpoint(target=target_path)

start_time = datetime.datetime.now()

# predict() function will return the predicts with the probabilities
predicts, _ = model.predict(x=dtgen.next_test_batch(),
                            steps=dtgen.steps['test'],
                            ctc_decode=True,
                            verbose=1)

# decode to string
predicts = [dtgen.tokenizer.decode(x[0]) for x in predicts]

total_time = datetime.datetime.now() - start_time

# mount predict corpus file
with open(os.path.join(output_path, "predict.txt"), "w") as lg:
    for pd, gt in zip(predicts, dtgen.dataset['test']['gt']):
        lg.write(f"TE_L {gt}\nTE_P {pd}\n")

for i, item in enumerate(dtgen.dataset['test']['dt'][:10]):
    print("=" * 1024, "\n")
    # cv2_imshow(pp.adjust_to_see(item))
    print(dtgen.dataset['test']['gt'][i])
    print(predicts[i], "\n")

from data import preproc as pp
import os
import datetime
import string
from data.generator import DataGenerator
from network.model import HTRModel
from keras.models import load_model

from data import preproc as pp
# create and compile HTRModel
# note: `learning_rate=None` will get architecture default value
weights_path = '/home/tmt/Documents/teko/handwritten-text-recognition/output/bentham/flor/checkpoint_weights.hdf5'

# define parameters
source = "bentham"
arch = "flor"
epochs = 5
batch_size = 16

# define paths
source_path = os.path.join("..", "data", f"{source}.hdf5")
output_path = os.path.join("..", "output", source, arch)
target_path = os.path.join(output_path, "checkpoint_weights.hdf5")
os.makedirs(output_path, exist_ok=True)

# define input size, number max of chars per line and list of valid chars
input_size = (1024, 128, 1)
max_text_length = 128
charset_base = string.printable[:95]

print("source:", source_path)
print("output", output_path)
print("target", target_path)
print("charset:", charset_base)


dtgen = DataGenerator(source=source_path,
                      batch_size=batch_size,
                      charset=charset_base,
                      max_text_length=max_text_length)

print(f"Train images: {dtgen.size['train']}")
print(f"Validation images: {dtgen.size['valid']}")
print(f"Test images: {dtgen.size['test']}")


model = HTRModel(architecture=arch, input_size=input_size,
                 vocab_size=dtgen.tokenizer.vocab_size)
model.load_checkpoint(target=target_path)

start_time = datetime.datetime.now()

# predict() function will return the predicts with the probabilities
predicts, _ = model.predict(x=dtgen.next_test_batch(),
                            steps=dtgen.steps['test'],
                            ctc_decode=True,
                            verbose=1)

# decode to string
predicts = [dtgen.tokenizer.decode(x[0]) for x in predicts]

total_time = datetime.datetime.now() - start_time

# mount predict corpus file
with open(os.path.join(output_path, "predict.txt"), "w") as lg:
    for pd, gt in zip(predicts, dtgen.dataset['test']['gt']):
        lg.write(f"TE_L {gt}\nTE_P {pd}\n")

for i, item in enumerate(dtgen.dataset['test']['dt'][:10]):
    print("=" * 1024, "\n")
    # cv2_imshow(pp.adjust_to_see(item))
    print(dtgen.dataset['test']['gt'][i])
    print(predicts[i], "\n")
# create and compile HTRModel # note: `learning_rate=None` will get architecture default value # define parameters # define paths # define input size, number max of chars per line and list of valid chars # predict() function will return the predicts with the probabilities # decode to string # mount predict corpus file # cv2_imshow(pp.adjust_to_see(item))