Spaces:
Sleeping
Sleeping
| import torch | |
| from torch import nn | |
| class Net(nn.Module): | |
| def __init__(self): | |
| super(Net, self).__init__() | |
| self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1) | |
| self.relu = nn.ReLU() | |
| self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2) | |
| self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1) | |
| self.flatten = nn.Flatten() | |
| self.fc1 = nn.Linear(64 * 7 * 7, 128) | |
| self.fc2 = nn.Linear(128, 10) | |
| def forward(self, x): | |
| x = self.conv1(x) | |
| x = self.relu(x) | |
| x = self.maxpool(x) | |
| x = self.conv2(x) | |
| x = self.relu(x) | |
| x = self.maxpool(x) | |
| x = self.flatten(x) | |
| x = self.fc1(x) | |
| x = self.relu(x) | |
| x = self.fc2(x) | |
| return x | |
| def predict(model, image): | |
| model.eval() | |
| with torch.no_grad(): | |
| output = model(image) | |
| result = torch.argmax(output,dim=1).item() | |
| return result | |