chrisjay commited on
Commit
f91a2b3
1 Parent(s): 2b63af2

added train files and code 2

Browse files
.files/MNIST/raw/t10k-images-idx3-ubyte ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0fa7898d509279e482958e8ce81c8e77db3f2f8254e26661ceb7762c4d494ce7
3
+ size 7840016
.files/MNIST/raw/t10k-images-idx3-ubyte.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8d422c7b0a1c1c79245a5bcf07fe86e33eeafee792b84584aec276f5a2dbc4e6
3
+ size 1648877
.files/MNIST/raw/t10k-labels-idx1-ubyte ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ff7bcfd416de33731a308c3f266cc351222c34898ecbeaf847f06e48f7ec33f2
3
+ size 10008
.files/MNIST/raw/t10k-labels-idx1-ubyte.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f7ae60f92e00ec6debd23a6088c31dbd2371eca3ffa0defaefb259924204aec6
3
+ size 4542
.files/MNIST/raw/train-images-idx3-ubyte ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ba891046e6505d7aadcbbe25680a0738ad16aec93bde7f9b65e87a2fc25776db
3
+ size 47040016
.files/MNIST/raw/train-images-idx3-ubyte.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:440fcabf73cc546fa21475e81ea370265605f56be210a4024d2ca8f203523609
3
+ size 9912422
.files/MNIST/raw/train-labels-idx1-ubyte ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:65a50cbbf4e906d70832878ad85ccda5333a97f0f4c3dd2ef09a8a9eef7101c5
3
+ size 60008
.files/MNIST/raw/train-labels-idx1-ubyte.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3552534a0a558bbed6aed32b30c495cca23d567ec52cac8be1a0730e8010255c
3
+ size 28881
.gitattributes CHANGED
@@ -25,3 +25,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
  *.zstandard filter=lfs diff=lfs merge=lfs -text
27
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
25
  *.zip filter=lfs diff=lfs merge=lfs -text
26
  *.zstandard filter=lfs diff=lfs merge=lfs -text
27
  *tfevents* filter=lfs diff=lfs merge=lfs -text
28
+ *ubyte filter=lfs diff=lfs merge=lfs -text
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 = 10
12
  batch_size_train = 128
13
  batch_size_test = 1000
14
  learning_rate = 0.01
@@ -16,7 +16,8 @@ momentum = 0.5
16
  log_interval = 10
17
  random_seed = 1
18
  TRAIN_CUTOFF = 10
19
- MODEL_PATH = 'model'
 
20
  METRIC_PATH = os.path.join(MODEL_PATH,'metrics.json')
21
  MODEL_WEIGHTS_PATH = os.path.join(MODEL_PATH,'mnist_model.pth')
22
  OPTIMIZER_PATH = os.path.join(MODEL_PATH,'optimizer.pth')
@@ -65,6 +66,62 @@ class MNIST_Model(nn.Module):
65
  x = self.fc2(x)
66
  return F.log_softmax(x)
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
 
70
 
@@ -77,8 +134,17 @@ optimizer = optim.SGD(network.parameters(), lr=learning_rate,
77
  momentum=momentum)
78
 
79
 
 
 
 
 
 
 
 
 
80
  # Train
81
- #train(n_epochs,network,optimizer)
 
82
 
83
 
84
  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 = 100
12
  batch_size_train = 128
13
  batch_size_test = 1000
14
  learning_rate = 0.01
 
16
  log_interval = 10
17
  random_seed = 1
18
  TRAIN_CUTOFF = 10
19
+ MODEL_PATH = 'weights'
20
+ os.makedirs(MODEL_PATH,exist_ok=True)
21
  METRIC_PATH = os.path.join(MODEL_PATH,'metrics.json')
22
  MODEL_WEIGHTS_PATH = os.path.join(MODEL_PATH,'mnist_model.pth')
23
  OPTIMIZER_PATH = os.path.join(MODEL_PATH,'optimizer.pth')
 
66
  x = self.fc2(x)
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
+ (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('.files/', train=False, download=True,
80
+ transform=torchvision.transforms.Compose([
81
+ torchvision.transforms.ToTensor(),
82
+ torchvision.transforms.Normalize(
83
+ (0.1307,), (0.3081,))
84
+ ])),
85
+ batch_size=batch_size_test, shuffle=True)
86
+
87
+ def train(epochs,network,optimizer,train_loader):
88
+
89
+ train_losses=[]
90
+ network.train()
91
+ for epoch in range(epochs):
92
+ for batch_idx, (data, target) in enumerate(train_loader):
93
+ optimizer.zero_grad()
94
+ output = network(data)
95
+ loss = F.nll_loss(output, target)
96
+ loss.backward()
97
+ optimizer.step()
98
+ if batch_idx % log_interval == 0:
99
+ print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
100
+ epoch, batch_idx * len(data), len(train_loader.dataset),
101
+ 100. * batch_idx / len(train_loader), loss.item()))
102
+ train_losses.append(loss.item())
103
+
104
+ torch.save(network.state_dict(), MODEL_WEIGHTS_PATH)
105
+ torch.save(optimizer.state_dict(), OPTIMIZER_PATH)
106
+
107
+ def test():
108
+ test_losses=[]
109
+ network.eval()
110
+ test_loss = 0
111
+ correct = 0
112
+ with torch.no_grad():
113
+ for data, target in test_loader:
114
+ output = network(data)
115
+ test_loss += F.nll_loss(output, target, size_average=False).item()
116
+ pred = output.data.max(1, keepdim=True)[1]
117
+ correct += pred.eq(target.data.view_as(pred)).sum()
118
+ test_loss /= len(test_loader.dataset)
119
+ test_losses.append(test_loss)
120
+ acc = 100. * correct / len(test_loader.dataset)
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
 
127
 
 
134
  momentum=momentum)
135
 
136
 
137
+ model_state_dict = MODEL_WEIGHTS_PATH
138
+ optimizer_state_dict = OPTIMIZER_PATH
139
+ if os.path.exists(model_state_dict) and os.path.exists(optimizer_state_dict):
140
+ network_state_dict = torch.load(model_state_dict)
141
+ network.load_state_dict(network_state_dict)
142
+
143
+ optimizer_state_dict = torch.load(optimizer_state_dict)
144
+ optimizer.load_state_dict(optimizer_state_dict)
145
  # Train
146
+ train(n_epochs,network,optimizer,train_loader)
147
+ test()
148
 
149
 
150
  def image_classifier(inp):