<jupyter_start><jupyter_text>Run in Google Colab        View source on GitHub  This code is part of [Chapter 2 - What’s in the Picture: Image Classification with Keras](https://learning.oreilly.com/library/view/practical-deep-learning/9781492034858/ch02.html).<jupyter_code>try:
  import google.colab
  IS_COLAB_ENV = True
except:
  IS_COLAB_ENV = False
IS_COLAB_ENV
if IS_COLAB_ENV:
    !pip install tensorflow==2.0.0<jupyter_output><empty_output><jupyter_text>Predict Category of an ImageThe image classification task simply answers the question “Does the image contain X” where X can be a cat, dog or any other category/class of objects. In this file, we see the code for predicting categories for an image. We start off by importing all the necessary modules from Keras and Python packages. Numpy here gives us the ability to transform matrices, while Matplotlib is graph plotting library that also allows us to display images.<jupyter_code>import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Once we have imported our libraries, we will load and display our test image in a 224x224 resolution.<jupyter_code>IMG_PATH = '../../sample-images/cat.jpg'
if IS_COLAB_ENV:
  !curl https://raw.githubusercontent.com/PracticalDL/Practical-Deep-Learning-Book/master/sample-images/cat.jpg --output cat.jpg
  IMG_PATH = 'cat.jpg'
img = image.load_img(IMG_PATH, target_size=(224, 224))
plt.imshow(img)
plt.show()
model = tf.keras.applications.resnet50.ResNet50()<jupyter_output><empty_output><jupyter_text>Our model has previously been trained only to accept a batch of images. `expand_dims` adds an extra dimension to the array, thereby creating an array of arrays (or a batch of one image).<jupyter_code>def classify(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    model = tf.keras.applications.resnet50.ResNet50()
    img_array = image.img_to_array(img)
    img_batch = np.expand_dims(img_array, axis=0)
    img_preprocessed = preprocess_input(img_batch)
    prediction = model.predict(img_preprocessed)
    print(decode_predictions(prediction, top=3)[0])
classify(IMG_PATH)<jupyter_output>[(u'n02123045', u'tabby', 0.5681726), (u'n02124075', u'Egyptian_cat', 0.22223175), (u'n02123159', u'tiger_cat', 0.061033875)]
{'fraction_non_alphanumeric': 0.0922887612797375, 'fraction_numerical': 0.041427399507793276, 'mean_word_length': 5.663934426229508, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 2, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '466620', 'license': 'MIT', 'n_tokens_mistral': 866, 'n_tokens_neox': 789, 'n_words': 227, 'path': 'code/chapter-2/1-predict-class.ipynb', 'repo_name': 'owillner/Practical-Deep-Learning-Book', 'size': '2438'}