Views
No views yet
EfficientNetB0 for robust feature extraction and is fine-tuned for this binary classification task.cats_vs_dogs from tensorflow_datasets0.1.EfficientNetB0 backbone (pre-trained on ImageNet) and custom classification layers:EfficientNetB0 (without top layers, include_top=False)(150, 150, 3) (images are resized to this dimension)GlobalAveragePooling2D(): Reduces spatial dimensions of feature maps.Dense(units=100, activation="relu"): A fully connected layer with ReLU activation.Dense(units=1, activation="sigmoid"): Output layer for binary classification, producing a probability between 0 and 1. precision recall f1-score support
0 0.99 0.99 0.99 2273
1 0.99 0.99 0.99 2380
accuracy 0.99 4653
macro avg 0.99 0.99 0.99 4653
weighted avg 0.99 0.99 0.99 4653huggingface_hub library:1import tensorflow as tf
2from PIL import Image
3from huggingface_hub import hf_hub_download
4import numpy as np
5
6# Download the model from Hugging Face Hub
7model_path = hf_hub_download(
8 repo_id="sirunchained/cats-vs-dogs",
9 filename="cats-vs-dogs.keras"
10)
11
12# Load the model
13model = tf.keras.models.load_model(model_path)
14
15classes = ["Cat", "Dog"]
16IMG_SIZE = 150
17
18def predict_image(image_path):
19 img = Image.open(image_path).resize((IMG_SIZE, IMG_SIZE))
20 img_array = np.array(img)
21 img_array = tf.expand_dims(img_array, axis=0)
22 img_array = tf.cast(img_array, tf.float32)
23
24 predictions = model.predict(img_array)
25 predicted_confidence = predictions[0][0] # Since it's a binary classifier with sigmoid output
26
27 if predicted_confidence >= 0.5:
28 predicted_class = "Dog"
29 confidence = predicted_confidence
30 else:
31 predicted_class = "Cat"
32 confidence = 1 - predicted_confidence
33
34 return predicted_class, confidence
35
36# Example usage:
37# predicted_class, confidence = predict_image("path/to/your/image.jpg")
38# print(f"Predicted: {predicted_class} with confidence: {confidence:.2f}")