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

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

  • [1. 从网络端下载 FashionMNIST 数据集到本地](#1. 从网络端下载 FashionMNIST 数据集到本地)
  • [2. 数据集可视化](#2. 数据集可视化)

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

csharp 复制代码
(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. 数据集可视化

csharp 复制代码
(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()
相关推荐
galileo201614 分钟前
LLM与金融
人工智能
DREAM依旧30 分钟前
隐马尔科夫模型|前向算法|Viterbi 算法
人工智能
ROBOT玲玉33 分钟前
Milvus 中,FieldSchema 的 dim 参数和索引参数中的 “nlist“ 的区别
python·机器学习·numpy
GocNeverGiveUp43 分钟前
机器学习2-NumPy
人工智能·机器学习·numpy
Kai HVZ1 小时前
python爬虫----爬取视频实战
爬虫·python·音视频
古希腊掌管学习的神1 小时前
[LeetCode-Python版]相向双指针——611. 有效三角形的个数
开发语言·python·leetcode
m0_748244832 小时前
StarRocks 排查单副本表
大数据·数据库·python
B站计算机毕业设计超人2 小时前
计算机毕业设计PySpark+Hadoop中国城市交通分析与预测 Python交通预测 Python交通可视化 客流量预测 交通大数据 机器学习 深度学习
大数据·人工智能·爬虫·python·机器学习·课程设计·数据可视化
路人甲ing..2 小时前
jupyter切换内核方法配置问题总结
chrome·python·jupyter
学术头条2 小时前
清华、智谱团队:探索 RLHF 的 scaling laws
人工智能·深度学习·算法·机器学习·语言模型·计算语言学