After recently completing the Machine Learning course from Stanford University on Coursera I’ve been preparing to give a small introduction to machine learning at work. Part of that is showing some demos of machine learning tools.
I made a character recognition neural network using the PyBrains Python library, it’s a great library and very fast but the documentation is very poor and examples are hard to come across. With enough digging I managed to put together something very simple and short.
In this example it reads in small PNG files of letters, extracts all of the pixel values and creates a 1D array of the values, this is used to train the neural network through back propagation. I test the network on one of the inputs. Each input is classified with a number in the addSample function, this takes the flattened array and a number (unfortunately it does not take a string as a classification). If you run the application you will see that, for example when using b.png as a test, it will return a value close to 2.
You can download the images I used here – Machine Learning Training Characters.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
#!/usr/bin/python from pybrain.datasets.supervised import SupervisedDataSet from pybrain.tools.shortcuts import buildNetwork from pybrain.supervised.trainers import BackpropTrainer import cv2 def loadImage(path): im = cv2.imread(path) return flatten(im) def flatten(x): result = [] for el in x: if hasattr(el, "__iter__") and not isinstance(el, basestring): result.extend(flatten(el)) else: result.append(el) return result if __name__ == "__main__": t = loadImage('a.png') net = buildNetwork(len(t), len(t), 1) ds = SupervisedDataSet(len(t), 1) ds.addSample(loadImage('a.png'),(1,)) ds.addSample(loadImage('b.png'),(2,)) ds.addSample(loadImage('c.png'),(3,)) ds.addSample(loadImage('d.png'),(4,)) trainer = BackpropTrainer(net, ds) error = 10 iteration = 0 while error > 0.001: error = trainer.train() iteration += 1 print "Iteration: {0} Error {1}".format(iteration, error) print "\nResult: ", net.activate(loadImage('b.png')) |
Questions? Comments?