Downloads · 30 days
2
7% of all-time downloads
ranjeetjha/univeral-img-classifier-small
univeral-img-classifier-small is a machine learning model from ranjeetjha. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
[](https://opensource.org/licenses/MIT) [](https://www.python.org/downloads/) [](https://pytorch.org/)
Downloads · 30 days
2
7% of all-time downloads
All-time downloads
28
Public
Repo size
44.2 MB
Likes
1
Public
Click a slice to open those files.
.pth88.4 MB · 100%
From the Hugging Face model README
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.
| 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 |
# Clone the repository
git clone https://github.com/hightechguys79/universal-image-classifier.git
cd universal-image-classifier
# Install dependencies
pip install -r requirements.txt
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")
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}")
# 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
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
your_dataset/
├── class1/
│ ├── img1.jpg
│ └── img2.jpg
└── class2/
├── img3.jpg
└── img4.jpg
image_path,label
path/to/img1.jpg,cat
path/to/img2.jpg,dog
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
)
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
)
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
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
)
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)
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"
)
from huggingface_hub import upload_folder
upload_folder(
folder_path="huggingface_model",
repo_id="your-username/universal-classifier",
repo_type="model"
)
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")
| 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
We welcome contributions! Please see our Contributing Guidelines for details.
# 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 .
This project is licensed under the MIT License - see the LICENSE file for details.
⭐ If you find this project helpful, please give it a star on GitHub!
Made with ❤️ by the Universal Image Classifier team