Pytorch基础:数据读取与预处理——调用PyTorch官方数据集

本文介绍了如何从网络下载FashionMNIST数据集,并使用PyTorch库进行数据预处理,包括使用ToTensor转换,最后展示了数据集中的样本及其对应的标签进行可视化。
摘要由CSDN通过智能技术生成

数据读取与预处理——调用PyTorch官方数据集

1. 从网络端下载 FashionMNIST 数据集到本地

(base) PS C:\Users\孙明阳> conda activate yang
(yang) PS C:\Users\孙明阳> python
Python 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> from torchvision import datasets
>>> from torch.utils.data import Dataset
>>> from torchvision.transforms import ToTensor
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>>
>>> training_data = datasets.FashionMNIST(
...     root="data/FashionMNIST/",
...     train=True,
...     download=True,
...     transform=ToTensor()
... )
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\train-images-idx3-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\train-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\train-labels-idx1-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\train-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\t10k-images-idx3-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\t10k-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\t10k-labels-idx1-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\t10k-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

在这里插入图片描述

2. 数据集可视化

(base) PS C:\Users\阳> conda activate yang
(yang) PS C:\Users\阳> python
Python 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> from torchvision import datasets
>>> from torch.utils.data import Dataset
>>> from torchvision.transforms import ToTensor
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> training_data = datasets.FashionMNIST(
...     root="data/FashionMNIST/",
...     train=True,
...     download=True,
...     transform=ToTensor()
... )
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\train-images-idx3-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\train-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\train-labels-idx1-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\train-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\t10k-images-idx3-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\t10k-images-idx3-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz
Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw\t10k-labels-idx1-ubyte.gz
100.0%
Extracting data/FashionMNIST/FashionMNIST\raw\t10k-labels-idx1-ubyte.gz to data/FashionMNIST/FashionMNIST\raw

>>> 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=(7, 7))
>>> cols, rows = 3, 3
>>> # 根据数据集的数据量len(training_data),随机生成9个位置坐标
>>> positions = np.random.randint(0, len(training_data), (9,))
>>> for i in range(9):
...     img, label = training_data[positions[i]]
...     plt.subplot(rows, cols, i + 1)
...     plt.tight_layout(pad=0.05)
...     # 每个子图的标题设置为对应图像的标签
...     plt.title(labels_map[label])
...     plt.axis("off")
...     plt.imshow(img.squeeze(), cmap="gray")
>>> plt.savefig("D:\\fashion_mnist2.png")
>>> plt.show()

在这里插入图片描述

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MechMaster

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值