<jupyter_start><jupyter_text>Reading the Dataset<jupyter_code># download and read the Fashion-MNIST dataset into memory via the build-in functions in the framework
mnist_train = gluon.data.vision.FashionMNIST(train=True)
mnist_test = gluon.data.vision.FashionMNIST(train=False)
len(mnist_train), len(mnist_test)
mnist_train[0][0].shape
# converts between numeric label indices and their names in text
def get_fashion_mnist_labels(labels):
    """Return text labels for the Fashion-MNIST dataset."""
    text_labels = ['t-shirt', 'trouser', 'pullover', 'dress', 'coat',
                   'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']
    return [text_labels[int(i)] for i in labels]
# function to visualize the examples
def show_images(imgs, num_rows, num_cols, titles=None, scale=1.5):
    """Plot a list of images."""
    figsize = (num_cols * scale, num_rows * scale)
    _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize)
    axes = axes.flatten()
    for i, (ax, img) in enumerate(zip(axes, imgs)):
        ax.imshow(img.asnumpy())
        ax.axes.get_xaxis().set_visible(False)
        ax.axes.get_yaxis().set_visible(False)
        if titles:
            ax.set_title(titles[i])
    return axes
!pip uninstall matplotlib
!pip install --upgrade matplotlib
# the images and their corresponding labels
X, y = mnist_train[:18]

print(X.shape)
show_images(X.squeeze(axis=-1), 2, 9, titles=get_fashion_mnist_labels(y));<jupyter_output>(18, 28, 28, 1)<jupyter_text>Reading a Minibatch<jupyter_code># reads a minibatch of data with size batch_size each time
batch_size = 256

def get_dataloader_workers():
    """Use 4 processes to read the data except for Windows."""
    return 0 if sys.platform.startswith('win') else 4

# `ToTensor` converts the image data from uint8 to 32-bit floating point. It
# divides all numbers by 255 so that all pixel values are between 0 and 1
transformer = gluon.data.vision.transforms.ToTensor()
train_iter = gluon.data.DataLoader(mnist_train.transform_first(transformer),
                                   batch_size, shuffle=True,
                                   num_workers=get_dataloader_workers())
# the time it takes to read the training data
timer = d2l.Timer()
for X, y in train_iter:
    continue
f'{timer.stop():.2f} sec'<jupyter_output><empty_output><jupyter_text>Putting All Things Together<jupyter_code># define the load_data_fashion_mnist function that obtains and reads the Fashion-MNIST dataset
def load_data_fashion_mnist(batch_size, resize=None):
    """Download the Fashion-MNIST dataset and then load it into memory."""
    dataset = gluon.data.vision
    trans = [dataset.transforms.ToTensor()]
    if resize:
        trans.insert(0, dataset.transforms.Resize(resize))
    trans = dataset.transforms.Compose(trans)
    mnist_train = dataset.FashionMNIST(train=True).transform_first(trans)
    mnist_test = dataset.FashionMNIST(train=False).transform_first(trans)
    return (gluon.data.DataLoader(mnist_train, batch_size, shuffle=True,
                                  num_workers=get_dataloader_workers()),
            gluon.data.DataLoader(mnist_test, batch_size, shuffle=False,
                                  num_workers=get_dataloader_workers()))
# image resizing
train_iter, test_iter = load_data_fashion_mnist(32, resize=64)
for X, y in train_iter:
    print(X.shape, X.dtype, y.shape, y.dtype)
    break<jupyter_output>(32, 1, 64, 64) <class 'numpy.float32'> (32,) <class 'numpy.int32'>
{'fraction_non_alphanumeric': 0.1103110599078341, 'fraction_numerical': 0.0146889400921659, 'mean_word_length': 3.975644699140401, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '375141', 'license': 'Unlicense', 'n_tokens_mistral': 1215, 'n_tokens_neox': 1119, 'n_words': 287, 'path': 'Week 9/3.5. The Image Classification Dataset.ipynb', 'repo_name': 'Hanif-2610/Machine-Learning-Homework-Project', 'size': '3472'}