YAML Metadata
Warning:
empty or missing yaml metadata in repo card
(https://huggingface.co/docs/hub/model-cards#model-card-metadata)
Universal Image Classifier ๐ผ๏ธ๐ค
A flexible, PyTorch-based image classification framework designed for easy adaptation to any image classification task. Perfect for researchers, practitioners, and anyone looking to quickly prototype and deploy image classification models.
๐ Features
- ๐๏ธ Multiple Architectures: Linear, MLP, Deep MLP, and Residual MLP models
- โ๏ธ Flexible Configuration: Easy hyperparameter tuning via config files
- ๐ฎ Universal Inference: Works with any trained model and dataset
- ๐ Production Ready: Optimized for both training and deployment
- ๐ Modern Training: Data augmentation, early stopping, learning rate scheduling
- ๐ค Hugging Face Ready: Easy integration with Hugging Face Hub
- ๐ Comprehensive Evaluation: Built-in metrics, visualization, and model analysis
- ๐ฆ Easy Dataset Loading: Support for folder structure and CSV datasets
๐ Model Architectures
Model | Description | Parameters | Use Case |
---|---|---|---|
LinearClassifier |
Simple linear baseline | ~50K | Quick prototyping, simple datasets |
MLPClassifier |
Single hidden layer MLP | ~100K | Moderate complexity datasets |
MLPClassifierDeep |
Deep MLP with multiple layers | ~500K | Complex pattern recognition |
MLPClassifierDeepResidual |
Deep MLP with residual connections | ~500K | Very deep networks, best performance |
๐ง Installation
# Clone the repository
git clone https://github.com/hightechguys79/universal-image-classifier.git
cd universal-image-classifier
# Install dependencies
pip install -r requirements.txt
๐ฏ Quick Start
1. Training a Model
from train import UniversalTrainer
from config import ModelConfig, TrainingConfig, DataConfig
from utils import load_dataset_from_folder
# Load your dataset
image_paths, labels, class_names = load_dataset_from_folder("path/to/your/data")
# Configure model
model_config = ModelConfig(
num_classes=len(class_names),
hidden_dim=256,
num_layers=6,
dropout_rate=0.1
)
training_config = TrainingConfig(
batch_size=64,
learning_rate=0.001,
num_epochs=100
)
# Train the model
trainer = UniversalTrainer(model_config, training_config, DataConfig())
trainer.train(train_dataset, val_dataset, model_name="mlp_deep_residual")
2. Making Predictions
from inference import load_classifier
# Load your trained model
classifier = load_classifier(
model_path="outputs/mlp_deep_residual_best.pth",
config_path="outputs/mlp_deep_residual_config.json",
class_names=["cat", "dog", "bird", "fish"]
)
# Predict single image
result = classifier.predict("test_image.jpg")
print(f"Predicted: {result['predicted_class']} ({result['confidence']:.3f})")
# Get top-3 predictions
top_3 = classifier.get_top_k_predictions("test_image.jpg", k=3)
for pred in top_3:
print(f"{pred['class_name']}: {pred['probability']:.3f}")
3. Running Examples
# Train a demo model
python examples/train_example.py
# Test inference
python examples/inference_example.py
# Prepare for Hugging Face
python examples/huggingface_integration.py
๐ Project Structure
universal-image-classifier/
โโโ README.md # This file
โโโ requirements.txt # Dependencies
โโโ config.py # Configuration classes
โโโ models.py # Model architectures
โโโ train.py # Training pipeline
โโโ inference.py # Inference pipeline
โโโ utils.py # Utility functions
โโโ examples/ # Example scripts
โ โโโ train_example.py # Training example
โ โโโ inference_example.py # Inference example
โ โโโ huggingface_integration.py # HF Hub integration
โโโ assets/ # Documentation assets
๐จ Supported Dataset Formats
1. Folder Structure
your_dataset/
โโโ class1/
โ โโโ img1.jpg
โ โโโ img2.jpg
โโโ class2/
โโโ img3.jpg
โโโ img4.jpg
2. CSV Format
image_path,label
path/to/img1.jpg,cat
path/to/img2.jpg,dog
โ๏ธ Configuration
Model Configuration
ModelConfig(
input_height=64, # Input image height
input_width=64, # Input image width
num_classes=10, # Number of output classes
hidden_dim=128, # Hidden layer dimension
num_layers=4, # Number of layers
dropout_rate=0.1, # Dropout rate
use_batch_norm=True # Use batch normalization
)
Training Configuration
TrainingConfig(
batch_size=32, # Batch size
learning_rate=0.001, # Learning rate
num_epochs=100, # Maximum epochs
weight_decay=1e-4, # L2 regularization
early_stopping_patience=10, # Early stopping patience
validation_split=0.2 # Validation split ratio
)
๐ฌ Advanced Usage
Custom Dataset Integration
from torch.utils.data import Dataset
from utils import load_dataset_from_csv
# Load from CSV
image_paths, labels, class_names = load_dataset_from_csv(
"dataset.csv",
image_col="image_path",
label_col="label"
)
# Create custom dataset
class MyDataset(Dataset):
def __init__(self, image_paths, labels, transform=None):
self.image_paths = image_paths
self.labels = labels
self.transform = transform
def __getitem__(self, idx):
# Your custom loading logic
pass
Model Evaluation
from utils import evaluate_model, plot_confusion_matrix
# Evaluate model
metrics = evaluate_model(model, test_loader, class_names, device)
print(f"Accuracy: {metrics['accuracy']:.4f}")
# Plot confusion matrix
plot_confusion_matrix(
metrics['true_labels'],
metrics['predictions'],
class_names
)
Hyperparameter Optimization with Weights & Biases
import wandb
# Initialize sweep
sweep_config = {
'method': 'random',
'parameters': {
'hidden_dim': {'values': [128, 256, 512]},
'learning_rate': {'min': 0.0001, 'max': 0.01},
'num_layers': {'values': [3, 4, 5, 6]},
'dropout_rate': {'min': 0.0, 'max': 0.3}
}
}
sweep_id = wandb.sweep(sweep_config, project="universal-classifier")
wandb.agent(sweep_id, train_function)
๐ค Hugging Face Integration
Export Model
from utils import export_to_huggingface
export_to_huggingface(
model_path="outputs/model.pth",
config_path="outputs/config.json",
class_names=["cat", "dog"],
model_name="mlp_deep_residual",
output_dir="huggingface_model"
)
Upload to Hub
from huggingface_hub import upload_folder
upload_folder(
folder_path="huggingface_model",
repo_id="your-username/universal-classifier",
repo_type="model"
)
๐ Performance Tips
- ๐ฏ Data Augmentation: Enable for small datasets
- ๐ Learning Rate: Start with 0.001, reduce if loss plateaus
- ๐๏ธ Architecture: Use residual connections for deep networks
- ๐๏ธ Regularization: Adjust dropout and weight decay based on overfitting
- ๐ฆ Batch Size: Larger batches for stable training, smaller for better generalization
- โฐ Early Stopping: Prevent overfitting with patience-based stopping
๐ Model Analysis
from models import count_parameters, calculate_model_size_mb
model = create_model("mlp_deep_residual", config)
print(f"Parameters: {count_parameters(model):,}")
print(f"Model size: {calculate_model_size_mb(model):.2f} MB")
๐จ Example Applications
- ๐ Object Recognition: Classify everyday objects
- ๐ฅ Medical Imaging: Diagnostic image classification
- ๐ญ Quality Control: Defect detection in manufacturing
- ๐ก๏ธ Content Moderation: Automatic image filtering
- ๐ฆ Wildlife Monitoring: Species identification
- ๐จ Art Classification: Style and genre recognition
- ๐ Food Recognition: Cuisine and dish classification
๐ Benchmarks
Dataset | Model | Accuracy | Parameters | Training Time |
---|---|---|---|---|
CIFAR-10 | Linear | 45.2% | 196K | 5 min |
CIFAR-10 | MLP | 52.8% | 1.2M | 15 min |
CIFAR-10 | Deep MLP | 61.4% | 2.1M | 25 min |
CIFAR-10 | Deep Residual | 68.9% | 2.1M | 30 min |
Benchmarks run on NVIDIA RTX 3080, batch size 64
๐ค Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
Development Setup
# Clone and install in development mode
git clone https://github.com/hightechguys79/universal-image-classifier.git
cd universal-image-classifier
pip install -e .
# Run tests
python -m pytest tests/
# Format code
black .
isort .
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- PyTorch team for the excellent deep learning framework
- Hugging Face for the model hub infrastructure
- The open-source community for inspiration and feedback
๐ Support
- ๐ Bug Reports: GitHub Issues
- ๐ฌ Discussions: GitHub Discussions
- ๐ง Email: [email protected]
๐บ๏ธ Roadmap
- CNN architectures (ResNet, EfficientNet)
- Vision Transformers support
- Multi-label classification
- Object detection capabilities
- TensorRT optimization
- ONNX export support
- Gradio web interface
- Docker containerization
โญ If you find this project helpful, please give it a star on GitHub!
Made with โค๏ธ by the Universal Image Classifier team
- Downloads last month
- 5
Inference Providers
NEW
This model isn't deployed by any Inference Provider.
๐
1
Ask for provider support