CoolFace
Modelpublic

AIOmarRehan/Animal-Image-Classification-Using-CNN

sourceHugging Facemitupdated 8mo agoView on Hugging Face
2likes
Model Card

Animal Image Classification (TensorFlow & CNN)

"A complete end‑to‑end pipeline for building, cleaning, preprocessing, training, evaluating, and deploying a deep CNN model for multi‑class animal image classification."

This project is designed to be clean, organized, and human-friendly, showing the full machine‑learning workflow, from data validation to model evaluation & ROC curves.


Project Structure

ComponentDescription
Data LoadingReads and extracts the ZIP dataset from Google Drive
EDAClass distribution, file integrity, image sizes, brightness, contrast, samples display
PreprocessingResizing, normalization, augmentation, hashing, cleaning corrupted files
ModelDeep custom CNN with BatchNorm, Dropout & L2 Regularization
TrainingAdam optimizer, LR scheduler, Early stopping
EvaluationConfusion matrix, classification report, ROC curves
ExportSaves final .h5 model

How to Run

1. Upload your dataset to Google Drive

Your dataset must be structured as:

Animals/
 ├── Cats/
 ├── Dogs/
 ├── Snakes/

2. Update the ZIP path

python
zip_path = '/content/drive/MyDrive/Animals.zip'
extract_to = '/content/my_data'

3. Run the Notebook

Once executed, the script will:

  • Mount Google Drive
  • Extract images
  • Build a DataFrame of paths
  • Run EDA checks
  • Clean and prepare images
  • Train the CNN model
  • Export results

Data Preparation & EDA

This project performs deep dataset validation including:

Class Distribution

python
class_count = df['class'].value_counts()
class_count.plot(kind='bar')

Image Size Properties

python
image_df['Channels'].value_counts().plot(kind='bar')

Duplicate Image Detection

Using MD5 hashing:

python
def get_hash(file_path):
    with open(file_path, 'rb') as f:
        return hashlib.md5(f.read()).hexdigest()

Brightness & Contrast Issues

python
stat = ImageStat.Stat(img.convert("L"))
brightness = stat.mean[0]
contrast = stat.stddev[0]

Auto‑fixing poor images

Brightness/contrast enhanced using:

python
img = ImageEnhance.Brightness(img).enhance(1.5)
img = ImageEnhance.Contrast(img).enhance(1.5)

Image Preprocessing

All images are resized to 256×256 and normalized to [0,1].

python
def preprocess_image(path, target_size=(256, 256)):
    img = tf.io.read_file(path)
    img = tf.image.decode_image(img, channels=3)
    img = tf.image.resize(img, target_size)
    return tf.cast(img, tf.float32) / 255.0

Data Augmentation

python
data_augmentation = tf.keras.Sequential([
    tf.keras.layers.RandomFlip("horizontal"),
    tf.keras.layers.RandomRotation(0.1),
    tf.keras.layers.RandomZoom(0.1),
])

CNN Model Architecture

Below is a simplified view of the model:

Conv2D (32) → BatchNorm → Conv2D (32) → BatchNorm → MaxPool → Dropout
Conv2D (64) → BatchNorm → Conv2D (64) → BatchNorm → MaxPool → Dropout
Conv2D (128) → BatchNorm → Conv2D (128) → BatchNorm → MaxPool → Dropout
Conv2D (256) → BatchNorm → Conv2D (256) → BatchNorm → MaxPool → Dropout
Flatten → Dense (softmax)

Example code:

