Having Problems While Doing Digit Recognition in OpenCV Using Tensorflow Mnist
@deringezgin I would recommend using a `Convolutional Neural Network(CNN)` based architecture for your digit classifier. It should boost your performance by extracting better spatial features. A good [tutorial](https://www.tensorflow.org/tutorials/images/cnn) for getting started with CNN's.</br> Let me know if you have any other questions! > @deringezgin I would recommend using a `Convolutional Neural Network(CNN)` based architecture for your digit classifier. It should boost your performance by extracting better spatial features. A good [tutorial](https://www.tensorflow.org/tutorials/images/cnn) for getting started with CNN's.</br> Let me know if you have any other questions!

Hi, Thank you for your response. Maybe a beginner question but how can I train the network with number images? When I checked the link you provided, I can only see other objects like car, plane etc. @deringezgin CNN's are highly versatile and work with all kinds of image data .</br>Just ensure your CNN is adapted to handle your resized 28 x 28 images. You can find many `Digit Classifier` Tutorials on the [Internet](https://machinelearningmastery.com/how-to-develop-a-convolutional-neural-network-from-scratch-for-mnist-handwritten-digit-classification/)<br/>Hope this helps! This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. @deringezgin,
Convolutional Neural Networks (CNNs) are the good choice for image recognition tasks. Use convolutional layers to extract features from the images and Add pooling layers for dimensionality reduction. Also employ fully-connected layers for final classification or regression.

Could you please refer to this official document for the reference.
https://www.tensorflow.org/tutorials/images/cnn

Thank you!

 This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62798">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/62798">No</a>

### Issue type

Support

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

source

### TensorFlow version

2.15.0

### Custom code

Yes

### OS platform and distribution

MacOS Sonoma 14.2.1

### Mobile device

_No response_

### Python version

3.11.7

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

Apple M2 Max

### Current behavior?

Currently I'm learning OpenCV and decided to make a project to practice my skills. In my project, I'm aiming to read a sudoku table, divide it into individual cells, and recognize the numbers. The rest is mostly sudoku solving.

Right now, I'm stuck on recognizing the numbers part. I decided to use mnist dataset to train the model. For a reason that I couldn't figure out, the program always recognizes the digit wrong. 

[First form of my Image](https://i.stack.imgur.com/9bHhp.png)
[My image after rescaling](https://i.stack.imgur.com/qOb0C.png)

I thought that the problem was with the way that I trained the model. Because I transformed my huge image to 28x28, it blurred the image a lot. I thought that that was the problem. The code that I added below trains the model in 300x300 form. It's definitely slower but I thought that It's more safe. Unfortunately, the result didn't change. 

The code below also outputs the probability for every class. I input an image (which I gave the link to) with the number 6, but it doesn't work. and the model guesses it as 8. I don't know to source of the problem. I hope that you can help me.

### Standalone code to reproduce the issue

```shell
sample = splitted_blocks[0][0][6]
size = 300
(ds_train, ds_test), ds_info = tfds.load(
    'mnist',
    split=['train', 'test'],
    shuffle_files=True,
    as_supervised=True,
    with_info=True,
)


def resize_img(image, label):
    return tf.image.resize(image, (size, size)), label


ds_train = ds_train.map(resize_img, num_parallel_calls=tf.data.AUTOTUNE)
ds_test = ds_test.map(resize_img, num_parallel_calls=tf.data.AUTOTUNE)


def normalize_img(image, label):
    """Normalizes images: `uint8` -> `float32`."""
    return tf.cast(image, tf.float32) / 255., label


ds_train = ds_train.map(normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
ds_train = ds_train.batch(128)
ds_train = ds_train.prefetch(tf.data.AUTOTUNE)

ds_test = ds_test.map(normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
ds_test = ds_test.batch(128)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.AUTOTUNE)

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(size, size, 1)),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10)
])
model.compile(
    optimizer=tf.keras.optimizers.legacy.Adam(0.001),
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)

model.fit(
    ds_train,
    epochs=6,
    validation_data=ds_test,
)
sample = cv.cvtColor(sample, cv.COLOR_BGR2GRAY)
sample = cv.resize(sample, (size, size))

view_image(sample)
sample = np.invert(np.array([sample]))

prediction = model.predict(sample)
print(np.argmax(prediction))

for class_index, prob in enumerate(prediction[0]):
    print(f'Class {class_index}: Probability {prob}')

sample = np.invert(np.array([sample])).reshape((size, size, 1))
cv.imshow("test", sample)
cv.waitKey(0)
```


### Relevant log output

```shell
Epoch 1/6
469/469 [==============================] - 20s 36ms/step - loss: 0.3859 - sparse_categorical_accuracy: 0.9090 - val_loss: 0.1917 - val_sparse_categorical_accuracy: 0.9432
Epoch 2/6
469/469 [==============================] - 17s 37ms/step - loss: 0.1866 - sparse_categorical_accuracy: 0.9472 - val_loss: 0.1665 - val_sparse_categorical_accuracy: 0.9530
Epoch 3/6
469/469 [==============================] - 17s 37ms/step - loss: 0.1414 - sparse_categorical_accuracy: 0.9585 - val_loss: 0.1636 - val_sparse_categorical_accuracy: 0.9548
Epoch 4/6
469/469 [==============================] - 17s 37ms/step - loss: 0.1215 - sparse_categorical_accuracy: 0.9642 - val_loss: 0.1735 - val_sparse_categorical_accuracy: 0.9563
Epoch 5/6
469/469 [==============================] - 17s 37ms/step - loss: 0.1060 - sparse_categorical_accuracy: 0.9676 - val_loss: 0.1444 - val_sparse_categorical_accuracy: 0.9615
Epoch 6/6
469/469 [==============================] - 17s 37ms/step - loss: 0.1040 - sparse_categorical_accuracy: 0.9688 - val_loss: 0.1410 - val_sparse_categorical_accuracy: 0.9630
1/1 [==============================] - 0s 59ms/step
8
Class 0: Probability 144.3349151611328
Class 1: Probability 2138.992431640625
Class 2: Probability 4751.95068359375
Class 3: Probability 4140.23583984375
Class 4: Probability -2975.758544921875
Class 5: Probability 2814.78662109375
Class 6: Probability 1327.113525390625
Class 7: Probability 2143.239990234375
Class 8: Probability 4821.7548828125
Class 9: Probability -3011.857666015625
```
