import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor, Lambda
from torch.utils.data import DataLoader
import matplotlib
import matplotlib.pyplot as plt

def load_data():
  
  # Loading a Dataset
  datadir = '../../nn_data'
  training_data = datasets.FashionMNIST(root=datadir, train=True, download=True, transform=ToTensor())
  test_data = datasets.FashionMNIST(root=datadir, train=False, download=True, transform=ToTensor())

  # Iterating and Visualizing the Dataset
  labels_map = {
    0: "T-Shirt",
    1: "Trouser",
    2: "Pullover",
    3: "Dress",
    4: "Coat",
    5: "Sandal",
    6: "Shirt",
    7: "Sneaker",
    8: "Bag",
    9: "Ankle Boot",
  }
  figure = plt.figure(figsize=(8, 8))
  cols, rows = 3, 3
  for i in range(1, cols * rows + 1):
      sample_idx = torch.randint(len(training_data), size=(1,)).item()
      img, label = training_data[sample_idx]
      figure.add_subplot(rows, cols, i)
      plt.title(labels_map[label])
      plt.axis("off")
      plt.imshow(img.squeeze(), cmap="gray")

  plt.show()
  
  train_dataloader = DataLoader(training_data, batch_size=64, shuffle=True)
  test_dataloader = DataLoader(test_data, batch_size=64, shuffle=True)

  # Display image and label.
  train_features, train_labels = next(iter(train_dataloader))
  print(f"Feature batch shape: {train_features.size()}")
  print(f"Labels batch shape: {train_labels.size()}")
  img = train_features[0].squeeze()
  label = train_labels[0]
  plt.imshow(img, cmap="gray")
  plt.show()
  print(f"Label: {label}")

if __name__ == '__main__':
  load_data()