After setting up the deep learning environment, the first thing is to try if it works. The simplest way is to use a pretrained model to predict new samples. Here, we use a dog breed prediction model trained on Kaggle as an example. (Complete data and scripts are provided later, just download them locally to run.)
1. Load images to be predicted
from os.path import join
image_dir = 'train/'
img_paths = [join(image_dir, filename) for filename in
['0246f44bb123ce3f91c939861eb97fb7.jpg',
'84728e78632c0910a69d33f82e62638c.jpg']]
Here we selected 2 images, saved in the list img_paths
.
2. Define the function to read and preprocess images
import numpy as np
from tensorflow.python.keras.applications.resnet50 import preprocess_input
from tensorflow.python.keras.preprocessing.image import load_img, img_to_array
image_size = 224
def read_and_prep_images(img_paths, img_height=image_size, img_width=image_size): # define processing function
imgs = [load_img(img_path, target_size=(img_height, img_width)) for img_path in img_paths]
img_array = np.array([img_to_array(img) for img in imgs])
return preprocess_input(img_array)
3. Load the model and predict
from tensorflow.python.keras.applications import ResNet50
my_model = ResNet50(weights='../resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
test_data = read_and_prep_images(img_paths)
preds = my_model.predict(test_data)
4. View and visualize prediction results
import sys
# Add directory holding utility functions to path to allow importing
sys.path.append('~/utils')
from decode_predictions import decode_predictions
from IPython.display import Image, display
most_likely_labels = decode_predictions(preds, top=3, class_list_path='../resnet50/imagenet_class_index.json')
for i, img_path in enumerate(img_paths):
display(Image(img_path))
print(most_likely_labels[i])
[('n02097209', 'standard_schnauzer', 0.56502265), ('n02097047', 'miniature_schnauzer', 0.31319875), ('n02097130', 'giant_schnauzer', 0.045194548)]
[('n02092339', 'Weimaraner', 0.99767154), ('n02099849', 'Chesapeake_Bay_retriever', 0.001392837), ('n02109047', 'Great_Dane', 0.00032280287)]
You can see the results have been generated and they are consistent with the Kaggle cloud results. Because the official API is very slow and often crashes during download, and there are many details to handle such as file paths.
To make it easier for beginners to test and run on local servers, I have downloaded the data and packaged my own test scripts on Baidu Netdisk. You can directly download and use them.
Data download link:
Link: https://pan.baidu.com/s/1UqK8mJF97VzKh5abuxkH8g Extraction code: cxkf