Beispiel #1
0
################################ Train
print('Training the Network')
for epoch in range(num_epochs):
    ntwk.update_learning_rate(epoch)
    edit_dist, tot_len = 0, 0

    print('Epoch: {} '.format(epoch))
    # keeping 1 backup file as data might get lost, if script is stopped while pickling
    os.rename(network_fname, 'ntwk.bkp.pkl')
    with open(network_fname, 'wb') as fh:
        pickle.dump(ntwk, fh)
    print('Network saved to {}'.format(network_fname))

    for samp in range(num_samples):
        x, _, y = scriber.get_text_image()
        y_blanked = utils.insert_blanks(y, alphabet_size, num_blanks_at_start=2)
        # if len(y_blanked) < 2:
        #     print(y_blanked, end=' ')
        #     continue
        cst, pred, forward_probs = ntwk.trainer(x, y_blanked)

        if np.isinf(cst):
            printer.show_all(y, x, pred,
                             (forward_probs > 1e-20, 'Forward probabilities:', y_blanked))
            print('Exiting on account of Inf Cost...')
            break

        if samp == 0 and epoch==num_epochs-1:   # or len(y) == 0:
            pred, hidden = ntwk.tester(x)
Beispiel #2
0
    'nchars_per_sample': 30,
    'height': 45
}
alphabet_size = len(telugu.symbols)

scriber = Scribe(language=telugu, **options)

if len(sys.argv) > 1:
    target_dir = sys.argv[1]
    samples = int(sys.argv[2])
else:
    samples = 1000
    target_dir = '../data/train/3/'

print(samples, target_dir)

try:
    os.makedirs(target_dir)
except FileExistsError:
    pass

for i in range(samples):
    image, text, labels = scriber.get_text_image()
    image = bin_arr_to_img(image)

    base_name = '{}/{:04d}'.format(target_dir, i)
    image.save(base_name + '.png')

    with open(base_name + '.gt.txt', 'w') as fh:
        fh.write(''.join(text))
    'nchars_per_sample': 1,
    'height': 45
}
alphabet_size = len(telugu.symbols)

scriber = Scribe(language=telugu, **options)

if len(sys.argv) > 1:
    target_dir = sys.argv[1]
    samples = int(sys.argv[2])
else:
    samples = 100
    target_dir = 'data'

print(samples, target_dir)

try:
    os.makedirs(target_dir)
except FileExistsError:
    pass

for i in range(samples):
    image, text, labels = scriber.get_text_image()
    image = bin_arr_to_img(image)

    base_name = '{}/{:04d}'.format(target_dir, i)
    image.save(base_name + '.png')

    with open(base_name + '.gt.txt', 'w') as fh:
        fh.write(''.join(text))
Beispiel #4
0
ntwk = nn.NeuralNet(scriber.height, alphabet_size, **nnet_args)

# Print
print('\nArguments:')
utils.write_dict(args)
print('FloatX: {}'.format(th.config.floatX))
print('Alphabet Size: {}'.format(alphabet_size))

################################ Train
print('Training the Network')
for epoch in range(num_epochs):
    ntwk.update_learning_rate(epoch)
    edit_dist, tot_len = 0, 0

    for samp in range(num_samples):
        x, _, y = scriber.get_text_image()
        y_blanked = utils.insert_blanks(y, alphabet_size, num_blanks_at_start=2)
        # if len(y_blanked) < 2:
        #     print(y_blanked, end=' ')
        #     continue
        cst, pred, forward_probs = ntwk.trainer(x, y_blanked)

        if np.isinf(cst):
            printer.show_all(y, x, pred,
                             (forward_probs > 1e-20, 'Forward probabilities:', y_blanked))
            print('Exiting on account of Inf Cost...')
            break

        if samp == 0 and epoch==num_epochs-1:   # or len(y) == 0:
            pred, hidden = ntwk.tester(x)