#πŸ”’ Problem with my neural network to recognize my own handwritten digits.

65 messages Β· Page 1 of 1 (latest)

carmine bear
#

I have trained a neural network to detect handwritten digits on a 28x28 pixel grid from the MNIST dataset.
When I test the network on dataset given by MNIST, it does quite okay. 74.66% accuracy out of 5000 test images but when I make my own paint program or paint handwritten digits on photoshop on my own, it does a terrible job. When can be the reason for this?

modern chasmBOT
#

@carmine bear

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dawn coral
carmine bear
#

Wdym?

smoky bridge
#

you'll need to make sure that your own handwritten examples are in the same format as the MNIST data

#

i.e. maybe you inverted the colors or transposed the axes

carmine bear
#

They are

smoky bridge
#

or the numbers have different clippings

smoky bridge
carmine bear
#

!pastebin

modern chasmBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

carmine bear
#

Images are passed to detect()

rapid trout
carmine bear
jade gust
#

Can you show some examples of image in your training set, and images you have drawn yourself and are using for testing?

smoky bridge
#

or detect, as you've named it

smoky bridge
#

I'm still betting that your data is transposed or something compared to mnist

smoky bridge
# carmine bear

i know, but how is it formatted - where do you convert it from your drawing app into an mnist-shaped image

#

that's the code that probably contains an issue

jade gust
#

that looks way more high resolution than MNIST's

carmine bear
#

all are 28x28

smoky bridge
#

see upper left downsampled image

carmine bear
#
def detect(self):
        canvas_copy = self.canvas.scaled(QSize(28, 28), aspectRatioMode=Qt.KeepAspectRatio)
        pixmap = QImage(canvas_copy).convertToFormat(QImage.Format_Grayscale8)
        ptr = pixmap.bits()
        ptr.setsize(pixmap.byteCount())
        np_canvas = np.frombuffer(ptr, dtype=np.uint8).reshape((pixmap.height(), pixmap.width()))
        cv2.imshow("Win", np_canvas)
        print(self.det.detect(np_canvas/255))

From my paint program.

smoky bridge
#

np_canvas/255
you're passing in all 0's and 1's

#

I believe mnist is grayscale 0-255

carmine bear
#

Its 0-1 i believe

smoky bridge
#

you'll also want to cv2.imshow a couple of images from the mnist set to make sure that they're in the same row/column order

smoky bridge
carmine bear
#

numpy array

smoky bridge
#

I mean the array's dtype

carmine bear
#
transform = transforms.Compose([
    transforms.ToTensor(),
])

train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)

train_dataset_5000 = torch.utils.data.Subset(train_dataset, range(1000))
test_dataset_500 = torch.utils.data.Subset(test_dataset, range(500))

def dataset_to_ndarray(dataset):
    images = []
    labels = []

    for image, label in dataset:
        images.append(image.numpy())
        labels.append(label)

    images = np.array(images)
    labels = np.array(labels)

    return images, labels

# Converting datasets to numpy arrays
x_train, y_train = dataset_to_ndarray(train_dataset_5000)
x_test, y_test= dataset_to_ndarray(test_dataset_500)

Code to convert dataset to np array

#

They are float

smoky bridge
# carmine bear ```py def detect(self): canvas_copy = self.canvas.scaled(QSize(28, 28), ...

try adding ```py
def detect(self):
canvas_copy = self.canvas.scaled(QSize(28, 28), aspectRatioMode=Qt.KeepAspectRatio)
pixmap = QImage(canvas_copy).convertToFormat(QImage.Format_Grayscale8)
ptr = pixmap.bits()
ptr.setsize(pixmap.byteCount())
np_canvas = np.frombuffer(ptr, dtype=np.uint8).reshape((pixmap.height(), pixmap.width()))

mine = np_canvas / 255
mnist = x_test[0]
cv2.imshow("Mine", mine)
cv2.imshow("Mnist", mnist)

print(self.det.detect(mine))

carmine bear
#

In my paint program the array consists only 1s and 0s. The network is not used to those. So, I think that maybe the problem

rapid trout
#

(Thinking out loud)
Could it be that mnist has a 4px pad on all sides and you don’t?

smoky bridge
#

they should look identical

#

try to draw a number that looks exactly like x_test[0]

carmine bear
#

ok

rapid trout
#

Mnist img is 20x20 centered in a 28x28 img.
(According to internet, if this is useless information, just disregard. πŸ™‚ )

carmine bear
#

The result was 2 in the paint program but in google colab it was 7(correct)

#

I did load the weights and biases in the paint program.

smoky bridge
#

that's definitely concerning

carmine bear
#

Trained on google colab, ran on my machine

smoky bridge
#

try getting the mnist data working locally

carmine bear
#

I have

#

Test results are same

smoky bridge
# carmine bear

so like here if you add

def detect(self):
  canvas_copy = self.canvas.scaled(QSize(28, 28), aspectRatioMode=Qt.KeepAspectRatio)
  pixmap = QImage(canvas_copy).convertToFormat(QImage.Format_Grayscale8)
  ptr = pixmap.bits()
  ptr.setsize(pixmap.byteCount())
  np_canvas = np.frombuffer(ptr, dtype=np.uint8).reshape((pixmap.height(), pixmap.width()))

  mine = np_canvas / 255
  mnist = x_test[0]
  cv2.imshow("Mine", mine)
  cv2.imshow("Mnist", mnist)

  print("mine:", self.det.detect(mine))
  print("mnist:", self.det.detect(mnist))
#

for mnist it's 7 and for mine it's just wrong?

carmine bear
#

Yes

#

but my writing is not exactly like mnist

#

but its similar

smoky bridge
carmine bear
#

Explain how to do that

#

Maybe I need to train it on my own dataset

modern chasmBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.