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
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "4.0.0"
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# PyTorch Neural Network Classifier
|
| 13 |
+
|
| 14 |
+
This is a Gradio interface for a convolutional neural network based on the [PyTorch Neural Networks Tutorial](https://pytorch.org/tutorials/beginner/blitz/neural_networks_tutorial.html). The model is a simplified version of LeNet-5, designed for image classification tasks.
|
| 15 |
+
|
| 16 |
+
## Model Architecture
|
| 17 |
+
|
| 18 |
+
The neural network has the following architecture (exactly as shown in the PyTorch tutorial):
|
| 19 |
+
- Two convolutional layers with ReLU activation and max pooling
|
| 20 |
+
- Three fully connected layers
|
| 21 |
+
- Designed for 32x32 grayscale input images
|
| 22 |
+
|
| 23 |
+
```
|
| 24 |
+
Input β Conv2d(1, 6, 5) β ReLU β MaxPool2d(2, 2) β
|
| 25 |
+
Conv2d(6, 16, 5) β ReLU β MaxPool2d(2, 2) β
|
| 26 |
+
Flatten β Linear(16*5*5, 120) β ReLU β
|
| 27 |
+
Linear(120, 84) β ReLU β Linear(84, 10) β Output
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## Features
|
| 31 |
+
|
| 32 |
+
- Interactive image classification interface with modern UI
|
| 33 |
+
- Example images for quick testing
|
| 34 |
+
- Real-time predictions with probability scores
|
| 35 |
+
- Support for custom image uploads
|
| 36 |
+
- Built-in drawing tool for creating test images
|
| 37 |
+
- Responsive design with gradient backgrounds and animations
|
| 38 |
+
- Automatic image preprocessing (resize, grayscale conversion)
|
| 39 |
+
|
| 40 |
+
## How to Use with Your Existing Model
|
| 41 |
+
|
| 42 |
+
1. Place your trained PyTorch model file in the app directory and name it `model.pth`
|
| 43 |
+
2. Ensure your model uses the same architecture as defined in the Net class
|
| 44 |
+
3. Install the required dependencies:
|
| 45 |
+
```bash
|
| 46 |
+
pip install -r requirements.txt
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
4. Run the application:
|
| 50 |
+
```bash
|
| 51 |
+
python app.py
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
5. Access the interface at `http://localhost:7860` (or the URL provided in the terminal)
|
| 55 |
+
|
| 56 |
+
## Image Input Capabilities
|
| 57 |
+
|
| 58 |
+
The model can handle various types of image inputs:
|
| 59 |
+
|
| 60 |
+
### Supported Image Formats
|
| 61 |
+
- JPG, PNG, BMP, TIFF, and other common image formats
|
| 62 |
+
- Color images (automatically converted to grayscale)
|
| 63 |
+
- Any resolution (automatically resized to 32Γ32 pixels)
|
| 64 |
+
|
| 65 |
+
### Robustness Features
|
| 66 |
+
- **Resolution Independence**: Works with images of any size (resized to 32Γ32)
|
| 67 |
+
- **Color Conversion**: Automatically converts color images to grayscale
|
| 68 |
+
- **Contrast Handling**: Works with both high and low contrast images
|
| 69 |
+
- **Noise Tolerance**: Can handle some image noise
|
| 70 |
+
- **Rotation Tolerance**: Some tolerance to slight rotations
|
| 71 |
+
|
| 72 |
+
### Best Practices for Good Results
|
| 73 |
+
1. **Center the digit** in the image area
|
| 74 |
+
2. **Use clear contrast** between the digit and background
|
| 75 |
+
3. **Fill most of the image** area with the digit
|
| 76 |
+
4. **Avoid excessive noise** or artifacts
|
| 77 |
+
5. **Use dark digits on light background** or vice versa
|
| 78 |
+
|
| 79 |
+
## Deployment to Hugging Face Spaces
|
| 80 |
+
|
| 81 |
+
This application can be deployed to Hugging Face Spaces by:
|
| 82 |
+
|
| 83 |
+
1. Creating a new Space on Hugging Face
|
| 84 |
+
2. Uploading these files to the repository
|
| 85 |
+
3. Setting the SDK to "Gradio"
|
| 86 |
+
4. Adding the requirements in the requirements.txt file
|
| 87 |
+
5. Uploading your `model.pth` file
|
| 88 |
+
|
| 89 |
+
The Space will automatically run the `app.py` file as the entry point.
|
| 90 |
+
|
| 91 |
+
## Example Usage
|
| 92 |
+
|
| 93 |
+
The interface comes with hand-drawn example images that demonstrate how the classifier works. You can:
|
| 94 |
+
1. Click on any example image to load it
|
| 95 |
+
2. Upload your own image using the file browser
|
| 96 |
+
3. Draw an image using the built-in sketch tool
|
| 97 |
+
4. View the classification probabilities for each class
|
| 98 |
+
|
| 99 |
+
Try these examples:
|
| 100 |
+
- Handwritten digits of different styles
|
| 101 |
+
- Printed digits
|
| 102 |
+
- Digits with varying thickness
|
| 103 |
+
- Digits with different backgrounds
|
| 104 |
+
|
| 105 |
+
## Technical Details
|
| 106 |
+
|
| 107 |
+
This implementation follows the PyTorch tutorial exactly and includes:
|
| 108 |
+
- Gradio interface with custom CSS styling
|
| 109 |
+
- Image preprocessing pipeline (resize to 32x32, grayscale conversion)
|
| 110 |
+
- Softmax probability output (as shown in the tutorial)
|
| 111 |
+
- Example generation for demonstration
|
| 112 |
+
- Model loading functionality for your trained weights
|
| 113 |
+
|
| 114 |
+
The prediction function exactly matches the tutorial:
|
| 115 |
+
```python
|
| 116 |
+
model.eval()
|
| 117 |
+
with torch.no_grad():
|
| 118 |
+
output = model(input_tensor)
|
| 119 |
+
probabilities = F.softmax(output, dim=1)
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
The UI features:
|
| 123 |
+
- Animated gradient background
|
| 124 |
+
- Glass-morphism design elements
|
| 125 |
+
- Responsive layout for all screen sizes
|
| 126 |
+
- Interactive buttons with hover effects
|
| 127 |
+
- Clean, modern typography
|
| 128 |
+
|
| 129 |
+
## Requirements
|
| 130 |
+
|
| 131 |
+
- Python 3.6+
|
| 132 |
+
- PyTorch >= 1.7.0
|
| 133 |
+
- TorchVision >= 0.8.0
|
| 134 |
+
- Gradio >= 4.0.0
|
| 135 |
+
- Pillow >= 8.0.0
|
| 136 |
+
- NumPy >= 1.19.0
|
| 137 |
+
|
| 138 |
+
Install with:
|
| 139 |
+
```bash
|
| 140 |
+
pip install -r requirements.txt
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
## Troubleshooting
|
| 144 |
+
|
| 145 |
+
If you encounter issues:
|
| 146 |
+
1. Ensure your `model.pth` file is in the same directory as `app.py`
|
| 147 |
+
2. Verify that your model uses the same architecture as defined in the Net class
|
| 148 |
+
3. Check that all required dependencies are installed
|
| 149 |
+
4. Make sure you're using a compatible version of Python (3.6+)
|
| 150 |
+
|
| 151 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|