<jupyter_start><jupyter_text>This code example demonstrates how to use a pre-trained residual network to solve an image classification problem, using a picture of a dog. More context for this code example can be found in the section "Programming Example: Use a Pretrained ResNet Implementation" in Chapter 8 in the book Learning Deep Learning by <NAME> (ISBN: 9780137470358).We start with a number of import statements.<jupyter_code>import numpy as np
from tensorflow.keras.applications import resnet50
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.applications.resnet50 import \
    decode_predictions
import matplotlib.pyplot as plt
import tensorflow as tf
import logging
tf.get_logger().setLevel(logging.ERROR)<jupyter_output><empty_output><jupyter_text>In the next code snippet, we then load the image with the function load_img, which will return an image in PIL format. We specified that we want the picture to be scaled to 224×224 pixels because that is what the ResNet-50 implementation expects. We then convert the image into a NumPy tensor to be able to present it to our network. The network expects an array of multiple images, so we add a fourth dimension; consequently, we have an array of images with a single element.<jupyter_code># Load image and convert to 4-dimensional tensor.
image = load_img('../data/dog.jpg', target_size=(224, 224))
image_np = img_to_array(image)
image_np = np.expand_dims(image_np, axis=0)<jupyter_output><empty_output><jupyter_text>The final code snippet shows how to load the ResNet-50 model, using weights that have been trained using the ImageNet dataset. Just as we did in previous examples, we standardize the input images because the ResNet-50 model expects them to be standardized. The function preprocess_input does that for us, using parameters derived from the training dataset that was used to train the model. We present the image to the network by calling model.predict() and then print the predictions after first calling the convenience method decode_predictions(), which retrieves the labels in textual form.<jupyter_code># Load the pretrained model.
model = resnet50.ResNet50(weights='imagenet')
# Standardize input data.
X = resnet50.preprocess_input(image_np.copy())
# Do prediction.
y = model.predict(X)
predicted_labels = decode_predictions(y)
print('predictions = ', predicted_labels)

# Show image.
plt.imshow(np.uint8(image_np[0]))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.06232741617357002, 'fraction_numerical': 0.01814595660749507, 'mean_word_length': 5.74468085106383, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '413229', 'license': 'MIT', 'n_tokens_mistral': 720, 'n_tokens_neox': 678, 'n_words': 307, 'path': 'tf_framework/c8e1_resnet_inference.ipynb', 'repo_name': 'jpgacrama/DeepLearning', 'size': '2541'}