Course 4/ Week 4/ Programming Assignment: Face recognition

Why do we need to apply : np.expand_dims on our image before entering x_train aw input parameter into model.predict_on_batch ? (5.1 part of the exercise)

def img_to_encoding(image_path, model):
img = tf.keras.preprocessing.image.load_img(image_path, target_size=(160, 160))
img = np.around(np.array(img) / 255.0, decimals=12)
x_train = np.expand_dims(img, axis=0)
embedding = model.predict_on_batch(x_train)

That logic just adds the “samples” dimension, because the model is defined to take input data in the form of 4D arrays: m x h x w x c. If you print the shapes before and after, you’ll see that

img.shape = (160, 160, 3)
x_train.shape = (1, 160, 160, 3)
1 Like