Your Task - Classifying Images
Machine learning is used in image processing in a wide range of scenarios – e.g. passport control, facial recognition in security scenarios, handwriting recognition, robot vision and driverless vehicles.
An analysis of the principles of machine learning that are applied to image processing can reveal more general machine learning principles that can be applied in other scenarios – voice recognition, for example.
Turning Pictures Into Numbers.
Let’s start by exploring how images can be represented digitally, because once we have numbers in an array we can perform machine learning methods on them.
Open and run 5.5 Classifying Images/Visual Experiment/Vizexp.py
Figure 63. Image, code, data, and colour map
The resulting plot is a colour map of the data contained in a PNG file.
Note the following line in the code –
M = misc.imread('Singlecolour.png', flatten=True)
By changing ('Singlecolour.png'...) we can instruct the algorithm to work with other data files - in this case, PNG files.
Note that when we run the program with it reading the file ‘Singlecolour.png’, it shows an array comprising of just one number (112.4...), and the distribution map of that colour is flat and even.
Now Change 'Singlecolour.png' to 'B&W.png' and Run the code again.
When we change the file to B&W.png we can see that the array now has two colours – 0, Black, and 255, white. The amount of memory that the file takes is bigger, partly because it has more information than the Singlecolour.png file.
Figure 64. More features in the colour map due to more colours present in the image file
Finally, when we look at RGBpic.png we see a full range of numbers, a much wider distribution, and a much bigger file size.
Figure 65. Again, more features in the colour map due to more data in the image file
Looking at them all together –
Figure 66. Each image file and their corresponding colour map
Turning Number Into Pictures.
So we can convert a picture to numbers in an array, but we can also create a picture from numbers in an array.
Open 5.5 Classifying Images/Pic to and From Data/Generate image from array.py
Figure 67. Turning numbers into images
Here, we’ve reduced the colour palate from 255 colours to just 4. Here, black is 0 and white is 3, and we have two shades of grey between.
Run 1. Generate_image_from_array.py
Figure 68. Output from number to image algorithm
Your task
Now experiment with the numbers in the array.
'Round-Trip" Image Data.
Now let’s bring picture into an array, then turn it back into a picture. The purpose of this is just to illustrate how we can process a full cycle of image processing.
Open and run 5.5 Classifying Images/Pic to and From Data/2. Image_to_array_to_csv_and_back.py
The process works like this:
First the program reads the file, in this case ‘Face.png’
Next, the array is exported as a .txt file
Then this external text file is read by the program, and a new array is created, from which a picture can be plotted.
Figure 69. Image to data and back to image
Let’s look at how the code works -
Figure 70. Image 'round-trip' code analysis
Handwriting Recognition.
So now we know how images can be processed, lets now explore a practical application – handwriting recognition. We will do this by classifying images.
In the following exercise, we are going explore how we can compare arrays to find similarities in order to recognise an image.
Will use 10 image files, each with a handwritten digit between 0 and 9.
We will then bring in another digit and see if the algorithm can find the correct match.
Note that this is an extremely crude version of image recognition, but it shows some basic techniques.
In the first instance, we are looking for a handwritten digit ‘8’.
Classifying Images
First look in the folder - 5.5 Classifying Images/Digit Recognition/
Note that 0.png - 9.png is the training data.
10.png, 11.png, and 12.png are test data.
Click on 10.png to Open it using Image Viewer.