数据读取与预处理——调用PyTorch官方数据集
1. 从网络端下载 FashionMNIST 数据集到本地 2. 数据集可视化
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 ( )