How can TensorFlow be used to download and explore the Fashion MNIST dataset using Python?


Tensorflow is a machine learning framework that is provided by Google. It is an open−source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.

The ‘tensorflow’ package can be installed on Windows using the below line of code −

pip install tensorflow

The ‘Fashion MNIST’ dataset contains images of clothing of different kinds. It contains grayscale images of more than 70 thousand clothes that belong to 10 different categories. These images are of low resolution (28 x 28 pixels).

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Following is the code −

Example

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

print("The tensorflow version used is ")
print(tf.__version__)
print("The dataset is being loaded")
fashion_mnist = tf.keras.datasets.fashion_mnist
print("The dataset is being classified into training and testing data ")
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

print("The dimensions of training data ")          
print(train_images.shape)

print("The number of rows in the training data")
print(len(train_labels))

print("The column names of dataset")
print(train_labels)
print("The dimensions of test data ")          
print(test_images.shape)
print("The number of rows in the test data")
print(len(test_labels))

Code credit https://www.tensorflow.org/tutorials/keras/classification

Output

The tensorflow version used is
2.4.0
The dataset is being loaded
The dataset is being classified into training and testing data
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
The dimensions of training data
(60000, 28, 28)
The number of rows in the training data
60000
The column names of dataset
[9 0 0 ... 3 0 5]
The dimensions of test data
(10000, 28, 28)
The number of rows in the test data
10000

Explanation

  • The required packages are imported.

  • The version of Tensorflow being used is determined.

  • The Fashion MNIST dataset is loaded, and the Fashion MNIST dataset can be accessed directly from TensorFlow.

  • Next, the data is split into training and testing datasets.

  • There are totally 70000 rows in the dataset, out of which 60k images are used for training and 10k is used to evaluate how well the model has learnt to classify the images into different labels.

  • This is a classification problem wherein every image from the dataset is given a specific label.

  • These images are of clothing, and respective labels are assigned to them.

  • The shape, the number of rows in the training and test dataset, and the column names in the dataset are displayed on the console.

Updated on: 20-Jan-2021

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements