Converting images to matrices

Much of machine learning is just operations on matrices. We will start that process next by converting our images into a series of matrices—essentially, a 3D matrix as wide as the number of images we have.

Almost all matrix operations that we will perform in this chapter, and the entire book, use NumPy—the most popular scientific computing package in the Python landscape. NumPy is available at http://www.numpy.org/. You should install it before running the next series of operations.

The following code opens images and creates the matrices of data (note the three extra imports now required):

 import numpy as np 
 from IPython.display import display, Image 
 from scipy import ndimage 
 
 image_size = 28  # Pixel width and height. 
 pixel_depth = 255.0  # Number of levels per pixel. 
 def loadClass(folder): 
  image_files = os.listdir(folder) 
  dataset = np.ndarray(shape=(len(image_files), 
image_size,
image_size), dtype=np.float32) image_index = 0 print(folder) for image in os.listdir(folder): image_file = os.path.join(folder, image) try: image_data =
(ndimage.imread(image_file).astype(float) - pixel_depth / 2) / pixel_depth if image_data.shape != (image_size, image_size): raise Exception('Unexpected image shape: %s' %
str(image_data.shape)) dataset[image_index, :, :] = image_data image_index += 1 except IOError as e: l print('Could not read:', image_file, ':', e, '-
it's ok,
skipping.') return dataset[0:image_index, :, :]

We have our extracted files from the previous section. Now, we can simply run this procedure on all our extracted images, as follows:

 classFolders = [os.path.join(tst_files, d) for d in 
os.listdir(tst_files) if os.path.isdir(os.path.join(tst_files,
d))] print (classFolders) for cf in classFolders: print (" Examing class folder " + cf) dataset=loadClass(cf) print (dataset.shape)

The procedure essentially loads letters into a matrix that looks something like this:

However, a peek into the matrix reveals more subtlety. Go ahead and take a look by printing an arbitrary layer on the stack (for example, np.set_printoptions(precision=2); print (dataset[47]). You will find a matrix not of bits, but of floating point numbers:

The images first get loaded into a matrix of values 0 to 255:

These get scaled down to numbers between -0.5 and 0.5, we will revisit the reasons why later. We will end up with a stack of images that looks like this:

These are all greyscale images, so we will deal with just one layer. We'll deal with color images in future chapters; in those cases, each photo will have a matrix of height three and a separate matrix for red, green, and blue.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.229.111