Spaces:
Runtime error
A newer version of the Gradio SDK is available:
6.4.0
title: CIFAR-10 Image Classifier
emoji: π
colorFrom: blue
colorTo: red
sdk: gradio
sdk_version: 4.44.1
app_file: app.py
pinned: false
CIFAR-10 Image Classifier
This is a Gradio interface for a convolutional neural network trained on the CIFAR-10 dataset. The model can classify images into 10 different object categories: Airplane, Automobile, Bird, Cat, Deer, Dog, Frog, Horse, Ship, and Truck.
Model Architecture
The neural network has the following architecture (based on the PyTorch CIFAR-10 Tutorial):
- Two convolutional layers with ReLU activation and max pooling
- Three fully connected layers
- Designed for 32x32 RGB input images
Input β Conv2d(3, 6, 5) β ReLU β MaxPool2d(2, 2) β
Conv2d(6, 16, 5) β ReLU β MaxPool2d(2, 2) β
Flatten β Linear(16*5*5, 120) β ReLU β
Linear(120, 84) β ReLU β Linear(84, 10) β Output
Features
- Interactive image classification interface with modern UI
- Example images for each CIFAR-10 class
- Real-time predictions with probability scores
- Support for custom image uploads
- Built-in drawing tool for creating test images
- Responsive design with gradient backgrounds and animations
- Automatic image preprocessing (resize to 32Γ32)
How to Use with Your Existing Model
Place your trained PyTorch model file in the app directory and name it
model.pthEnsure your model uses the same architecture as defined in the Net class
Install the required dependencies:
pip install -r requirements.txtRun the application:
python app.pyAccess the interface at
http://localhost:7860(or the URL provided in the terminal)
Image Input Capabilities
The model can handle various types of image inputs:
Supported Image Formats
- JPG, PNG, BMP, TIFF, and other common image formats
- Color images (RGB with 3 channels)
- Any resolution (automatically resized to 32Γ32 pixels)
Robustness Features
- Resolution Independence: Works with images of any size (resized to 32Γ32)
- Color Preservation: Maintains RGB color information
- Contrast Handling: Works with both high and low contrast images
- Noise Tolerance: Can handle some image noise
- Rotation Tolerance: Some tolerance to slight rotations
Best Practices for Good Results
- Center the object in the image area
- Use clear contrast between the object and background
- Fill most of the image area with the object
- Avoid excessive noise or artifacts
- Ensure the object is clearly visible
CIFAR-10 Classes
The model classifies images into these 10 categories:
- Airplane - Aircraft flying in the sky
- Automobile - Cars and vehicles on the road
- Bird - Flying or perched birds
- Cat - Domestic cats and felines
- Deer - Wild deer and similar animals
- Dog - Domestic dogs and canines
- Frog - Amphibians like frogs
- Horse - Horses and similar animals
- Ship - Boats and ships on water
- Truck - Trucks and heavy vehicles
Deployment to Hugging Face Spaces
This application can be deployed to Hugging Face Spaces by:
- Creating a new Space on Hugging Face
- Uploading these files to the repository
- Setting the SDK to "Gradio"
- Adding the requirements in the requirements.txt file
- Uploading your
model.pthfile
The Space will automatically run the app.py file as the entry point.
Example Usage
The interface comes with simple example images representing each CIFAR-10 class. You can:
- Click on any example image to load it
- Upload your own image using the file browser
- Draw an image using the built-in sketch tool
- View the classification probabilities for each class
Try these examples:
- Simple drawings of objects from each class
- Photos of objects that match the CIFAR-10 categories
- Images with varying styles and backgrounds
Technical Details
This implementation is based on the PyTorch CIFAR-10 tutorial and includes:
- Gradio interface with custom CSS styling
- Image preprocessing pipeline (resize to 32x32)
- Softmax probability output
- Example generation for demonstration
- Model loading functionality for your trained weights
The prediction function:
model.eval()
with torch.no_grad():
output = model(input_tensor)
probabilities = F.softmax(output, dim=1)
The UI features:
- Animated gradient background
- Glass-morphism design elements
- Responsive layout for all screen sizes
- Interactive buttons with hover effects
- Clean, modern typography
Requirements
- Python 3.6+
- PyTorch >= 1.7.0
- TorchVision >= 0.8.0
- Gradio >= 4.0.0
- Pillow >= 8.0.0
- NumPy >= 1.19.0
Install with:
pip install -r requirements.txt
Troubleshooting
If you encounter issues:
- Ensure your
model.pthfile is in the same directory asapp.py - Verify that your model uses the same architecture as defined in the Net class
- Check that all required dependencies are installed
- Make sure you're using a compatible version of Python (3.6+)
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference