diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index ce7d1f0..ad5314e 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -19,12 +19,12 @@
-
-
-
+
+
+
-
-
+
+
@@ -106,6 +106,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -147,35 +166,10 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -215,18 +209,18 @@
-
+
-
+
+
-
@@ -262,7 +256,9 @@
-
+
+
+
1589815443652
@@ -341,7 +337,14 @@
1590359074952
-
+
+ 1590447313737
+
+
+
+ 1590447313737
+
+
@@ -372,7 +375,8 @@
-
+
+
@@ -399,10 +403,10 @@
-
+
-
+
diff --git a/coder/coder.py b/coder/coder.py
new file mode 100644
index 0000000..e6f0d84
--- /dev/null
+++ b/coder/coder.py
@@ -0,0 +1,58 @@
+import numpy as np
+import torch
+import torchvision
+import matplotlib.pyplot as plt
+from time import time
+from torchvision import datasets, transforms
+from torch import nn, optim, nn, optim
+import cv2
+
+
+def view_classify(img, ps):
+ ''' Function for viewing an image and it's predicted classes.
+ '''
+ ps = ps.data.numpy().squeeze()
+
+ fig, (ax1, ax2) = plt.subplots(figsize=(6,9), ncols=2)
+ ax1.imshow(img.resize_(1, 28, 28).numpy().squeeze())
+ ax1.axis('off')
+ ax2.barh(np.arange(10), ps)
+ ax2.set_aspect(0.1)
+ ax2.set_yticks(np.arange(10))
+ ax2.set_yticklabels(np.arange(10))
+ ax2.set_title('Class Probability')
+ ax2.set_xlim(0, 1.1)
+ plt.tight_layout()
+
+# load nn model
+model = torch.load('digit_reco_model2.pt')
+
+if model is None:
+ print("Model is not loaded.")
+else:
+ print("Model is loaded.")
+
+# image
+img = cv2.cvtColor(cv2.imread('test3.png'), cv2.COLOR_BGR2GRAY)
+img = cv2.blur(img, (9, 9)) # poprawia jakosc
+img = cv2.resize(img, (28, 28), interpolation=cv2.INTER_AREA)
+img = img.reshape((len(img), -1))
+
+print(type(img))
+# print(img.shape)
+# plt.imshow(img ,cmap='binary')
+# plt.show()
+img = np.array(img, dtype=np.float32)
+img = torch.from_numpy(img)
+img = img.view(1, 784)
+
+# recognizing
+
+with torch.no_grad():
+ logps = model(img)
+
+ps = torch.exp(logps)
+probab = list(ps.numpy()[0])
+print("Predicted Digit =", probab.index(max(probab)))
+
+view_classify(img.view(1, 28, 28), ps)
diff --git a/coder/digit_reco_model.pt b/coder/digit_reco_model.pt
new file mode 100644
index 0000000..391642b
Binary files /dev/null and b/coder/digit_reco_model.pt differ
diff --git a/coder/digit_reco_model2.pt b/coder/digit_reco_model2.pt
new file mode 100644
index 0000000..f8f60db
Binary files /dev/null and b/coder/digit_reco_model2.pt differ
diff --git a/coder/digits_recognizer.py b/coder/digits_recognizer.py
index cc06540..0788313 100644
--- a/coder/digits_recognizer.py
+++ b/coder/digits_recognizer.py
@@ -6,17 +6,19 @@ from time import time
from torchvision import datasets, transforms
from torch import nn, optim
+# IMG transform
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
-trainset = datasets.MNIST('PATH_TO_STORE_TRAINSET', download=True, train=True, transform=transform)
-valset = datasets.MNIST('PATH_TO_STORE_TESTSET', download=True, train=False, transform=transform)
-trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
-valloader = torch.utils.data.DataLoader(valset, batch_size=64, shuffle=True)
+# dataset download
+train_set = datasets.MNIST('PATH_TO_STORE_TRAINSET', download=True, train=True, transform=transform)
+val_set = datasets.MNIST('PATH_TO_STORE_TESTSET', download=True, train=False, transform=transform)
+train_loader = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True)
+val_loader = torch.utils.data.DataLoader(val_set, batch_size=64, shuffle=True)
-dataiter = iter(trainloader)
-images, labels = dataiter.next()
+data_iter = iter(train_loader)
+images, labels = data_iter.next()
print(images.shape)
print(labels.shape)
@@ -25,15 +27,93 @@ plt.imshow(images[0].numpy().squeeze(), cmap='gray_r')
plt.show()
# building nn model
-input_size = 784
-hidden_sizes = [128, 64]
+input_size = 784 # = 28*28
+hidden_sizes = [128, 128, 64]
output_size = 10
model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),
nn.ReLU(),
nn.Linear(hidden_sizes[0], hidden_sizes[1]),
nn.ReLU(),
- nn.Linear(hidden_sizes[1], output_size),
- nn.LogSoftmax(dim=1))
-print(model)
+ nn.Linear(hidden_sizes[1], hidden_sizes[2]),
+ nn.ReLU(),
+ nn.Linear(hidden_sizes[2], output_size),
+ nn.LogSoftmax(dim=-1))
+# print(model)
+criterion = nn.NLLLoss()
+images, labels = next(iter(train_loader))
+images = images.view(images.shape[0], -1)
+
+logps = model(images) # log probabilities
+loss = criterion(logps, labels) # calculate the NLL loss
+
+# print('Before backward pass: \n', model[0].weight.grad)
+loss.backward()
+# print('After backward pass: \n', model[0].weight.grad)
+
+# training
+
+optimizer = optim.SGD(model.parameters(), lr=0.003, momentum=0.9)
+time0 = time()
+epochs = 100
+for e in range(epochs):
+ running_loss = 0
+ for images, labels in train_loader:
+ # Flatten MNIST images into a 784 long vector
+ images = images.view(images.shape[0], -1)
+
+ # Training pass
+ optimizer.zero_grad()
+
+ output = model(images)
+ loss = criterion(output, labels)
+
+ # This is where the model learns by backpropagating
+ loss.backward()
+
+ # And optimizes its weights here
+ optimizer.step()
+
+ running_loss += loss.item()
+ else:
+ print("Epoch {} - Training loss: {}".format(e + 1, running_loss / len(train_loader)))
+
+print("\nTraining Time (in minutes) =", (time() - time0) / 60)
+
+# testing
+
+images, labels = next(iter(val_loader))
+print(type(images))
+img = images[0].view(1, 784)
+with torch.no_grad():
+ logps = model(img)
+ps = torch.exp(logps)
+probab = list(ps.numpy()[0])
+print("Predicted Digit =", probab.index(max(probab)))
+# view_classify(img.view(1, 28, 28), ps)
+
+# accuracy
+correct_count, all_count = 0, 0
+for images, labels in val_loader:
+ for i in range(len(labels)):
+ img = images[i].view(1, 784)
+ with torch.no_grad():
+ logps = model(img)
+
+ ps = torch.exp(logps)
+ probab = list(ps.numpy()[0])
+ pred_label = probab.index(max(probab))
+ true_label = labels.numpy()[i]
+ if true_label == pred_label:
+ correct_count += 1
+ all_count += 1
+
+print("Number Of Images Tested =", all_count)
+print("\nModel Accuracy =", (correct_count / all_count))
+
+
+# saving model
+
+# torch.save(model, './digit_reco_model.pt')
+torch.save(model, './digit_reco_model2.pt')
\ No newline at end of file
diff --git a/coder/gr_test.png b/coder/gr_test.png
deleted file mode 100644
index 40cca5a..0000000
Binary files a/coder/gr_test.png and /dev/null differ