print("Hello World")
import tensorflow as tf
# Define the paths to your dataset directories
train_dir = 'Data\MNIST\MNIST - JPG - training'
test_dir = 'Data\MNIST\MNIST - JPG - testing'
# Define image size and batch size
IMG_HEIGHT = 28 # Adjust if your images are different size
IMG_WIDTH = 28  # Adjust if your images are different size
BATCH_SIZE = 32
tf.keras.utils.image_dataset_from_directory(
    train_dir
)
a = train_ds.take(1).as_numpy_iterator().next()
#label 
image  = a[0][0]
label = a[1][0]
image = image.astype('uint8')
import matplotlib.pyplot as plt
plt.imshow(image, cmap='gray')
plt.axis('off')  # Hide axes
plt.title(f'Label: {label}')
plt.show()
# Display the first batch of images and labels
import matplotlib.pyplot as plt
import numpy as np
import os

def display_images(images, labels):
    plt.figure(figsize=(10, 10))
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(int(labels[i]))
        plt.axis("off")
    plt.show()

# Load the dataset
train_ds = tf.keras.utils.image_dataset_from_directory(
    train_dir,
)
# Get a batch of images and labels
for images, labels in train_ds.take(1):
    display_images(images, labels)

help(tf.keras.utils.image_dataset_from_directory)
# Load the training dataset
train_dataset = tf.keras.utils.image_dataset_from_directory(
    train_dir,
    labels='inferred',       # Infer labels from directory structure
    label_mode='int',        # Labels are integers (0-9)
    image_size=(IMG_HEIGHT, IMG_WIDTH),
    interpolation='nearest', # Use 'nearest' for MNIST-like images
    batch_size=BATCH_SIZE,
    shuffle=True,            # Shuffle training data
    color_mode='grayscale'   # MNIST images are grayscale
)
# Load the testing dataset
test_dataset = tf.keras.utils.image_dataset_from_directory(
    test_dir,
    labels='inferred',
    label_mode='int',
    image_size=(IMG_HEIGHT, IMG_WIDTH),
    interpolation='nearest',
    batch_size=BATCH_SIZE,
    shuffle=False,           # No need to shuffle test data
    color_mode='grayscale'
)
# You can optionally print the class names found
class_names = train_dataset.class_names
print("Class names found:", class_names)
# Example: Iterate over the first batch of the training dataset
for images, labels in train_dataset.take(1):
    print("Images batch shape:", images.shape)
    print("Labels batch shape:", labels.shape)
