3/18/25, 10:01 AM DL exp3 Manasi.
ipynb - Colab
DEEP LEARNING Practical 3, Name : Manasi Sadashiv Choudhari, Roll no. : 57
Aim : Feeding the data to a pretrained neural network and making the predictions.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.preprocessing import image
VGG16
from tensorflow.keras.applications import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictions
# Load the VGG16 model with ImageNet weights
model = VGG16(weights='imagenet', include_top=True)
# Load and preprocess an image
img_path = '/content/cat3.jpg'
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)
# Predict the class
predictions = model.predict(img_array)
# Decode predictions to readable labels
from tensorflow.keras.applications.vgg16 import decode_predictions
decoded_predictions = decode_predictions(predictions, top=5)[0]
for label, description, probability in decoded_predictions:
print(f"{description}: {probability:.4f}")
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 816ms/step
Persian_cat: 0.9731
lynx: 0.0104
Angora: 0.0076
hamper: 0.0014
plastic_bag: 0.0008
ResNet50
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
# Load the ResNet50 model with ImageNet weights
model = ResNet50(weights='imagenet', include_top=True)
# Load and preprocess an image
img_path = '/content/cat3.jpg'
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)
# Predict the class
predictions = model.predict(img_array)
# Decode predictions to readable labels
decoded_predictions = decode_predictions(predictions, top=5)[0]
for label, description, probability in decoded_predictions:
print(f"{description}: {probability:.4f}")
1/1 ━━━━━━━━━━━━━━━━━━━━ 2s 2s/step
Persian_cat: 0.9918
Angora: 0.0029
lynx: 0.0015
Arctic_fox: 0.0005
tub: 0.0003
MobileNet
https://colab.research.google.com/drive/1J0oyKUke6CMZYdSihmikxTXYqiM6ka_y#scrollTo=OwnGJhqGWYKU&printMode=true 1/2
3/18/25, 10:01 AM DL exp3 Manasi.ipynb - Colab
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions
# Load the MobileNet model with ImageNet weights
model = MobileNet(weights='imagenet', include_top=True)
# Load and preprocess an image
img_path = '/content/cat3.jpg'
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)
# Predict the class
predictions = model.predict(img_array)
# Decode predictions to readable labels
decoded_predictions = decode_predictions(predictions, top=5)[0]
for label, description, probability in decoded_predictions:
print(f"{description}: {probability:.4f}")
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 1s/step
Persian_cat: 0.9919
lynx: 0.0013
Japanese_spaniel: 0.0011
Angora: 0.0006
washbasin: 0.0005
https://colab.research.google.com/drive/1J0oyKUke6CMZYdSihmikxTXYqiM6ka_y#scrollTo=OwnGJhqGWYKU&printMode=true 2/2