sirunchained commited on
Commit
704a477
·
verified ·
1 Parent(s): 33bc5b9

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +122 -3
README.md CHANGED
@@ -1,3 +1,122 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pretty_name: Cats vs Dogs Classifier
3
+ tags:
4
+ - image-classification
5
+ - tensorflow
6
+ - keras
7
+ - efficientnet
8
+ - gradio
9
+ widget:
10
+ - src: >-
11
+ https://raw.githubusercontent.com/huggingface/datasets-server/main/assets/image-classification/train/image.jpg
12
+ example_title: Dog example
13
+ - src: >-
14
+ https://raw.githubusercontent.com/huggingface/datasets-server/main/assets/image-classification/test/image.jpg
15
+ example_title: Cat example
16
+ license: mit
17
+ metrics:
18
+ - accuracy
19
+ base_model:
20
+ - google/efficientnet-b0
21
+ library_name: keras
22
+ ---
23
+
24
+ # Cats vs Dogs Classifier with EfficientNetB0
25
+
26
+ This model is a deep learning classifier capable of distinguishing between images of cats and dogs. It leverages the power of `EfficientNetB0` for robust feature extraction and is fine-tuned for this binary classification task.
27
+
28
+ ## Dataset
29
+
30
+ - **Name**: `cats_vs_dogs` from `tensorflow_datasets`
31
+ - **Description**: Contains 23,262 images in total, split into approximately equal numbers of cat and dog images.
32
+ - Cats are labeled as `0`.
33
+ - Dogs are labeled as `1`.
34
+
35
+ ## Model Architecture
36
+
37
+ The model is built using a pre-trained `EfficientNetB0` backbone (pre-trained on ImageNet) and custom classification layers:
38
+
39
+ - **Base Model**: `EfficientNetB0` (without top layers, `include_top=False`)
40
+ - **Input Shape**: `(150, 150, 3)` (images are resized to this dimension)
41
+ - **Custom Layers**:
42
+ - `GlobalAveragePooling2D()`: Reduces spatial dimensions of feature maps.
43
+ - `Dense(units=100, activation="relu")`: A fully connected layer with ReLU activation.
44
+ - `Dense(units=1, activation="sigmoid")`: Output layer for binary classification, producing a probability between 0 and 1.
45
+
46
+ ## Training Details
47
+
48
+ - **Epochs**: 7 (with EarlyStopping)
49
+ - **Batch Size**: 32
50
+ - **Optimizer**: Adam with a learning rate of 5e-5
51
+ - **Loss Function**: Binary Crossentropy
52
+ - **Callbacks**: EarlyStopping (patience=3, monitor='val_loss'), ModelCheckpoint (saves best model based on 'val_loss')
53
+
54
+ ## Evaluation Metrics
55
+
56
+ The model was evaluated on a test set of 4653 images. Here are the key metrics:
57
+
58
+ - **Test Loss**: 0.0334
59
+ - **Test Accuracy**: 0.9884
60
+
61
+ **Classification Report**:
62
+ ```
63
+ precision recall f1-score support
64
+
65
+ 0 0.99 0.99 0.99 2273
66
+ 1 0.99 0.99 0.99 2380
67
+
68
+ accuracy 0.99 4653
69
+ macro avg 0.99 0.99 0.99 4653
70
+ weighted avg 0.99 0.99 0.99 4653
71
+ ```
72
+
73
+ ## How to Use
74
+
75
+ You can use this model for image classification. Here's a Python snippet using TensorFlow and the `huggingface_hub` library:
76
+
77
+ ```python
78
+ import tensorflow as tf
79
+ from PIL import Image
80
+ from huggingface_hub import hf_hub_download
81
+ import numpy as np
82
+
83
+ # Download the model from Hugging Face Hub
84
+ model_path = hf_hub_download(
85
+ repo_id="sirunchained/cats-vs-dogs",
86
+ filename="cats-vs-dogs.keras"
87
+ )
88
+
89
+ # Load the model
90
+ model = tf.keras.models.load_model(model_path)
91
+
92
+ classes = ["Cat", "Dog"]
93
+ IMG_SIZE = 150
94
+
95
+ def predict_image(image_path):
96
+ img = Image.open(image_path).resize((IMG_SIZE, IMG_SIZE))
97
+ img_array = np.array(img)
98
+ img_array = tf.expand_dims(img_array, axis=0)
99
+ img_array = tf.cast(img_array, tf.float32)
100
+
101
+ predictions = model.predict(img_array)
102
+ predicted_confidence = predictions[0][0] # Since it's a binary classifier with sigmoid output
103
+
104
+ if predicted_confidence >= 0.5:
105
+ predicted_class = "Dog"
106
+ confidence = predicted_confidence
107
+ else:
108
+ predicted_class = "Cat"
109
+ confidence = 1 - predicted_confidence
110
+
111
+ return predicted_class, confidence
112
+
113
+ # Example usage:
114
+ # predicted_class, confidence = predict_image("path/to/your/image.jpg")
115
+ # print(f"Predicted: {predicted_class} with confidence: {confidence:.2f}")
116
+ ```
117
+
118
+ ## Gradio Demo
119
+
120
+ You can interact with a live demo of this model through Gradio:
121
+
122
+ [Gradio Demo Link](https://huggingface.co/spaces/sirunchained/cats-vs-dogs-gradio)