Downloads · 30 days
0
asenella/mnist_svhn_classifiers
mnist_svhn_classifiers is a machine learning model from asenella. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
Classifiers for MNIST and SVHN. Snippet of code on how to load them after downloading the files in datapath.
Downloads · 30 days
0
Access
Public
Updated Jun 13, 2023
Repo size
216 KB
Likes
0
Public
Click a slice to open those files.
.pt216 KB · 98%
From the Hugging Face model README
Classifiers for MNIST and SVHN.
Snippet of code on how to load them after downloading the files in data_path.
import os
import torch
import torch.nn.functional as F
from torch import nn, optim
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST, SVHN
from torchvision.transforms import ToTensor
class SVHN_Classifier(nn.Module):
def __init__(self):
super(SVHN_Classifier, self).__init__()
self.conv1 = nn.Conv2d(3, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(500, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 500)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x, dim=-1)
class MNIST_Classifier(nn.Module):
def __init__(self):
super(MNIST_Classifier, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x, dim=-1)
def load_mnist_svhn_classifiers(data_path, device="cuda"):
c1 = MNIST_Classifier()
c1.load_state_dict(torch.load(f"{data_path}/mnist.pt", map_location=device))
c2 = SVHN_Classifier()
c2.load_state_dict(torch.load(f"{data_path}/svhn.pt", map_location=device))
return {"mnist": c1.to(device).eval(), "svhn": c2.to(device).eval()}