Downloads · 30 days
19
13% of all-time downloads
Akimotorakiyu/mnist-cnn-classifier
mnist-cnn-classifier is a image classification model from Akimotorakiyu. Use it when you need a label for an image. It is set up for transformers. The card lists the license as mit.
A custom Convolutional Neural Network for MNIST digit classification, built with PyTorch and compatible with Hugging Face Transformers.
Downloads · 30 days
19
13% of all-time downloads
All-time downloads
142
Public
Repo size
6.7 MB
Likes
0
Public
Click a slice to open those files.
.bin6.7 MB · 100%
From the Hugging Face model README
A custom Convolutional Neural Network for MNIST digit classification, built with PyTorch and compatible with Hugging Face Transformers.
This model implements a CNN architecture specifically designed for MNIST handwritten digit recognition. The model achieves over 98% accuracy on the MNIST test set and is fully compatible with the Hugging Face Transformers ecosystem.
from transformers import AutoModel, AutoImageProcessor
import torch
from PIL import Image
import numpy as np
# Load model and processor
model = AutoModel.from_pretrained("your-username/mnist-cnn-classifier")
processor = AutoImageProcessor.from_pretrained("your-username/mnist-cnn-classifier")
# Prepare image
image = Image.open("path/to/mnist_digit.png").convert("L") # Convert to grayscale
inputs = processor(images=image, return_tensors="pt")
# Forward pass
with torch.no_grad():
outputs = model(**inputs)
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
predicted_class = torch.argmax(predictions, dim=-1).item()
confidence = predictions[0][predicted_class].item()
print(f"Predicted digit: {predicted_class} (confidence: {confidence:.4f})")
import torch
from modeling_mnist_cnn import MnistCNN
from configuration_mnist_cnn import MnistCnnConfig
from torchvision import transforms
# Load configuration and model
config = MnistCnnConfig()
model = MnistCNN(config)
model.load_state_dict(torch.load("best_model.pth", map_location="cpu"))
model.eval()
# Define transforms
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
# Load and preprocess image
from PIL import Image
image = Image.open("digit.png").convert("L")
input_tensor = transform(image).unsqueeze(0)
# Predict
with torch.no_grad():
output = model(input_tensor)
prediction = torch.argmax(output, dim=1).item()
print(f"Predicted digit: {prediction}")
This model is designed for:
This model was trained on a standard academic dataset and poses no significant ethical concerns. It should be used responsibly for educational and research purposes.
The model was trained on the MNIST dataset, which is freely available for academic and research use. The dataset consists of:
pytorch_model.bin: Trained model weightsconfig.json: Model configurationpreprocessor_config.json: Image preprocessing configurationmodeling_mnist_cnn.py: Model architecture definitionconfiguration_mnist_cnn.py: Configuration classIf you use this model in your research, please cite:
@misc{mnist-cnn-classifier,
title={MNIST CNN Classifier},
author={Your Name},
year={2024},
url={https://huggingface.co/your-username/mnist-cnn-classifier}
}
This model is released under the MIT License.