Upload Mini-Vision-V3 Model
Browse filesFiles changed (5)
- Mini-Vision-V3.pthAdded+3
- README.mdAdded+121
- demo.pyAdded+46
- model.pyAdded+44
- train.pyAdded+95
123version https://git-lfs.github.com/spec/v1oid sha256:31ef54c30c43db9ab2954812d65cb34801f5583964ebb6a90195246f867a2036size 1612075123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121---
license: mit
library_name: pytorch
tags:
- image-classification
- emnist
- cnn
- computer-vision
- pytorch
- mini-vision
- mini-vision-series
metrics:
- accuracy
pipeline_tag: image-classification
datasets:
- emnist
---
# Mini-Vision-V3: EMNIST Balanced Handwritten Character Classifier
 
Welcome to **Mini-Vision-V3**, the third model in the Mini-Vision series. Following the MNIST digit recognition task in V2, this model expands capabilities to **47 classes** of handwritten characters (Digits & Uppercase & Lowercase letters) using the EMNIST Balanced dataset. It features a deeper yet highly efficient 3-layer CNN architecture, achieving over 90% accuracy with less than half a million parameters.
## Model Description
Mini-Vision-V3 is a custom 3-layer CNN architecture tailored for 28x28 grayscale images. While maintaining a lightweight footprint with only **0.40M parameters** (half the size of V2), it handles the significantly increased complexity of 47 character classes. This project demonstrates how depth and Batch Normalization can improve performance on more complex classification tasks without increasing model size.
- **Dataset**: [EMNIST Balanced](https://www.nist.gov/itl/products-and-services/emnist-dataset) (28x28 grayscale images, 47 classes)
- **Framework**: PyTorch
- **Total Parameters**: 0.40M
## Model Architecture
The network utilizes a deeper structure compared to V2, featuring three convolutional blocks. This allows for better feature extraction in the more complex 47-class task.
| Layer | Input Channels | Output Channels | Kernel Size | Stride | Padding | Activation | Other |
| :--- | :---: | :---: | :---: | :---: | :---: | :--- | :--- |
| **Conv Block 1** | 1 | 32 | 3 | 1 | 1 | ReLU | MaxPool(2), BatchNorm |
| **Conv Block 2** | 32 | 64 | 3 | 1 | 1 | ReLU | MaxPool(2), BatchNorm |
| **Conv Block 3** | 64 | 128 | 3 | 1 | 1 | ReLU | MaxPool(2), BatchNorm |
| **Flatten** | - | - | - | - | - | - | Output: 1152 |
| **Linear 1** | 1152 | 256 | - | - | - | ReLU | Dropout(0.3) |
| **Linear 2** | 256 | 47 | - | - | - | - | - |
## Training Strategy
The training strategy was adjusted for the larger dataset and increased class complexity, utilizing a higher initial learning rate and a StepLR scheduler for convergence.
- **Optimizer**: SGD (Momentum=0.8)
- **Initial Learning Rate**: 0.05
- **Scheduler**: StepLR (Step size=5, Gamma=0.5)
- **Loss Function**: CrossEntropyLoss
- **Batch Size**: 256
- **Epochs**: 50 (Best model at Epoch 40)
- **Data Preprocessing**:
- EMNIST specific alignment: Rotate -90 degrees and Flip Horizontal (to match standard image orientation).
- Random Crop (28x28 with padding=2)
- Random Rotation (10 degrees)
## Performance
The model achieved solid results on the EMNIST Balanced test set (18800 samples), selected based on the best performing epoch (Epoch 40):
| Metric | Value |
| :--- | :---: |
| **Test Accuracy** | **90.06%** |
| Test Loss | 0.28 |
| Train Loss | 0.28 |
| Parameters | 0.40M |
### Training Visualization (TensorBoard)
Below are the training and testing curves visualized via TensorBoard.
#### 1. Training Loss

*(Recorded every epoch)*
#### 2. Test Loss & Accuracy

*(Recorded every epoch)*
## Quick Start
### Dependencies
- Python 3.x
- PyTorch
- Torchvision
- Gradio (for demo)
- Pillow
### Inference / Web Demo
Run the Gradio demo to draw characters and see predictions in real-time:
```bash
python demo.py
```
*Note: The demo supports inverted drawing (white ink on black background) to match the EMNIST format.*
## File Structure
```
.
├── model.py # Model architecture definition (MiniVisionV3)
├── train.py # Training script
├── demo.py # Gradio Web Interface
├── Mini-Vision-V3.pth # Trained model weights (Epoch 40)
├── config.json
├── README.md
└── assets
├── train_loss.png # Visualized train loss graph
└── test_loss.png # Visualized test loss graph
```
## License
This project is licensed under the MIT License.12345678910111213141516171819202122232425262728293031323334353637383940414243444546import gradio
import torch
import torchvision
from model import MiniVisionV3
from PIL import Image, ImageOps
old_classes = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35, 'a': 36, 'b': 37, 'd': 38, 'e': 39, 'f': 40, 'g': 41, 'h': 42, 'n': 43, 'q': 44, 'r': 45, 't': 46}
classes = {v: k for k, v in old_classes.items()}
transform = torchvision.transforms.Compose([
torchvision.transforms.Resize(28),
torchvision.transforms.ToTensor()])
def load_model():
minivisionv3 = MiniVisionV3()
state_dict = torch.load("Mini-Vision-V3.pth", weights_only=False)
minivisionv3.load_state_dict(state_dict)
minivisionv3.eval()
return minivisionv3
minivisionv3 = load_model()
def inference(img):
img_convert = ImageOps.invert(img["composite"])
input = transform(img_convert)
input = input.unsqueeze(0)
with torch.no_grad():
outputs = minivisionv3(input)
prob = torch.softmax(outputs, 1)
result = {}
for i in range(47):
result[str(classes[i])] = prob[0][i].item()
return result
demo = gradio.Interface(fn=inference,
inputs=gradio.Sketchpad(height=560, width=560, image_mode="L", label="Draw Here", type="pil"),
outputs=gradio.Label(label="Results"),
title="Mini-Vision-V3",
description="A lightweight CNN (0.4M params) trained on EMNIST Balanced for handwritten character recognition.")
if __name__ == '__main__':
demo.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344import torch
from torch import nn
class MiniVisionV3(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(1, 32, 3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(1152, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, 47),
)
def forward(self, x):
x = self.model(x)
return x
if __name__ == '__main__':
minivisionv3 = MiniVisionV3()
total_params = sum(param.numel() for param in minivisionv3.parameters())
print(f"Total params: {total_params / 1000000: .2f}M")
# with torch.no_grad():
# input = torch.randn(256, 1, 28, 28)
# output = minivisionv3(input)
# print(output)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import os
import sys
import torch
import torchvision
from model import MiniVisionV3
from torch.utils.data import DataLoader
from torchvision.transforms import functional as F
from tqdm import tqdm
from torch.utils.tensorboard import SummaryWriter
# Global config
epoch = 50
learningrate = 5e-2
batchsize = 256
save_folder = "MiniVisionV3"
writer = SummaryWriter("../MiniVisionV3_log")
if not os.path.exists(save_folder):
os.mkdir(save_folder)
transform_correct_train = torchvision.transforms.Compose([
torchvision.transforms.Lambda(lambda x: F.rotate(x, -90)),
F.hflip,
torchvision.transforms.RandomCrop(28, 2),
torchvision.transforms.RandomRotation(10),
torchvision.transforms.ToTensor()
])
transform_correct_test = torchvision.transforms.Compose([
torchvision.transforms.Lambda(lambda x: F.rotate(x, -90)),
F.hflip,
torchvision.transforms.ToTensor()
])
dataset_train = torchvision.datasets.EMNIST("../EMNIST_train", "balanced", train=True, download=True, transform=transform_correct_train)
dataset_test = torchvision.datasets.EMNIST("../EMNIST_test", "balanced", train=False, download=True, transform=transform_correct_test)
dataloader_train = DataLoader(dataset_train, batchsize, True)
dataloader_test = DataLoader(dataset_test, batchsize, False)
minivisionv3 = MiniVisionV3()
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(minivisionv3.parameters(), lr=learningrate, momentum=0.8)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 5, 0.5)
train_datasize = len(dataset_train)
test_datasize = len(dataset_test)
print(f"Train dataset size: {train_datasize}")
print(f"test dataset size: {test_datasize}")
for i in range(epoch):
print(f"=============== Epoch {i} Start | LR: {optimizer.param_groups[0]['lr']} ===============")
minivisionv3.train()
total_train_loss = 0
for data in tqdm(dataloader_train,file=sys.stdout):
optimizer.zero_grad()
imgs, labels = data
output = minivisionv3(imgs)
loss = loss_fn(output, labels)
loss.backward()
optimizer.step()
total_train_loss += loss.item() * len(imgs)
epoch_train_loss = total_train_loss / train_datasize
print(f"Train epoch loss: {epoch_train_loss:.2f}")
writer.add_scalar("Train Loss", epoch_train_loss, i)
minivisionv3.eval()
total_test_loss = 0
total_accuracy = 0
with torch.no_grad():
for data in tqdm(dataloader_test, file=sys.stdout):
imgs, labels = data
output = minivisionv3(imgs)
loss = loss_fn(output, labels)
total_test_loss += loss.item() * len(imgs)
accuracy = (output.argmax(1) == labels).sum().item()
total_accuracy += accuracy
epoch_test_loss = round(total_test_loss / test_datasize, 2)
print(f"Test epoch loss: {epoch_test_loss}")
writer.add_scalar("Test Loss", epoch_test_loss, i)
total_accuracy_percentage = round((total_accuracy / test_datasize) * 100, 2)
print(f"Test accuracy percentage: {total_accuracy_percentage}%")
writer.add_scalar("Test Accuracy", total_accuracy_percentage, i)
scheduler.step()
torch.save(minivisionv3.state_dict(), f"{save_folder}/MiniVisionV3_Epoch{i}.pth")Mini-Vision-V3.pthAdded+3
README.mdAdded+121
demo.pyAdded+46
model.pyAdded+44
train.pyAdded+95