How to interpret expand_dims while predicting a uploaded image in tensorflow

Viewed 1571

These days I am getting familiar with CNN & tensorflow (via cat/dog classification problem) and while writing the predict function, I realize that the code uses expand_dims, which I am not able to interpret [1].

When I am able to get the desired (150, 150, 3) array of uploaded image, why cant I feed it to the predict function? the images I trained/validated were also of the same size (150, 150, 3). What exactly adding this additional (1, 150, 150, 3) helping???

Many thanks in advance,

[1]

import numpy as np

from google.colab import files
from keras.preprocessing import image

uploaded=files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path='/content/' + fn
  img=image.load_img(path, target_size=(150, 150))
  
  x=image.img_to_array(img)
  x=np.expand_dims(x, axis=0)
  images = np.vstack([x])
  
  classes = model.predict(images, batch_size=10)
  
  print(classes[0])
  
  if classes[0]>0.5:
    print(fn + " is a dog")
    
  else:
    print(fn + " is a cat")
2 Answers

The extra dimension at the beginning of images are generally for batch sizes. Batch size represent how many images you bundle to feed the CNN at once. The batch can also be a single image, however it still needs that extra dimension of 1 to show the size of batch. If for example batch size is 5, your input array would be of shape (5, 150, 150, 3)

The function adds a dimension to a numpy array. You load the image and get an array of shape (150, 150, 3), after calling expand_dims in 0th axis, you get shape (1, 150, 150, 3).

Related