python
model.add(Conv2D(32, (3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D((2,2)))

Training

python
epochs = 50
optimizer = Adam(learning_rate=0.0005)
model.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Callbacks

CallbackPurpose
ReduceLROnPlateauAuto‑reduce LR when val_loss stagnates
EarlyStoppingStop training when no improvement

Model Evaluation

Accuracy

python
test_loss, test_accuracy = model.evaluate(test_ds)

Classification Report

python
print(classification_report(y_true, y_pred, target_names=le.classes_))

Confusion Matrix

python
sns.heatmap(cm, annot=True, cmap='Blues')

ROC Curve (One-vs-Rest)

python
fpr, tpr, _ = roc_curve(y_true_bin[:, i], y_probs[:, i])

Saving the Model

python
model.save("Animal_Classification.h5")

Full Code Organization (High-Level)

StepDescription
1Import libraries, mount drive
2Extract ZIP
3Build DataFrame
4EDA & cleaning
5Preprocessing & augmentation
6Dataset pipeline (train/val/test)
7CNN architecture
8Feature Extraction
9Model Fine-Tuning
10Evaluation
11Save model

Final Notes

This README is crafted to feel human, clean, and attractive, not autogenerated. It can be directly used in any GitHub repository.

If you want, I can also:

  • Generate a short version
  • Add badges (TensorFlow, Python, etc.)
  • Write an installation section
  • Turn it into a Hugging Face Space README

Animal Image Classification – Complete Pipeline (README)

"A clean dataset is half the model’s accuracy. The rest is just engineering."

This project presents a complete end-to-end deep learning pipeline for multi-class animal image classification using TensorFlow/Keras. It includes everything from data extraction, cleaning, and analysis, to model training, evaluation, and exporting.


Table of Contents

SectionDescription
1. Project OverviewWhat this project does & architecture overview
2. FeaturesKey capabilities of this pipeline
3. Directory StructureRecommended project layout
4. InstallationHow to install and run this project
5. Dataset ProcessingExtraction, cleaning, inspections
6. Exploratory Data AnalysisVisualizations & summary statistics
7. Preprocessing & AugmentationData preparation logic
8. CNN Model ArchitectureLayers, blocks, hyperparameters
9. Feature Extraction & CallbacksHow the model is extracting feature before fine-tuning
10. Model Fine-TuningHow the model is fine-tuned after extracted features
11. Evaluation MetricsReports, ROC curve, confusion matrix
12. Model ExportSaving and downloading the model
13. Code ExamplesImportant snippets explained

1. Project Overview

This project builds a Convolutional Neural Network (CNN) to classify images of animals into multiple categories. The process includes:

  • Dataset extraction from Google Drive
  • Data validation (duplicates, corrupt files, mislabeled images)
  • Image enhancement & cleaning
  • Class distribution analysis
  • Image size analysis and outlier detection
  • Data augmentation
  • CNN model training with regularization
  • Performance evaluation using multiple metrics
  • Model export to .h5

The pipeline is designed to be robust, explainable, and production-friendly.


2. Features

FeatureDescription
Automatic Dataset ExtractionUnzips and loads images from Google Drive
Image ValidationDetects duplicates, corrupted images, and mislabeled files
Data CleaningBrightness/contrast enhancements for dark or overexposed samples
EDA VisualizationsClass distribution, size, color modes, outliers
TensorFlow Dataset PipelineEfficient TFRecords-like batching & prefetching
Deep CNN Model32 → 64 → 128 → 256 filters with batch norm and dropout
Model Evaluation DashboardConfusion matrix, ROC curves, precision/recall/F1
Model ExportSaves final model as Animal_Classification.h5

3. Recommended Directory Structure

text
Animal-Classification
 ┣ data
 ┃ ┗ Animals (extracted folders)
 ┣ notebooks
 ┣ src
 ┃ ┣ preprocessing.py
 ┃ ┣ model.py
 ┃ ┗ utils.py
 ┣ README.md
 ┗ requirements.txt

4. Installation

bash
pip install tensorflow pandas matplotlib seaborn scikit-learn pillow tqdm

If using Google Colab, the project already supports:

  • google.colab.drive
  • google.colab.files

5. Dataset Extraction & Loading

Example snippet:

python
zip_path = '/content/drive/MyDrive/Animals.zip'
extract_to = '/content/my_data'
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
    zip_ref.extractall(extract_to)

Images are collected into a DataFrame:

python
paths = [(path.parts[-2], path.name, str(path)) for path in Path(extract_to).rglob('*.*')]
df = pd.DataFrame(paths, columns=['class','image','full_path'])

6. Exploratory Data Analysis

Examples of generated visualizations:

  • Barplot of class distribution
  • Pie chart of percentage per class
  • Scatter plots of image width and height
  • Image mode (RGB/Gray) distribution

Example:

python
plt.figure(figsize=(32,16))
class_count.plot(kind='bar')

7. Preprocessing & Augmentation

Preprocessing function

python
def preprocess_image(path, target_size=(256,256)):
    img = tf.io.read_file(path)
    img = tf.image.decode_image(img, channels=3)
    img = tf.image.resize(img, target_size)
    return tf.cast(img, tf.float32)/255.0

Augmentation

python
data_augmentation = tf.keras.Sequential([
    tf.keras.layers.RandomFlip("horizontal"),
    tf.keras.layers.RandomRotation(0.1),
    tf.keras.layers.RandomZoom(0.1),
])

8. CNN Model Architecture

BlockLayers
Block 1Conv2D(32) → BN → Conv2D(32) → BN → MaxPool → Dropout(0.2)
Block 2Conv2D(64) → BN → Conv2D(64) → BN → MaxPool → Dropout(0.3)
Block 3Conv2D(128) → BN → Conv2D(128) → BN → MaxPool → Dropout(0.4)
Block 4Conv2D(256) → BN → Conv2D(256) → BN → MaxPool → Dropout(0.5)
OutputFlatten → Dense(num_classes, softmax)

Example snippet:

python
model.add(Conv2D(64,(3,3),activation='relu',padding='same'))
model.add(BatchNormalization())

9. Training

python
optimizer = Adam(learning_rate=0.0005)
model.compile(optimizer=optimizer,
              loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Using callbacks:

python
ReduceLROnPlateau(...)
EarlyStopping(...)

10. Evaluation Metrics

This project computes:

  • Precision, Recall, F1 (macro & per class)
  • Confusion matrix (heatmap)
  • ROC curves (one-vs-rest)
  • Macro-average ROC

Example:

python
cm = confusion_matrix(y_true, y_pred)
sns.heatmap(cm, annot=True)

11. Model Export

python
model.save("Animal_Classification.h5")
files.download("Animal_Classification.h5")

12. Example Snippets

Checking corrupted files

python
try:
    with Image.open(path) as img:
        img.verify()
except:
    corrupted.append(path)

Filtering duplicate images

python
df['file_hash'] = df['full_path'].apply(get_hash)
df_unique = df.drop_duplicates(subset='file_hash')