mnist torch加载fashion_Pytorch加载并可视化FashionMNIST指定层(Udacity)
加載并可視化FashionMNIST
在這個notebook中,我們要加載并查看?Fashion-MNIST 數(shù)據(jù)庫中的圖像。
任何分類問題的第一步,都是查看你正在使用的數(shù)據(jù)集。這樣你可以了解有關(guān)圖像和標(biāo)簽格式的一些詳細(xì)信息,以及對如何定義網(wǎng)絡(luò)以識別此類圖像集中的模式的一些見解。
PyTorch有一些你可以使用的內(nèi)置數(shù)據(jù)集,而FashionMNIST就是其中之一,它已經(jīng)下載到了這個notebook中的data/目錄中,所以我們要做的就是使用FashionMNIST數(shù)據(jù)集類加載這些圖像,并使用DataLoader批量加載數(shù)據(jù)。
加載數(shù)據(jù)
數(shù)據(jù)集類和張量
torch.utils.data.Dataset是一個表示數(shù)據(jù)集的抽象類,而 FashionMNIST類是這個數(shù)據(jù)集類的擴展,它可以讓我們加載批量的圖像/標(biāo)簽數(shù)據(jù),并且統(tǒng)一地將變換應(yīng)用于我們的數(shù)據(jù),例如將所有圖像轉(zhuǎn)換為用于訓(xùn)練神經(jīng)網(wǎng)絡(luò)的張量。張量類似于numpy數(shù)組,但也可以在GPU上使用,用來加速計算 。
下面,讓我們看一看如何構(gòu)建訓(xùn)練數(shù)據(jù)集。
# our basic libraries
import torch
import torchvision
# data loading and transforming
from torchvision.datasets import FashionMNIST
from torch.utils.data import DataLoader
from torchvision import transforms
# The output of torchvision datasets are PILImage images of range [0, 1].
# We transform them to Tensors for input into a CNN
## Define a transform to read the data in as a tensor
data_transform = transforms.ToTensor()
# choose the training and test datasets
train_data = FashionMNIST(root='./data', train=True,
download=False, transform=data_transform)
# Print out some stats about the training data
print('Train data, number of images: ', len(train_data))
Train data, number of images: 60000
數(shù)據(jù)迭代與批處理
接下來,我們將要使用的是torch.utils.data.DataLoader,它是一個可以批量處理數(shù)據(jù)并置亂數(shù)據(jù)的迭代器。
在下一個單元格中,我們將數(shù)據(jù)置亂,并以大小為20的批量加載圖像/標(biāo)簽數(shù)據(jù)。
# prepare data loaders, set the batch_size
## TODO: you can try changing the batch_size to be larger or smaller
## when you get to training your network, see how batch_size affects the loss
batch_size = 20
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
# specify the image classes
classes = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
將一些訓(xùn)練數(shù)據(jù)可視化
這個單元格會遍歷該訓(xùn)練數(shù)據(jù)集,并使用dataiter.next()加載一個隨機批次的圖像/標(biāo)簽數(shù)據(jù)。然后,它會在2 x batch_size/2網(wǎng)格中將這批圖像和標(biāo)簽可視化。
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# obtain one batch of training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
images = images.numpy()
# plot the images in the batch, along with the corresponding labels
fig = plt.figure(figsize=(25, 4))
for idx in np.arange(batch_size):
ax = fig.add_subplot(2, batch_size/2, idx+1, xticks=[], yticks=[])
ax.imshow(np.squeeze(images[idx]), cmap='gray')
ax.set_title(classes[labels[idx]])
更詳細(xì)地查看圖像
該數(shù)據(jù)集中的每個圖像都是28x28像素且已歸一化的灰度圖像。
關(guān)于歸一化的說明
歸一化可以確保在訓(xùn)練CNN的過程中,先后經(jīng)歷前饋與反向傳播步驟時,每個圖像特征都將落入類似的值范圍內(nèi),而不是過度激活該網(wǎng)絡(luò)中的特定層。在前饋步驟期間,該神經(jīng)網(wǎng)絡(luò)會接收輸入圖像并將每個輸入像素乘以一些卷積濾波器權(quán)重并加上偏差,然后應(yīng)用一些激活和池化函數(shù)。如果沒有歸一化,反向傳播步驟中的計算梯度將會非常大,并且會導(dǎo)致我們的損失增加而不是收斂。
# select an image by index
idx = 2
img = np.squeeze(images[idx])
# display the pixel values in that image
fig = plt.figure(figsize = (12,12))
ax = fig.add_subplot(111)
ax.imshow(img, cmap='gray')
width, height = img.shape
thresh = img.max()/2.5
for x in range(width):
for y in range(height):
val = round(img[x][y],2) if img[x][y] !=0 else 0
ax.annotate(str(val), xy=(y,x),
horizontalalignment='center',
verticalalignment='center',
color='white' if img[x][y]
總結(jié)
以上是生活随笔為你收集整理的mnist torch加载fashion_Pytorch加载并可视化FashionMNIST指定层(Udacity)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python线程等待_python3 中
- 下一篇: python中文文本分析_python使
