The goal here is to load predict a class for an image of your choice using Xception network, a pre-trained deep learning model by Google. Please re-order the blocks of code below, copy-paste the code blocks in appropriate order in a new Colab notebook, run them, and analyze the outputs.
Inspect the number of paramaters, number of layers, input shape, and number of output classes using the two approaches below:
print(model.summary())
from tensorflow.keras.utils import plot_model
plot_model(model, show_layer_names=True, show_shapes=True)
Upload the image elephant.jpg to Colab Files.
Option 1: Existing random image
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(299, 299))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
Option 2: An image from your dataset
from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(rescale=1./255)
train_ds = datagen.flow_from_directory(
'./face-expression-kaggle/images/train/',
target_size=(299, 299),
shuffle=True,
batch_size=32)
images, labels = train_ds[0] # # Take one batch full of images
import matplotlib.pyplot as plt
plt.imshow(images[0])
plt.title(labels[0])
plt.axis("off")
x = images[0]
plt.imshow(x)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
Check the list of several state-of-the-art deep learning models for computer vision at https://keras.io/api/applications/.
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.xception import preprocess_input, decode_predictions
import numpy as np
model = Xception(weights='imagenet')
print('Predicted:', decode_predictions(preds, top=3)[0])
preds = model.predict(x)
print(preds)
print(preds.shape)