0% found this document useful (0 votes)
13 views2 pages

Keras Image Classification Example

The document outlines the process of using TensorFlow and Keras to load and preprocess an image for classification using two different models, VGG16 and ResNet50. It demonstrates loading an image, preparing it for prediction, and displaying the top predictions from the ResNet50 model. The predictions include labels and confidence scores for the identified objects in the image.

Uploaded by

samadhankanade29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Keras Image Classification Example

The document outlines the process of using TensorFlow and Keras to load and preprocess an image for classification using two different models, VGG16 and ResNet50. It demonstrates loading an image, preparing it for prediction, and displaying the top predictions from the ResNet50 model. The predictions include labels and confidence scores for the identified objects in the image.

Uploaded by

samadhankanade29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In [40]: import numpy as np

import tensorflow as tf

In [41]: from [Link].vgg16 import VGG16, preprocess_input, decode_predictions


from [Link] import image
from [Link].resnet50 import ResNet50, preprocess_input, decode_predictions

In [44]: model = VGG16(weights='imagenet')


img_path = "img [Link]"
img = image.load_img(img_path, target_size=(224, 224))

In [45]: img_array = image.img_to_array(img)


img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)

predictions = [Link](img_array)

1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 367ms/step

In [46]: model = ResNet50(weights='imagenet')

img_path = "img [Link]"


img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)

predictions = [Link](img_array)
decoded_predictions = decode_predictions(predictions, top=3)[0]

for i, (imagenet_id, label, score) in enumerate(decoded_predictions):


print(f"{i+1}: {label} ({score:.2f})")

1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 1s/step


1: zebra (0.98)
2: hartebeest (0.00)
3: cheetah (0.00)

In [47]: from [Link] import display


from PIL import Image

img_path = "img [Link]"


img = [Link](img_path)
display(img)
In [ ]:

You might also like