欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解

 更新時(shí)間:2020年01月10日 10:31:24   作者:water&12  
今天小編就為大家分享一篇關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解

MNIST的準(zhǔn)確率達(dá)到99.7%

用于MNIST的卷積神經(jīng)網(wǎng)絡(luò)(CNN)的實(shí)現(xiàn),具有各種技術(shù),例如數(shù)據(jù)增強(qiáng),丟失,偽隨機(jī)化等。

操作系統(tǒng):ubuntu18.04

顯卡:GTX1080ti

python版本:2.7(3.7)

網(wǎng)絡(luò)架構(gòu)

具有4層的CNN具有以下架構(gòu)。

輸入層:784個(gè)節(jié)點(diǎn)(MNIST圖像大?。?/p>

第一卷積層:5x5x32

第一個(gè)最大池層

第二卷積層:5x5x64

第二個(gè)最大池層

第三個(gè)完全連接層:1024個(gè)節(jié)點(diǎn)

輸出層:10個(gè)節(jié)點(diǎn)(MNIST的類數(shù))

用于改善CNN性能的工具

采用以下技術(shù)來改善CNN的性能。

1. Data augmentation

通過以下方式將列車數(shù)據(jù)的數(shù)量增加到5倍

隨機(jī)旋轉(zhuǎn):每個(gè)圖像在[-15°,+ 15°]范圍內(nèi)隨機(jī)旋轉(zhuǎn)。

隨機(jī)移位:每個(gè)圖像在兩個(gè)軸上隨機(jī)移動(dòng)一個(gè)范圍為[-2pix,+ 2pix]的值。

零中心歸一化:將像素值減去(PIXEL_DEPTH / 2)并除以PIXEL_DEPTH。

2. Parameter initializers

重量初始化器:xaiver初始化器

偏差初始值設(shè)定項(xiàng):常量(零)初始值設(shè)定項(xiàng)

3. Batch normalization

所有卷積/完全連接的層都使用批量標(biāo)準(zhǔn)化。

4. Dropout

The third fully-connected layer employes dropout technique.

5. Exponentially decayed learning rate

A learning rate is decayed every after one-epoch.

代碼部分

第一步:了解MNIST數(shù)據(jù)集

MNIST數(shù)據(jù)集是一個(gè)手寫體數(shù)據(jù)集,一共60000張圖片,所有的圖片都是28×28的,下載數(shù)據(jù)集的地址:數(shù)據(jù)集官網(wǎng)。這個(gè)數(shù)據(jù)集由四部分組成,分別是:

train-images-idx3-ubyte.gz: training set images (9912422 bytes) 
train-labels-idx1-ubyte.gz: training set labels (28881 bytes) 
t10k-images-idx3-ubyte.gz: test set images (1648877 bytes) 
t10k-labels-idx1-ubyte.gz: test set labels (4542 bytes)

也就是一個(gè)訓(xùn)練圖片集,一個(gè)訓(xùn)練標(biāo)簽集,一個(gè)測(cè)試圖片集,一個(gè)測(cè)試標(biāo)簽集;我們可以看出這個(gè)其實(shí)并不是普通的文本文件

或是圖片文件,而是一個(gè)壓縮文件,下載并解壓出來,我們看到的是二進(jìn)制文件。

第二步:加載MNIST數(shù)據(jù)集

先引入一些庫文件

import torchvision,torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt

加載MNIST數(shù)據(jù)集有很多方法:

方法一:在pytorch下可以直接調(diào)用torchvision.datasets里面的MNIST數(shù)據(jù)集(這是官方寫好的數(shù)據(jù)集類)

train = torchvision.datasets.MNIST(root='./mnist/',train=True, transform= transforms.ToTensor())

返回值為一個(gè)元組(train_data,train_target)(這個(gè)類使用的時(shí)候也有坑,必須用train[i]索引才能使用 transform功能)

一般是與torch.utils.data.DataLoader配合使用

dataloader = DataLoader(train, batch_size=50,shuffle=True, num_workers=4)
for step, (x, y) in enumerate(dataloader):
 b_x = x.shape
 b_y = y.shape
 print 'Step: ', step, '| train_data的維度' ,b_x,'| train_target的維度',b_y

如圖將60000張圖片的數(shù)據(jù)分為1200份,每份包含50張圖像,這樣并行處理數(shù)據(jù)能有效加快計(jì)算速度

看個(gè)人喜好,本人不太喜歡這種固定的數(shù)據(jù)類,所以想要靈活多變,可以開始自己寫數(shù)據(jù)集類

方法二:自己設(shè)置數(shù)據(jù)集

使用pytorch相關(guān)類,API對(duì)數(shù)據(jù)集進(jìn)行封裝,pytorch中數(shù)據(jù)集相關(guān)的類位于torch.utils.data package中。

本次實(shí)驗(yàn),主要使用以下類:

torch.utils.data.Dataset

torch.utils.data.DataLoader

Dataset類的使用: 所有的類都應(yīng)該是此類的子類(也就是說應(yīng)該繼承該類)。 所有的子類都要重寫(override) len(), getitem() 這兩個(gè)方法。

使用到的python package

python package 目的
numpy 矩陣操作,對(duì)圖像進(jìn)行轉(zhuǎn)置
skimage 圖像處理,圖像I/O,圖像變換
matplotlib 圖像的顯示,可視化
os 一些文件查找操作
torch pytorch
torvision pytorch

導(dǎo)入相關(guān)的包

import numpy as np
from skimage import io
from skimage import transform
import matplotlib.pyplot as plt
import os
import torch
import torchvision
from torch.utils.data import Dataset, DataLoader
from torchvision.transforms import transforms
from PIL import Image

第一步:

定義一個(gè)子類,繼承Dataset類, 重寫 __len()__, __getitem()__ 方法。

細(xì)節(jié):

1.數(shù)據(jù)集一個(gè)樣本的表示:采用字典的形式sample = {'img': img, 'target': target}。

圖像的讀?。翰捎胻orch.load進(jìn)行讀取,讀取之后的結(jié)果為torch.Tensor形式。

圖像變換:transform參數(shù)

class MY_MNIST(Dataset):
 training_file = 'training.pt'
 test_file = 'test.pt'
 def __init__(self, root, transform=None):
  self.transform = transform
  self.data, self.targets = torch.load(root)
 def __getitem__(self, index):
  img, target = self.data[index], int(self.targets[index])
  img = Image.fromarray(img.numpy(), mode='L')

  if self.transform is not None:
   img = self.transform(img)
  img =transforms.ToTensor()(img)

  sample = {'img': img, 'target': target}
  return sample
 def __len__(self):
  return len(self.data)
  
train = MY_MNIST(root='./mnist/MNIST/processed/training.pt', transform= None)

第二步

實(shí)例化一個(gè)對(duì)象,并讀取和顯示數(shù)據(jù)集

for (cnt,i) in enumerate(train):
 image = i['img']
 label = i['target']
 ax = plt.subplot(4, 4, cnt+1)
 # ax.axis('off')
 ax.imshow(image.squeeze(0))
 ax.set_title(label)
 plt.pause(0.001)
 if cnt ==15:
  break

輸出如下 ,這樣就表明,咱們自己寫的數(shù)據(jù)集讀取圖像,并讀取之后的結(jié)果為torch.Tensor形式成功啦!

第三步(可選 optional)

對(duì)數(shù)據(jù)集進(jìn)行變換:一般收集到的圖像大小尺寸,亮度等存在差異,變換的目的就是使得數(shù)據(jù)歸一化。另一方面,可以通過變換進(jìn)行數(shù)據(jù)增強(qiáng)

關(guān)于pytorch中的變換transforms,請(qǐng)參考該系列之前的文章

由于數(shù)據(jù)集中樣本采用字典dicts形式表示。 因此不能直接調(diào)用torchvision.transofrms中的方法。

本實(shí)驗(yàn)進(jìn)行了旋轉(zhuǎn),隨機(jī)裁剪,調(diào)節(jié)圖像的色彩飽和明暗等操作。

compose = transforms.Compose([
   transforms.Resize(20),
   transforms.RandomHorizontalFlip(),
   transforms.RandomCrop(20),
   transforms.ColorJitter(brightness=1, contrast=0.1, hue=0.5),
   # transforms.ToTensor(),
   # transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
   ])
train_transformed = MY_MNIST(root='./mnist/MNIST/processed/training.pt', transform= compose)

#顯示變換后的圖像
for (cnt,i) in enumerate(train_transformed):
 image = i['img']
 # print image[0].sum()
 # image = compose(image)
 print 'sdsdadfasfasfasf',type(image)
 label = i['target']
 ax = plt.subplot(4, 4, cnt+1)
 # ax.axis('off')
 ax.imshow(image.squeeze(0))
 ax.set_title(label)
 plt.pause(0.001)
 if cnt ==15:
  break

變換后的圖像,和之前對(duì)比,你發(fā)現(xiàn)了什么不同嗎?

第四步: 使用DataLoader進(jìn)行包裝

為何要使用DataLoader?

① 深度學(xué)習(xí)的輸入是mini_batch形式

② 樣本加載時(shí)候可能需要隨機(jī)打亂順序,shuffle操作

③ 樣本加載需要采用多線程

pytorch提供的DataLoader封裝了上述的功能,這樣使用起來更方便。

# 使用DataLoader可以利用多線程,batch,shuffle等
trainset_dataloader = DataLoader(dataset=transformed_trainset,
         batch_size=4,
         shuffle=True,
         num_workers=4)

可視化:

dataloader = DataLoader(train, batch_size=50,shuffle=True, num_workers=4)

通過DataLoader包裝之后,樣本以min_batch形式輸出,而且進(jìn)行了隨機(jī)打亂順序。

for step, i in enumerate(dataloader):
 b_x = i['img'].shape
 b_y = i['target'].shape
 print 'Step: ', step, '| train_data的維度' ,b_x,'| train_target的維度',b_y

如圖圖片大小已經(jīng)裁剪為20*20,而且并行處理讓60000個(gè)數(shù)據(jù)在3秒內(nèi)就能處理好,效率非常高

Step: 1186 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1187 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1188 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1189 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1190 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1191 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1192 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1193 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1194 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1195 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1196 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1197 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1198 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)
Step: 1199 | train_data的維度 (50, 1, 20, 20) | train_target的維度 (50,)

