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?
#π Problem with my neural network to recognize my own handwritten digits.
65 messages Β· Page 1 of 1 (latest)
@carmine bear
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.
Closes after a period of inactivity, or when you send !close.
Different datasets have different trends
Wdym?
Probably your data being malformatted
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
They are
or the numbers have different clippings
can you show the code you're using to make predictions?
!pastebin
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.
Images are passed to detect()
You need to share the new URL here π
Can you show some examples of image in your training set, and images you have drawn yourself and are using for testing?
that's the network - I'm interested in knowing specifically what data you're passing to the feedforward method
or detect, as you've named it
From MNIST dataset
I'm still betting that your data is transposed or something compared to mnist
numpy array as image
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
that looks way more high resolution than MNIST's
all are 28x28
see upper left downsampled image
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.
Its 0-1 i believe
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
it would be worth confirming this, and the data type
I mean the array's dtype
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
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))
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
(Thinking out loud)
Could it be that mnist has a 4px pad on all sides and you donβt?
ok
Mnist img is 20x20 centered in a 28x28 img.
(According to internet, if this is useless information, just disregard. π )
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.
wait so you're running these programs in different places?
that's definitely concerning
Trained on google colab, ran on my machine
try getting the mnist data working locally
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?
hm, you might try padding out the image by 4 pixels
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.