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()
相关推荐
矢量赛奇2 分钟前
比ChatGPT更酷的AI工具
人工智能·ai·ai写作·视频
KuaFuAI10 分钟前
微软推出的AI无代码编程微应用平台GitHub Spark和国产AI原生无代码工具CodeFlying比到底咋样?
人工智能·github·aigc·ai编程·codeflying·github spark·自然语言开发软件
明月清风徐徐17 分钟前
Scrapy爬取豆瓣电影Top250排行榜
python·selenium·scrapy
theLuckyLong18 分钟前
SpringBoot后端解决跨域问题
spring boot·后端·python
Make_magic19 分钟前
Git学习教程(更新中)
大数据·人工智能·git·elasticsearch·计算机视觉
Yongqiang Cheng21 分钟前
Python operator.itemgetter(item) and operator.itemgetter(*items)
python·operator·itemgetter
shelly聊AI24 分钟前
语音识别原理:AI 是如何听懂人类声音的
人工智能·语音识别
MavenTalk24 分钟前
Move开发语言在区块链的开发与应用
开发语言·python·rust·区块链·solidity·move
源于花海27 分钟前
论文学习(四) | 基于数据驱动的锂离子电池健康状态估计和剩余使用寿命预测
论文阅读·人工智能·学习·论文笔记
雷龙发展:Leah27 分钟前
离线语音识别自定义功能怎么用?
人工智能·音频·语音识别·信号处理·模块测试