Solution and implementation for QB5 from Artificial Neural Network (ann).
This notebook demonstrates a simple binary classification model using TensorFlow and Keras.
!pip install tensorflow opencv-python matplotlib -q
Installing and importing necessary libraries for computer vision and deep learning.
import cv2 import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from tensorflow.keras import layers, models
Loading the 'cats_vs_dogs' dataset from TensorFlow Datasets.
import tensorflow_datasets as tfds
dataset, info = tfds.load('cats_vs_dogs', with_info=True, as_supervised=True)
train_data = dataset['train']plt.figure(figsize=(10,5))
for i, (image, label) in enumerate(train_data.take(5)):
plt.subplot(1,5,i+1)
plt.imshow(image)
plt.title("Dog" if label.numpy() == 1 else "Cat")
plt.axis("off")
plt.show()Normalizing and resizing images to prepare them for the neural network.
IMG_SIZE = 128
def preprocess(image, label):
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
image = image / 255.0
return image, label
train_data = train_data.map(preprocess).batch(32).take(200) # small subsetBuilding a Convolutional Neural Network (CNN) for binary classification.
model = models.Sequential([
layers.Conv2D(16, (3,3), activation='relu', input_shape=(128,128,3)),
layers.MaxPooling2D(),
layers.Conv2D(32, (3,3), activation='relu'),
layers.MaxPooling2D(),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(1, activation='sigmoid') # binary classification
])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()Training the model on a subset of the data.
history = model.fit(train_data, epochs=3)
Defining a prediction function and testing it on an image.
def predict_image(img_path):
img = cv2.imread(img_path)
img_resized = cv2.resize(img, (128,128))
img_norm = img_resized / 255.0
img_input = np.expand_dims(img_norm, axis=0)
prediction = model.predict(img_input)[0][0]
label = "Dog" if prediction > 0.5 else "Cat"
return img, labelimg, label = predict_image("image1.jpg")
# Draw a simple bounding box (whole image)
h, w, _ = img.shape
cv2.rectangle(img, (10,10), (w-10, h-10), (0,255,0), 2)
cv2.putText(img, label, (20,40),
cv2.FONT_HERSHEY_SIMPLEX, 1,
(0,255,0), 2)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis('off')img, label = predict_image("image2.jpg")
# Draw a simple bounding box (whole image)
h, w, _ = img.shape
cv2.rectangle(img, (10,10), (w-10, h-10), (0,255,0), 2)
cv2.putText(img, label, (20,40),
cv2.FONT_HERSHEY_SIMPLEX, 1,
(0,255,0), 2)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis('off')