Teaching Computers to Learn•Interactive notebook lesson•Free
dog cat prediction
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
🐶🐱 Dog vs Cat Prediction Notebook
Download an image and classify it using a trained VGG16 model.
Codepython
# !pip install tensorflow
Codepython
import urllib.request
import cv2
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.vgg16 import preprocess_inputOutput
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1774115950.554664 12324 port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
I0000 00:00:1774115950.585673 12324 cpu_feature_guard.cc:227] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1774115951.433409 12324 port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
Codepython
# Download image from URL
url = "https://github.com/cookieSensei/AI-tutorials/blob/main/img.jpeg?raw=true"
file_path = "../datasets/some_image.jpg"
urllib.request.urlretrieve(url, file_path)
print("Image downloaded!")Output
Image downloaded!
Codepython
# Load and display image
img = cv2.imread(file_path)
print("Image shape:", img.shape)
image_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
plt.axis("off")
plt.show()Output
Image shape: (318, 318, 3)
Codepython
# Load your trained model
model = load_model("saved_models/vgg16_model.h5")
print("Model loaded!")Output
I0000 00:00:1774115961.571701 12324 gpu_device.cc:2043] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10105 MB memory: -> device: 0, name: NVIDIA GeForce RTX 3060, pci bus id: 0000:01:00.0, compute capability: 8.6
WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
WARNING:absl:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
Model loaded!
Codepython
# Preprocess image for VGG16
IMG_SIZE = (160, 160)
img_resized = cv2.resize(img, IMG_SIZE)
img_array = preprocess_input(img_resized)
img_array = np.expand_dims(img_array, axis=0)
print("Prepared input shape:", img_array.shape)Output
Prepared input shape: (1, 160, 160, 3)
Codepython
# Predict
prediction = model.predict(img_array)[0][0]
print("Raw output:", prediction)
if prediction > 0.5:
print("Prediction: Dog 🐶")
else:
print("Prediction: Cat 🐱")Output
I0000 00:00:1774115965.668773 13249 service.cc:153] XLA service 0x7e5518043520 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
I0000 00:00:1774115965.668788 13249 service.cc:161] StreamExecutor [0]: NVIDIA GeForce RTX 3060, Compute Capability 8.6 (Driver: 13.1.0; Runtime: 12.8.0; Toolkit: 12.5.0; DNN: 9.10.2)
I0000 00:00:1774115965.674176 13249 dump_mlir_util.cc:269] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
I0000 00:00:1774115965.725512 13249 cuda_dnn.cc:461] Loaded cuDNN version 91002
[1m1/1[0m [32m━━━━━━━━━━━━━━━━━━━━[0m[37m[0m [1m2s[0m 2s/step
Raw output: 1.0
Prediction: Dog 🐶
I0000 00:00:1774115968.010155 13249 device_compiler.h:208] Compiled cluster using XLA! This line is logged at most once for the lifetime of the process.
Codepython
Lesson resources
Supporting code and files from this part of the curriculum.
Finished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →