import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
# first download the image
# You can download and load any image that you want! Just change the url and file name
!wget https://gfp-2a3tnpzj.stackpathdns.com/wp-content/uploads/2016/07/Dachshund-600x600.jpg
--2020-11-08 04:46:07-- https://gfp-2a3tnpzj.stackpathdns.com/wp-content/uploads/2016/07/Dachshund-600x600.jpg Resolving gfp-2a3tnpzj.stackpathdns.com (gfp-2a3tnpzj.stackpathdns.com)... 151.139.245.23 Connecting to gfp-2a3tnpzj.stackpathdns.com (gfp-2a3tnpzj.stackpathdns.com)|151.139.245.23|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 39238 (38K) [image/jpeg] Saving to: ‘Dachshund-600x600.jpg’ Dachshund-600x600.j 100%[===================>] 38.32K --.-KB/s in 0s 2020-11-08 04:46:07 (368 MB/s) - ‘Dachshund-600x600.jpg’ saved [39238/39238]
img = load_img('Dachshund-600x600.jpg')
img
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications import NASNetLarge
model = ResNet50(weights='imagenet')
img = load_img('Dachshund-600x600.jpg', target_size = (224, 224)) # image size can be calibrated with target_size parameter
img
img = img_to_array(img)
print(img.shape)
(224, 224, 3)
img = np.expand_dims(img, axis=0)
print(img.shape)
(1, 224, 224, 3)
## prediction wo preprocessing
pred_class = model.predict(img)
pred_class.shape
(1, 1000)
Without pre-processing the image, the model predicts the dachshund image as a Chesapeake Bay retriever image. Though both are dogs having brown furs, this doesn't seem very right.
# print only top 10 predicted classes
n = 10
topn_predictions = decode_predictions(pred_class, top=n)
for c in topn_predictions[0]:
print(c)
('n02099849', 'Chesapeake_Bay_retriever', 0.50923026) ('n02099712', 'Labrador_retriever', 0.19167913) ('n02088364', 'beagle', 0.054074917) ('n02105412', 'kelpie', 0.03438173) ('n02087394', 'Rhodesian_ridgeback', 0.02017579) ('n02090379', 'redbone', 0.01838291) ('n04409515', 'tennis_ball', 0.0147442585) ('n02100236', 'German_short-haired_pointer', 0.012236389) ('n02107142', 'Doberman', 0.009778115) ('n02107908', 'Appenzeller', 0.008025773)
img = preprocess_input(img) # preprocess image with preprocess_input function
print(img.shape)
(1, 224, 224, 3)
After pre-processing, the model predicts a rottweiler image. It is much closer, though the two dog breeds differ greatly in terms of size
## prediction with preprocessing
pred_class = model.predict(img)
# print(pred_class)
n = 10
top_n = decode_predictions(pred_class, top=n)
for c in top_n[0]:
print(c)
('n02106550', 'Rottweiler', 0.71815056) ('n02107142', 'Doberman', 0.10249863) ('n02107312', 'miniature_pinscher', 0.05253944) ('n02107908', 'Appenzeller', 0.036325805) ('n02101006', 'Gordon_setter', 0.031185042) ('n02112706', 'Brabancon_griffon', 0.013586109) ('n02089078', 'black-and-tan_coonhound', 0.012167945) ('n02108000', 'EntleBucher', 0.0036388754) ('n02099712', 'Labrador_retriever', 0.002448782) ('n02093754', 'Border_terrier', 0.00216503)