Example #1
0
    gradients = T.grad(cost, wrt=params)

    gradient_acc = [theano.shared(0 * p.get_value()) for p in params]
    counter = theano.shared(np.float32(0.))
    acc = theano.function(inputs=[X, Y],
                          outputs=cost,
                          updates=[(a, a + g)
                                   for a, g in zip(gradient_acc, gradients)] +
                          [(counter, counter + np.float32(1.))])
    update = theano.function(
            inputs=[],outputs=[],
            updates = updates.momentum(params,[ g / counter for g in gradient_acc ]) \
                    + [ (a, np.float32(0) * a) for a in gradient_acc ] \
                    + [ (counter,np.float32(0.)) ]
        )

    test = theano.function(inputs=[X, Y], outputs=probs[:, Y])

    training_examples = [word.strip() for word in open('dictionary.txt')]
    import random
    for _ in xrange(1500):
        random.shuffle(training_examples)
        for i, string in enumerate(training_examples):
            print acc(font.imagify(string), label_seq(string))
            if i % 20 == 0: update()
            if i % 100 == 0:
                hinton.plot(test(font.imagify("test"), label_seq("test")).T,
                            max_arr=1.)
                hinton.plot(font.imagify("test").T[::-1].astype('float32'))
        P.save('model.pkl')
Example #2
0
import theano.tensor as T
import numpy as np
from theano_toolkit import utils as U
from theano_toolkit import hinton
from theano_toolkit import updates
from theano_toolkit.parameters import Parameters

import ctc
import font
import lstm
from ocr import *

if __name__ == "__main__":
    import sys
    test_word = sys.argv[1]

    P = Parameters()
    X = T.matrix('X')

    predict = build_model(P, 8, 512, len(font.chars) + 1)
    probs = predict(X)
    test = theano.function(inputs=[X], outputs=probs)
    P.load('model.pkl')
    image = font.imagify(test_word)
    hinton.plot(image.astype(np.float32).T[::-1])
    y_seq = label_seq(test_word)
    probs = test(image)
    print " ", ' '.join(font.chars[i] if i < len(font.chars) else "_"
                        for i in np.argmax(probs, axis=1))
    hinton.plot(probs[:, y_seq].T, max_arr=1.)
Example #3
0
import theano.tensor as T
import numpy as np
from theano_toolkit import utils as U
from theano_toolkit import hinton
from theano_toolkit import updates
from theano_toolkit.parameters import Parameters

import ctc
import font
import lstm
from ocr import *

if __name__ == "__main__":
    import sys
    test_word = sys.argv[1]

    P = Parameters()
    X = T.matrix('X')

    predict = build_model(P,8,512,len(font.chars)+1)
    probs = predict(X)
    test = theano.function(inputs=[X],outputs=probs)
    P.load('model.pkl')
    image = font.imagify(test_word)
    hinton.plot(image.astype(np.float32).T[::-1])
    y_seq = label_seq(test_word)
    probs = test(image)
    print " ", ' '.join(font.chars[i] if i < len(font.chars) else "_" for i in np.argmax(probs,axis=1))
    hinton.plot(probs[:,y_seq].T,max_arr=1.)

Example #4
0
    acc = theano.function(
            inputs=[X, Y],
            outputs=cost,
            updates = [
                (a,a + g) for a,g in zip(gradient_acc,gradients)
            ] + [(counter,counter + np.float32(1.))]
        )
    update = theano.function(
            inputs=[],outputs=[],
            updates = updates.momentum(params,[ g / counter for g in gradient_acc ]) \
                    + [ (a, np.float32(0) * a) for a in gradient_acc ] \
                    + [ (counter,np.float32(0.)) ]
        )

    test = theano.function(
            inputs=[X,Y],
            outputs=probs[:,Y]
        )

    training_examples = [ word.strip() for word in open('dictionary.txt') ]
    import random
    for _ in xrange(1500):
        random.shuffle(training_examples)
        for i,string in enumerate(training_examples):
            print acc(font.imagify(string),label_seq(string))
            if i % 20 == 0: update()
            if i % 100 == 0:
                hinton.plot(test(font.imagify("test"),label_seq("test")).T,max_arr=1.)
                hinton.plot(font.imagify("test").T[::-1].astype('float32'))
        P.save('model.pkl')
Example #5
0
	predict = T.nnet.softmax(T.dot(hidden,W_hidden_output) + b_output)

	return X,predict

def label_seq(string):
	idxs = font.indexify(string)
	result = np.ones((len(idxs)*2 + 1,),dtype=np.int32) * -1
	result[np.arange(len(idxs))*2+1] = idxs
	print result
	return result


if __name__ == "__main__":
	P = Parameters()
	X = T.matrix('X')
	Y = T.ivector('Y')
	X,predict = build_model(P,X,8,256,len(font.chars)+1)

	cost = ctc.cost(predict,Y)
	params = P.values()
	grad = T.grad(cost,wrt=params)
	train = theano.function(
			inputs=[X,Y],
			outputs=cost,
			updates=updates.adadelta(params,grad)
		)

	string = "Shawn"

	print train(font.imagify(string),label_seq(string))