#๐Ÿ”’ wrong predictions

10 messages ยท Page 1 of 1 (latest)

lilac gazelleBOT
#

@thorn bolt

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.

thorn bolt
#

import numpy as np
from keras.datasets import mnist
from scipy.special import softmax
from sklearn.decomposition import PCA
from keras.models import Sequential
from keras.layers import Dense
from tkinter import *
from PIL import ImageGrab, Image, ImageOps, ImageDraw
from sklearn.svm import SVC

np.random.seed(42)

Load handwritten digit dataset

(X_train, y_train), (X_test, y_test) = mnist.load_data()

Flatten the images from 28x28 to 784

X_train = X_train.reshape(X_train.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

Normalize the pixel values to range [0, 1]

X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0

lilac gazelleBOT
#

Hey @thorn bolt!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
thorn bolt
#

Stage 1: PCA-based Feature Extraction

pca = PCA(n_components=50, random_state=42) # Number of principal components
X_train_pca = pca.fit_transform(X_train)
X_test_pca = pca.transform(X_test)

Stage 2: PTNN-based Feature Transformation

ptnn = Sequential([
Dense(128, activation='relu', input_shape=(50,), kernel_initializer='glorot_uniform', bias_initializer='zeros'), # 50 is the number of PCA components
Dense(10, activation='softmax', kernel_initializer='glorot_uniform', bias_initializer='zeros') # 10 classes for digits 0-9
])

ptnn.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

Train the PTNN for a few epochs

ptnn.fit(X_train_pca, y_train, epochs=7, batch_size=32, validation_data=(X_test_pca, y_test), verbose=1)

Stop the training of PTNN

ptnn.stop_training = True

Transform features using PTNN

X_train_ptnn = ptnn.predict(X_train_pca)
X_test_ptnn = ptnn.predict(X_test_pca)

#

Initialize SVM classifier with seed

classifier = SVC(kernel='rbf', random_state=42, probability=True) # Enable probability estimates
classifier.fit(X_train_ptnn, y_train)

from sklearn.metrics import accuracy_score, classification_report

Predict digits on the test set using the trained models

y_pred = classifier.predict(X_test_ptnn)

Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)
print("Accuracy on test set:", accuracy)

Generate classification report

print("Classification Report:")
print(classification_report(y_test, y_pred))

def predict_digit(image):
# Convert image to grayscale
image_gray = image.convert('L')
# Resize image to 28x28
image_resized = image_gray.resize((28, 28))
# Convert image to array
img_array = np.array(image_resized)
# Invert the image (black background, white digit)
img_array_inverted = 255 - img_array
# Reshape array to 1D
img_array_1d = img_array_inverted.reshape(1, -1)
# Normalize pixel values
img_array_normalized = img_array_1d.astype('float32') / 255.0
# Apply PCA transformation
img_pca = pca.transform(img_array_normalized)
# Transform features using PTNN
img_ptnn = ptnn.predict(img_pca)
# Predict digit
scores = classifier.decision_function(img_ptnn)
proba = softmax(scores)
digit = np.argmax(proba)
# Get the probability for the predicted class
confidence = proba.max()
return digit, confidence, image_resized

#

def paint(event):
x1, y1 = (event.x - 10), (event.y - 10)
x2, y2 = (event.x + 10), (event.y + 10)
canvas.create_oval(x1, y1, x2, y2, fill="black", width=1)
draw.line([x1, y1, x2, y2], fill="black", width=1)

Function to clear canvas

def clear():
canvas.delete("all")
draw.rectangle([0, 0, 280, 280], fill="white")

def recognize_digit():
HWND = canvas.winfo_id()
rect = (canvas.winfo_rootx(), canvas.winfo_rooty(), canvas.winfo_rootx() + canvas.winfo_width(),
canvas.winfo_rooty() + canvas.winfo_height())
im = ImageGrab.grab(rect)

# Convert to grayscale
image_gray = im.convert('L')

# Resize and pad the image to match MNIST dimensions (28x28)
width, height = image_gray.size
if width > height:
    new_height = 28
    new_width = int(width * (28 / height))
else:
    new_width = 28
    new_height = int(height * (28 / width))
image_resized = image_gray.resize((new_width, new_height))
padded_image = Image.new('L', (28, 28), color=255)  # Create a white background
padded_image.paste(image_resized, ((28 - new_width) // 2, (28 - new_height) // 2))

# Invert colors (black digit on white background)
inverted_image = ImageOps.invert(padded_image)

# Convert image to array
img_array = np.array(inverted_image)

# Normalize pixel values
img_array_normalized = img_array.astype('float32') / 255.0
#

Reshape array to 1D

img_array_1d = img_array_normalized.flatten()

# Apply PCA transformation
img_pca = pca.transform(img_array_1d.reshape(1, -1))

# Transform features using PTNN
img_ptnn = ptnn.predict(img_pca)

# Predict digit
scores = classifier.decision_function(img_ptnn)
proba = softmax(scores)
digit = np.argmax(proba)
# Get the probability for the predicted class
confidence = proba.max()

# Display the recognized digit and its confidence level
label.config(text=f"Predicted Digit: {digit}, Confidence: {confidence:.2f}")

# Convert NumPy array to PIL Image
img_pil = Image.fromarray(img_array)

# Display the image
img_pil.show()

# Debugging info
print("PCA-transformed features:", img_pca)
print("Predicted digit:", digit)
print("Confidence:", confidence)

Create main window

root = Tk()
root.title("Handwritten Digit Recognition")

#

Create canvas

canvas = Canvas(root, width=280, height=280, bg="white")
canvas.pack()

Create image

image = Image.new("RGB", (280, 280), (255, 255, 255))
draw = ImageDraw.Draw(image)

Bind mouse events

canvas.bind("<B1-Motion>", paint)

Create buttons

btn_recognize = Button(text="Recognize Digit", command=recognize_digit)
btn_recognize.pack(side=BOTTOM)
btn_clear = Button(text="Clear", command=clear)
btn_clear.pack(side=BOTTOM)

Create label

label = Label(root, text="")
label.pack(side=BOTTOM)

root.mainloop()

lilac gazelleBOT
#

@thorn bolt

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.

#

๐Ÿ”’ wrong predictions