Spaces:
Runtime error
Runtime error
trained pth files 2
Browse files- app.py +25 -22
- weights/mnist_model.pth +0 -0
- weights/optimizer.pth +0 -0
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import torch.optim as optim
|
|
| 8 |
|
| 9 |
# This is just to show an interface where one draws a number and gets prediction.
|
| 10 |
|
| 11 |
-
n_epochs =
|
| 12 |
batch_size_train = 128
|
| 13 |
batch_size_test = 1000
|
| 14 |
learning_rate = 0.01
|
|
@@ -67,16 +67,16 @@ class MNIST_Model(nn.Module):
|
|
| 67 |
return F.log_softmax(x)
|
| 68 |
|
| 69 |
train_loader = torch.utils.data.DataLoader(
|
| 70 |
-
torchvision.datasets.MNIST('
|
| 71 |
transform=torchvision.transforms.Compose([
|
| 72 |
torchvision.transforms.ToTensor(),
|
| 73 |
torchvision.transforms.Normalize(
|
| 74 |
-
(0.1307,), (0.3081,))
|
| 75 |
])),
|
| 76 |
batch_size=batch_size_train, shuffle=True)
|
| 77 |
|
| 78 |
test_loader = torch.utils.data.DataLoader(
|
| 79 |
-
torchvision.datasets.MNIST('
|
| 80 |
transform=torchvision.transforms.Compose([
|
| 81 |
torchvision.transforms.ToTensor(),
|
| 82 |
torchvision.transforms.Normalize(
|
|
@@ -84,25 +84,24 @@ test_loader = torch.utils.data.DataLoader(
|
|
| 84 |
])),
|
| 85 |
batch_size=batch_size_test, shuffle=True)
|
| 86 |
|
| 87 |
-
def train(
|
| 88 |
|
| 89 |
train_losses=[]
|
| 90 |
network.train()
|
| 91 |
-
for
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
torch.save(optimizer.state_dict(), OPTIMIZER_PATH)
|
| 106 |
|
| 107 |
def test():
|
| 108 |
test_losses=[]
|
|
@@ -121,6 +120,7 @@ def test():
|
|
| 121 |
acc = acc.item()
|
| 122 |
test_metric = '〽Current test metric -> Avg. loss: `{:.4f}`, Accuracy: `{:.0f}%`\n'.format(
|
| 123 |
test_loss,acc)
|
|
|
|
| 124 |
return test_metric,acc
|
| 125 |
|
| 126 |
|
|
@@ -143,8 +143,11 @@ if os.path.exists(model_state_dict) and os.path.exists(optimizer_state_dict):
|
|
| 143 |
optimizer_state_dict = torch.load(optimizer_state_dict)
|
| 144 |
optimizer.load_state_dict(optimizer_state_dict)
|
| 145 |
# Train
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
|
| 150 |
def image_classifier(inp):
|
|
|
|
| 8 |
|
| 9 |
# This is just to show an interface where one draws a number and gets prediction.
|
| 10 |
|
| 11 |
+
n_epochs = 10
|
| 12 |
batch_size_train = 128
|
| 13 |
batch_size_test = 1000
|
| 14 |
learning_rate = 0.01
|
|
|
|
| 67 |
return F.log_softmax(x)
|
| 68 |
|
| 69 |
train_loader = torch.utils.data.DataLoader(
|
| 70 |
+
torchvision.datasets.MNIST('files/', train=True, download=True,
|
| 71 |
transform=torchvision.transforms.Compose([
|
| 72 |
torchvision.transforms.ToTensor(),
|
| 73 |
torchvision.transforms.Normalize(
|
| 74 |
+
mean=(0.1307,), std=(0.3081,))
|
| 75 |
])),
|
| 76 |
batch_size=batch_size_train, shuffle=True)
|
| 77 |
|
| 78 |
test_loader = torch.utils.data.DataLoader(
|
| 79 |
+
torchvision.datasets.MNIST('files/', train=False, download=True,
|
| 80 |
transform=torchvision.transforms.Compose([
|
| 81 |
torchvision.transforms.ToTensor(),
|
| 82 |
torchvision.transforms.Normalize(
|
|
|
|
| 84 |
])),
|
| 85 |
batch_size=batch_size_test, shuffle=True)
|
| 86 |
|
| 87 |
+
def train(epoch,network,optimizer,train_loader):
|
| 88 |
|
| 89 |
train_losses=[]
|
| 90 |
network.train()
|
| 91 |
+
for batch_idx, (data, target) in enumerate(train_loader):
|
| 92 |
+
optimizer.zero_grad()
|
| 93 |
+
output = network(data)
|
| 94 |
+
loss = F.nll_loss(output, target)
|
| 95 |
+
loss.backward()
|
| 96 |
+
optimizer.step()
|
| 97 |
+
if batch_idx % log_interval == 0:
|
| 98 |
+
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
|
| 99 |
+
epoch, batch_idx * len(data), len(train_loader.dataset),
|
| 100 |
+
100. * batch_idx / len(train_loader), loss.item()))
|
| 101 |
+
train_losses.append(loss.item())
|
| 102 |
+
|
| 103 |
+
torch.save(network.state_dict(), MODEL_WEIGHTS_PATH)
|
| 104 |
+
torch.save(optimizer.state_dict(), OPTIMIZER_PATH)
|
|
|
|
| 105 |
|
| 106 |
def test():
|
| 107 |
test_losses=[]
|
|
|
|
| 120 |
acc = acc.item()
|
| 121 |
test_metric = '〽Current test metric -> Avg. loss: `{:.4f}`, Accuracy: `{:.0f}%`\n'.format(
|
| 122 |
test_loss,acc)
|
| 123 |
+
print(test_metric)
|
| 124 |
return test_metric,acc
|
| 125 |
|
| 126 |
|
|
|
|
| 143 |
optimizer_state_dict = torch.load(optimizer_state_dict)
|
| 144 |
optimizer.load_state_dict(optimizer_state_dict)
|
| 145 |
# Train
|
| 146 |
+
|
| 147 |
+
for epoch in range(n_epochs):
|
| 148 |
+
|
| 149 |
+
train(epoch,network,optimizer,train_loader)
|
| 150 |
+
test()
|
| 151 |
|
| 152 |
|
| 153 |
def image_classifier(inp):
|
weights/mnist_model.pth
ADDED
|
Binary file (89.9 kB). View file
|
|
|
weights/optimizer.pth
ADDED
|
Binary file (89.8 kB). View file
|
|
|