Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Science
  • Data Science Projects
  • Data Analysis
  • Data Visualization
  • Machine Learning
  • ML Projects
  • Deep Learning
  • NLP
  • Computer Vision
  • Artificial Intelligence
Open In App
Next Article:
How to load CIFAR10 Dataset in Pytorch?
Next article icon

How to load Fashion MNIST dataset using PyTorch?

Last Updated : 14 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In machine learning, datasets are essential because they serve as benchmarks for comparing and assessing the performance of different algorithms. Fashion MNIST is one such dataset that replaces the standard MNIST dataset of handwritten digits with a more difficult format. The article explores the Fashion MNIST dataset, including its characteristics, uses, and how can we load it using PyTorch.


What is Fashion MNIST?

Fashion-MNIST is a dataset developed by Zalando Research as a modern alternative to the original MNIST dataset. It comprises 70,000 grayscale images categorized into 10 fashion-related items. Each image is 28x28 pixels, providing a uniform format for machine learning model input. The dataset is divided into a training set of 60,000 images and a test set of 10,000 images.

The ten categories in Fashion MNIST are:

  1. T-shirt/top
  2. Trouser
  3. Pullover
  4. Dress
  5. Coat
  6. Sandal
  7. Shirt
  8. Sneaker
  9. Bag
  10. Ankle boot

Characteristics of Fashion MNIST Dataset

Here are the key characteristics of the Fashion-MNIST dataset in bullet points:

  • Images are preprocessed and normalized, with pixel values ranging from 0 to 255.
  • Fashion-MNIST introduces real-world complexity with variations in lighting, pose, and background clutter.
  • The dataset exhibits class imbalance, with some categories having more images than others.

Load Fashion MNIST dataset in PyTorch

The 'torchvision.datasets.FashionMNIST()' function is used to load the FashionMNIST dataset in PyTorch.

torchvision.datasets.FashionMNIST(root: Union[str, Path], train: bool = True, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, download: bool = False)

Breakdown of Parameters:

  1. root: Location where the dataset is stored or will be stored. Can be a directory path or a Path object.
  2. train: Indicates whether to load the training set (`True`) or the test set (`False`). Default is training set.
  3. transform: Transformation to apply to the samples (images). Can be a single transformation or a composition of multiple transformations. Default is no transformation.
  4. target_transform: Transformation to apply to the targets (labels). Used to preprocess the labels. Default is no transformation.
  5. download: Specifies whether to download the dataset if it's not already downloaded. Default is `False`, meaning it won't download if dataset exists locally.

Loading Fashion MNIST dataset using PyTorch in Python

In the following code, we have loaded the Fashion MNIST dataset using PyTorch and displayed 4x4 grid of images with their labels.

For loading the Fashion MNIST dataset, we have followed these steps:

  1. Imported necessary libraries including PyTorch, torchvision, and matplotlib.
  2. Defined a transformation using transforms.ToTensor() to convert the images into PyTorch tensors.
  3. Load the FashionMNIST dataset using torchvision.datasets.FashionMNIST(). It downloads the dataset if it's not already downloaded and applies the defined transformation.
  4. Created 4x4 grid of subplots and set the title of each subplot with corresponding labels and turn off the axis.
Python
import torch import torchvision import torchvision.transforms as transforms import matplotlib.pyplot as plt  # Define the transformation transform = transforms.ToTensor()  # Load the dataset train_dataset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform)  # Create a subplot with 4x4 grid fig, axs = plt.subplots(4, 4, figsize=(8, 8))  # Loop through each subplot and plot an image for i in range(4):     for j in range(4):         image, label = train_dataset[i * 4 + j]  # Get image and label         image_numpy = image.numpy().squeeze()    # Convert image tensor to numpy array         axs[i, j].imshow(image_numpy, cmap='gray')  # Plot the image         axs[i, j].axis('off')  # Turn off axis         axs[i, j].set_title(f"Label: {label}")  # Set title with label  plt.tight_layout()  # Adjust layout plt.show()  # Show plot 

Output:

download-(8)



Next Article
How to load CIFAR10 Dataset in Pytorch?

D

deepakp7eq
Improve
Article Tags :
  • Blogathon
  • Deep Learning
  • AI-ML-DS
  • Python-PyTorch
  • AI-ML-DS With Python
  • DataSets
  • Data Science Blogathon 2024

Similar Reads

  • How to Split a Dataset Using PyTorch
    Splitting a dataset is an important step in training machine learning models. It helps to separate the data into different sets, typically training, and validation, so we can train our model on one set and validate its performance on another. In this article, we are going to discuss the process of s
    6 min read
  • Build the Model for Fashion MNIST dataset Using TensorFlow in Python
    The primary objective will be to build a classification model which will be able to identify the different categories of the fashion industry from the Fashion MNIST dataset using Tensorflow and Keras To complete our objective, we will create a CNN model to identify the image categories and train it
    5 min read
  • How to implement Genetic Algorithm using PyTorch
    The optimization algorithms are capable of solving complex problems and genetic algorithm is one of the optimization algorithm. Genetic Algorithm can be easily integrate with PyTorch to address a wide array of optimization tasks. We will understand how to implement Genetic Algorithm using PyTorch. G
    8 min read
  • How to load CIFAR10 Dataset in Pytorch?
    The CIFAR-10 dataset is a popular resource for training machine learning models, especially in the field of image recognition. It consists of 60,000 32x32 color images in 10 different classes, with 6,000 images per class. The dataset is divided into 50,000 training images and 10,000 testing images.
    3 min read
  • How to Fix "fast.ai Not Using the GPU"?
    Fast.ai is one of the leading deep-learning frameworks built on top of PyTorch.e. The library provides high-level components that make it easy to build and train neural networks. This library simplifies the deep learning process making the process easy in high-level API building for building and tra
    4 min read
  • How to use a DataLoader in PyTorch?
    Operating with large datasets requires loading them into memory all at once. In most cases, we face a memory outage due to the limited amount of memory available in the system. Also, the programs tend to run slowly due to heavy datasets loaded once. PyTorch offers a solution for parallelizing the da
    2 min read
  • How to import datasets using sklearn in PyBrain
    In this article, we will discuss how to import datasets using sklearn in PyBrain Dataset: A Dataset is defined as the set of data that is can be used to test, validate, and train on networks. On comparing it with arrays, a dataset is considered more flexible and easy to use. A dataset resembles a 2-
    2 min read
  • Load a Computer Vision Dataset in PyTorch
    Computer vision is a subset of Artificial Intelligence that gives the ability to the computer to understand images. In Deep Learning, Convolution Neural Network is used to process the image. For building the good we need a lot of images to process. There are several ways to load a computer vision da
    3 min read
  • How to use GPU acceleration in PyTorch?
    PyTorch is a well-liked deep learning framework that offers good GPU acceleration support, enabling users to take advantage of GPUs' processing power for quicker neural network training. This post will discuss the advantages of GPU acceleration, how to determine whether a GPU is available, and how t
    7 min read
  • How to create a custom Loss Function in PyTorch?
    Choosing the appropriate loss function is crucial in deep learning. It serves as a guide for directing the optimization process of neural networks while they are being trained. Although PyTorch offers many pre-defined loss functions, there are cases where regular loss functions are not enough. In th
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences