|
--- |
|
license: mit |
|
pipeline_tag: image-classification |
|
--- |
|
MNIST Classification Comparison Project |
|
This repository hosts two classification models trained on the MNIST dataset, representing handwritten digits. The project's goal is to compare the performance of a deep learning model with a traditional machine learning model. |
|
|
|
Included Models |
|
The repository contains the following two models, saved after a parallel training process: |
|
|
|
Simple Neural Network (PyTorch): A Feed-Forward Neural Network (FFNN) model suitable for low-resolution image classification tasks. It was trained to recognize digits from 0 to 9. |
|
|
|
Random Forest (Scikit-learn): An ensemble model from the decision tree family, known for its robustness and high efficiency on structured data. |
|
|
|
Usage Instructions |
|
To use the models, please follow the steps below. |
|
|
|
Prerequisites |
|
The following Python libraries are required to load and run the models: |
|
|
|
``` |
|
pip install torch scikit-learn joblib huggingface-hub |
|
``` |
|
|
|
|
|
Loading the Models |
|
The following script allows you to download the model files from this repository and load them into memory for later use. |
|
|
|
``` |
|
import torch |
|
import torch.nn as nn |
|
from joblib import load |
|
from huggingface_hub import hf_hub_download |
|
|
|
# Definition of the Neural Network class for loading. |
|
class SimpleFFNN(nn.Module): |
|
def __init__(self): |
|
super(SimpleFFNN, self).__init__() |
|
self.fc1 = nn.Linear(28 * 28, 128) |
|
self.relu = nn.ReLU() |
|
self.fc2 = nn.Linear(128, 10) |
|
|
|
def forward(self, x): |
|
x = x.view(-1, 28 * 28) |
|
x = self.fc1(x) |
|
x = self.relu(x) |
|
x = self.fc2(x) |
|
return x |
|
|
|
# Hugging Face repository ID |
|
repo_id = "RAYAuser/ratron-minst-2tech" |
|
|
|
# Downloading the files |
|
ffnn_path = hf_hub_download(repo_id=repo_id, filename="ratron-neuronal-ffnn_model_state.pt") |
|
rf_path = hf_hub_download(repo_id=repo_id, filename="ratron-random_forest_model.joblib") |
|
|
|
# Loading the models |
|
ffnn_model_loaded = SimpleFFNN() |
|
ffnn_model_loaded.load_state_dict(torch.load(ffnn_path)) |
|
ffnn_model_loaded.eval() |
|
|
|
rf_model_loaded = load(rf_path) |
|
|
|
print("The models have been loaded successfully.") |
|
|
|
Prediction on New Data |
|
Once the models are loaded, you can use them to make inferences on new images. |
|
|
|
import numpy as np |
|
|
|
# Creating a numpy array to simulate an image (28x28 pixels) |
|
sample_image = np.random.rand(28, 28) |
|
|
|
# Reshaping the input data for the models |
|
sample_image_flat = sample_image.reshape(1, -1) |
|
ffnn_input_tensor = torch.from_numpy(sample_image_flat).float() |
|
|
|
# Prediction with the Neural Network |
|
with torch.no_grad(): |
|
output = ffnn_model_loaded(ffnn_input_tensor) |
|
_, ffnn_prediction = torch.max(output.data, 1) |
|
|
|
# Prediction with the Random Forest |
|
rf_prediction = rf_model_loaded.predict(sample_image_flat) |
|
|
|
print(f"Prediction by the Neural Network: {ffnn_prediction.item()}") |
|
print(f"Prediction by the Random Forest: {rf_prediction[0]}") |
|
``` |
|
|
|
Notes |
|
The SimpleFFNN class must be defined to allow the PyTorch model to be loaded. |
|
|
|
A warning regarding Scikit-learn version incompatibility may appear if the version used for training is not identical to the one in your environment. This is generally non-critical. |
|
|
|
RAY AUTRA TECHNOLOGY 2025 |