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()
相关推荐
开MINI的工科男15 分钟前
【笔记】自动驾驶预测与决策规划_Part3_路径与轨迹规划
人工智能·笔记·自动驾驶·预测与决策
AI原吾42 分钟前
掌握Python-uinput:打造你的输入设备控制大师
开发语言·python·apython-uinput
毕设木哥44 分钟前
25届计算机专业毕设选题推荐-基于python的二手电子设备交易平台【源码+文档+讲解】
开发语言·python·计算机·django·毕业设计·课程设计·毕设
weixin_455446171 小时前
Python学习的主要知识框架
开发语言·python·学习
D11_1 小时前
Pandas缺失值处理
python·机器学习·数据分析·numpy·pandas
花生了什么树~.2 小时前
python基础知识(四)--if语句,for\while循环
python
IT毕设梦工厂3 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
luthane3 小时前
python 实现average mean平均数算法
开发语言·python·算法
xuanyu223 小时前
Linux常用指令
linux·运维·人工智能
码农研究僧3 小时前
Flask 实现用户登录功能的完整示例:前端与后端整合(附Demo)
python·flask·用户登录