ResNet-50 & Dog Breed Classification

Pre-trained models are really awesome. They do all the heavy lifting and saves ones’ time, effort, blood, sweat, tears and GPU, etc. They are trained on much larger datasets, and are constantly being improved by the organization or the developers’ community.

Dog Breed Classification Using ResNet-50

Let’s use a pre-trained model, the ResNet-50 to classify dog breeds, and see how it does.

Note: I have saved five random pictures of dogs in a folder called data, in the project directory. I have named the pictures as dog1.jpeg, dog2.jpeg….dog5.jpeg.

Import Libraries & Model

mport numpy as np
from keras.applications import resnet50
from keras.preprocessing import image

Choose Images, Define Variables & Model

images_array = ["data/dog1.jpeg", "data/dog2.jpeg", "data/dog3.jpeg", "data/dog4.jpeg", "data/dog5.jpg"]

image_width = 224
image_length = 224

model = resnet50.ResNet50()

Supply Model With Data To Classify

def run_resnet_model(img_path):
img = image.load_img(path=img_path, target_size=(image_width, image_length))
X = image.img_to_array(img)
X = np.expand_dims(X, axis=0)
X = resnet50.preprocess_input(X)
X_Pred = model.predict(X)
display_prediction(resnet50.decode_predictions(X_Pred, top=1))

Display Prediction(s)

def display_prediction(pred_class):
for imagenet_id, name, likelihood in pred_class[0]:
print(" - {}: {:2f} likelihood".format(name, likelihood))

Run The Model

for img in images_array:

Model Output

Standard_schnauzer: 0.549144 likelihood
Weimaraner: 0.998188 likelihood
Shetland_sheepdog: 0.921892 likelihood
Affenpinscher: 0.839659 likelihood
American_Staffordshire_terrier: 0.763416 likelihood

So as you can see, our model did pretty good, and all of the classifications were spot on. Hope this little example helps you with your Deep Learning quest.

Please do follow me and my publication page for more simplified deep learning blogs.…….. Happy Learning!

Source: Deep Learning on Medium