mnist torch加载fashion_Pytorch加载并可视化FashionMNIST指定层(Udacity)
加載并可視化FashionMNIST
在這個notebook中,我們要加載并查看?Fashion-MNIST 數據庫中的圖像。
任何分類問題的第一步,都是查看你正在使用的數據集。這樣你可以了解有關圖像和標簽格式的一些詳細信息,以及對如何定義網絡以識別此類圖像集中的模式的一些見解。
PyTorch有一些你可以使用的內置數據集,而FashionMNIST就是其中之一,它已經下載到了這個notebook中的data/目錄中,所以我們要做的就是使用FashionMNIST數據集類加載這些圖像,并使用DataLoader批量加載數據。
加載數據
數據集類和張量
torch.utils.data.Dataset是一個表示數據集的抽象類,而 FashionMNIST類是這個數據集類的擴展,它可以讓我們加載批量的圖像/標簽數據,并且統一地將變換應用于我們的數據,例如將所有圖像轉換為用于訓練神經網絡的張量。張量類似于numpy數組,但也可以在GPU上使用,用來加速計算 。
下面,讓我們看一看如何構建訓練數據集。
# 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
數據迭代與批處理
接下來,我們將要使用的是torch.utils.data.DataLoader,它是一個可以批量處理數據并置亂數據的迭代器。
在下一個單元格中,我們將數據置亂,并以大小為20的批量加載圖像/標簽數據。
# 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']
將一些訓練數據可視化
這個單元格會遍歷該訓練數據集,并使用dataiter.next()加載一個隨機批次的圖像/標簽數據。然后,它會在2 x batch_size/2網格中將這批圖像和標簽可視化。
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]])
更詳細地查看圖像
該數據集中的每個圖像都是28x28像素且已歸一化的灰度圖像。
關于歸一化的說明
歸一化可以確保在訓練CNN的過程中,先后經歷前饋與反向傳播步驟時,每個圖像特征都將落入類似的值范圍內,而不是過度激活該網絡中的特定層。在前饋步驟期間,該神經網絡會接收輸入圖像并將每個輸入像素乘以一些卷積濾波器權重并加上偏差,然后應用一些激活和池化函數。如果沒有歸一化,反向傳播步驟中的計算梯度將會非常大,并且會導致我們的損失增加而不是收斂。
# 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]
總結
以上是生活随笔為你收集整理的mnist torch加载fashion_Pytorch加载并可视化FashionMNIST指定层(Udacity)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python线程等待_python3 中
- 下一篇: cnpm安装webpack_Webpac