Prepare Images For CNN

How are images looked at by computers?

  • Matrices

  • Number in matrix represents darkness of image in that position (darker part → larger #)

How are coloured images looked at by computers?

  • Matrices, but with extra dimension (RGB)

    • Stack of 3 matrices

Reshaping Numpy Arrays

# images is a list of floats/integers that represent an image
images = np.array(images, dtype = 'float')  
  • We have a numpy array (images) of these dimensions: (x, )

    • Need 4D array for input shape whose product is xx

    • So we can take x\sqrt{x} and reshape as:

np.reshape(-1, sqrt(x), sqrt(x), 1) # 1 if the image is greyscaled

Plotting Images as Numpy Arrays

import matplotlib.pyplot as plt
plt.imshow(images_reshaped[0].reshape(sqrt(x),sqrt(x)),cmap='gray')
plt.show()

Creating Labels with Numpy Arrays

# Isolate dataframe to only have the labels
y = []
for i in range(len(df)):
  y.append(np.array([labels_df.iloc[i,:]], dtype='float'))

Last updated