Welcome back to the third installment of the OCR series.

In the last post I showed you how to encode the images from a set of training images into training data for the OCR Neural Network we are building.

Here are the previous posts in this series:

Lets teach AI how to read using PHP

Lets teach AI how to read using PHP Part 2

Once the images are encoded as numbers representing the pixel color we are now ready to teach our ANN how to identify symbols in images.

train_ocr.php



<?php

set_time_limit ( 300 ); // max run time 5 minutes (adjust as needed)

$num_input = 160;
$num_output = 1;
$num_layers = 3;
$num_neurons_hidden = 107;
$desired_error = 0.00001;
$max_epochs = 5000000;
$epochs_between_reports = 10;

$ann = fann_create_standard($num_layers, $num_input, $num_neurons_hidden, $num_output);

if ($ann) {
	echo 'Training OCR... '; 
	fann_set_activation_function_hidden($ann, FANN_SIGMOID_SYMMETRIC);
	fann_set_activation_function_output($ann, FANN_SIGMOID_SYMMETRIC);

	$filename = dirname(__FILE__) . "/ocr.data";
	if (fann_train_on_file($ann, $filename, $max_epochs, $epochs_between_reports, $desired_error))
		fann_save($ann, dirname(__FILE__) . "/ocr_float.net");

	fann_destroy($ann);
}

echo 'All Done! Now run <a href="test_ocr.php">Test OCR</a><br>' . PHP_EOL;



If you run this code the following things will happen:
  1.  A standard fully connected 3 layer backward propagating neural network will be created with 160 inputs, and 1 output.
  2. The ANN will be configured to use the Sigmoid activation function.
  3. The ANN is trained, saved and dumped from memory.

Now that we have trained the ANN all that is left is to test it, which I will cover in my next post.

If you would like to obtain a copy of this code from GitHub or fork this project to follow along as I release the code you can find this project here: OCR on GitHub

Note: This project (all the code, the title images as well as the infographic licensed under everyone’s favorite license (MIT LICENSE) so feel free to take this code and develop it into something amazing! Please just attribute me as the author of the initial code base. Also if you use this to create something cool, I’d love to hear about it! 🙂

As always I hope you found this project both interesting and informative. Please Like, Comment & Share this post with your friends and followers on your social media platforms and don’t forget to click the follow button over on the top right of this page to get notified when I post something new.

Also please support me on Patreon.

If would like to suggest a topic or project for an upcoming post feel free to contact me.

Much Love,
~Joy

Advertisements