未完待續(xù)…

以上這篇關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python+selenium+chrome實(shí)現(xiàn)淘寶購物車秒殺自動(dòng)結(jié)算

    python+selenium+chrome實(shí)現(xiàn)淘寶購物車秒殺自動(dòng)結(jié)算

    這篇文章主要介紹了python+selenium+chrome實(shí)現(xiàn)淘寶購物車秒殺自動(dòng)結(jié)算,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Python使用Pandas處理測(cè)試數(shù)據(jù)的方法

    Python使用Pandas處理測(cè)試數(shù)據(jù)的方法

    Pandas是一個(gè)功能極其強(qiáng)大的數(shù)據(jù)分析庫,可以高效地操作各種數(shù)據(jù)集,這篇文章主要介紹了Python自動(dòng)化測(cè)試-使用Pandas來高效處理測(cè)試數(shù)據(jù),需要的朋友可以參考下
    2023-02-02
  • Python操作Jira庫常用方法解析

    Python操作Jira庫常用方法解析

    這篇文章主要介紹了Python操作Jira庫常用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • pycharm配置python環(huán)境的詳細(xì)圖文教程

    pycharm配置python環(huán)境的詳細(xì)圖文教程

    PyCharm是一款功能強(qiáng)大的Python編輯器,具有跨平臺(tái)性,下面這篇文章主要給大家介紹了關(guān)于pycharm配置python環(huán)境的詳細(xì)圖文教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01
  • Python實(shí)現(xiàn)提取和去除數(shù)據(jù)中包含關(guān)鍵詞的行

    Python實(shí)現(xiàn)提取和去除數(shù)據(jù)中包含關(guān)鍵詞的行

    這篇文章主要介紹了Python如何提取數(shù)據(jù)中包含關(guān)鍵詞的行已經(jīng)如何去除數(shù)據(jù)中包含關(guān)鍵詞的行,文中的示例代碼講解詳細(xì),需要的可以參考一下
    2023-08-08
  • pycharm右鍵沒有run,run不了問題的解決

    pycharm右鍵沒有run,run不了問題的解決

    這篇文章主要介紹了pycharm右鍵沒有run,run不了問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python基于pygame實(shí)現(xiàn)圖片代替鼠標(biāo)移動(dòng)效果

    Python基于pygame實(shí)現(xiàn)圖片代替鼠標(biāo)移動(dòng)效果

    這篇文章主要介紹了Python基于pygame實(shí)現(xiàn)圖片代替鼠標(biāo)移動(dòng)效果,可實(shí)現(xiàn)將鼠標(biāo)箭頭轉(zhuǎn)換成圖形的功能,涉及pygame圖形操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • 如何用GAN訓(xùn)練自己的數(shù)據(jù)生成新的圖片

    如何用GAN訓(xùn)練自己的數(shù)據(jù)生成新的圖片

    這篇文章主要介紹了如何用GAN訓(xùn)練自己的數(shù)據(jù)生成新的圖片問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Python 中的Sympy詳細(xì)使用

    Python 中的Sympy詳細(xì)使用

    這篇文章主要介紹了Python 中的Sympy詳細(xì)使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Python實(shí)現(xiàn)ATM系統(tǒng)

    Python實(shí)現(xiàn)ATM系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)ATM系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02

最新評(píng)論