最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例)

 更新時間:2020年01月18日 16:06:16   作者:sjtu_leexx  
今天小編就為大家分享一篇pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

本文將原始的numpy array數(shù)據(jù)在pytorch下封裝為Dataset類的數(shù)據(jù)集,為后續(xù)深度網(wǎng)絡(luò)訓(xùn)練提供數(shù)據(jù)。

加載并保存圖像信息

首先導(dǎo)入需要的庫,定義各種路徑。

import os
import matplotlib
from keras.datasets import mnist
import numpy as np
from torch.utils.data.dataset import Dataset
from PIL import Image
import scipy.misc

root_path = 'E:/coding_ex/pytorch/Alexnet/data/'
base_path = 'baseset/'
training_path = 'trainingset/'
test_path = 'testset/'

這里將數(shù)據(jù)集分為三類,baseset為所有數(shù)據(jù)(trainingset+testset),trainingset是訓(xùn)練集,testset是測試集。直接通過keras.dataset加載mnist數(shù)據(jù)集,不能自動下載的話可以手動下載.npz并保存至相應(yīng)目錄下。

def LoadData(root_path, base_path, training_path, test_path):
  (x_train, y_train), (x_test, y_test) = mnist.load_data()
  x_baseset = np.concatenate((x_train, x_test))
  y_baseset = np.concatenate((y_train, y_test))
  train_num = len(x_train)
  test_num = len(x_test)
  
  #baseset
  file_img = open((os.path.join(root_path, base_path)+'baseset_img.txt'),'w')
  file_label = open((os.path.join(root_path, base_path)+'baseset_label.txt'),'w')
  for i in range(train_num + test_num):
    file_img.write(root_path + base_path + 'img/' + str(i) + '.png\n') #name
    file_label.write(str(y_baseset[i])+'\n') #label
#    scipy.misc.imsave(root_path + base_path + '/img/'+str(i) + '.png', x_baseset[i])
    matplotlib.image.imsave(root_path + base_path + 'img/'+str(i) + '.png', x_baseset[i])
  file_img.close()
  file_label.close()
  
  #trainingset
  file_img = open((os.path.join(root_path, training_path)+'trainingset_img.txt'),'w')
  file_label = open((os.path.join(root_path, training_path)+'trainingset_label.txt'),'w')
  for i in range(train_num):
    file_img.write(root_path + training_path + 'img/' + str(i) + '.png\n') #name
    file_label.write(str(y_train[i])+'\n') #label
#    scipy.misc.imsave(root_path + training_path + '/img/'+str(i) + '.png', x_train[i])
    matplotlib.image.imsave(root_path + training_path + 'img/'+str(i) + '.png', x_train[i])
  file_img.close()
  file_label.close()
  
  #testset
  file_img = open((os.path.join(root_path, test_path)+'testset_img.txt'),'w')
  file_label = open((os.path.join(root_path, test_path)+'testset_label.txt'),'w')
  for i in range(test_num):
    file_img.write(root_path + test_path + 'img/' + str(i) + '.png\n') #name
    file_label.write(str(y_test[i])+'\n') #label
#    scipy.misc.imsave(root_path + test_path + '/img/'+str(i) + '.png', x_test[i])
    matplotlib.image.imsave(root_path + test_path + 'img/'+str(i) + '.png', x_test[i])
  file_img.close()
  file_label.close()

使用這段代碼時,需要建立相應(yīng)的文件夾及.txt文件,./data文件夾結(jié)構(gòu)如下:

/img文件夾

由于mnist數(shù)據(jù)集其實(shí)是灰度圖,這里用matplotlib保存的圖像是偽彩色圖像。

如果用scipy.misc.imsave的話保存的則是灰度圖像。

xxx_img.txt文件

xxx_img.txt文件中存放的是每張圖像的名字

xxx_label.txt文件

xxx_label.txt文件中存放的是類別標(biāo)記

這里記得保存的時候一行為一個圖像信息,便于后續(xù)讀取。

定義自己的Dataset類

pytorch訓(xùn)練數(shù)據(jù)時需要數(shù)據(jù)集為Dataset類,便于迭代等等,這里將加載保存之后的數(shù)據(jù)封裝成Dataset類,繼承該類需要寫初始化方法(__init__),獲取指定下標(biāo)數(shù)據(jù)的方法__getitem__),獲取數(shù)據(jù)個數(shù)的方法(__len__)。這里尤其需要注意的是要把label轉(zhuǎn)為LongTensor類型的。

class DataProcessingMnist(Dataset):
  def __init__(self, root_path, imgfile_path, labelfile_path, imgdata_path, transform = None):
    self.root_path = root_path
    self.transform = transform
    self.imagedata_path = imgdata_path
    img_file = open((root_path + imgfile_path),'r')
    self.image_name = [x.strip() for x in img_file]
    img_file.close()
    label_file = open((root_path + labelfile_path), 'r')
    label = [int(x.strip()) for x in label_file]
    label_file.close()
    self.label = torch.LongTensor(label)#這句很重要,一定要把label轉(zhuǎn)為LongTensor類型的
    
  def __getitem__(self, idx):
    image = Image.open(str(self.image_name[idx]))
    image = image.convert('RGB')
    if self.transform is not None:
      image = self.transform(image)
    label = self.label[idx]
    return image, label
  def __len__(self):
    return len(self.image_name)

定義完自己的類之后可以測試一下。

  LoadData(root_path, base_path, training_path, test_path)
  training_imgfile = training_path + 'trainingset_img.txt'
  training_labelfile = training_path + 'trainingset_label.txt'
  training_imgdata = training_path + 'img/'
  #實(shí)例化一個類
  dataset = DataProcessingMnist(root_path, training_imgfile, training_labelfile, training_imgdata)

得到圖像名稱

name = dataset.image_name

這里我們可以單獨(dú)輸出某一個名稱看一下是否有換行符

print(name[0])
>>>'E:/coding_ex/pytorch/Alexnet/data/trainingset/img/0.png'

如果定義類的時候self.image_name = [x.strip() for x in img_file]這句沒有strip掉,則輸出的值將為'E:/coding_ex/pytorch/Alexnet/data/trainingset/img/0.png\n'

獲取固定下標(biāo)的圖像

im, label = dataset.__getitem__(0)

得到結(jié)果

以上這篇pytorch實(shí)現(xiàn)建立自己的數(shù)據(jù)集(以mnist為例)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解requirements.txt的生成和安裝

    詳解requirements.txt的生成和安裝

    本文主要介紹了詳解requirements.txt的生成和安裝,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • python優(yōu)化測試穩(wěn)定性的失敗重試工具pytest-rerunfailures詳解

    python優(yōu)化測試穩(wěn)定性的失敗重試工具pytest-rerunfailures詳解

    筆者在執(zhí)行自動化測試用例時,會發(fā)現(xiàn)有時候用例失敗并非代碼問題,而是由于服務(wù)正在發(fā)版,導(dǎo)致請求失敗,從而降低了自動化用例的穩(wěn)定性,那該如何增加失敗重試機(jī)制呢?帶著問題我們一起探索
    2023-10-10
  • 一文教會你用Python繪制動態(tài)可視化圖表

    一文教會你用Python繪制動態(tài)可視化圖表

    數(shù)據(jù)可視化是數(shù)據(jù)科學(xué)中關(guān)鍵的一步,下面這篇文章主要給大家介紹了關(guān)于如何利用Python繪制動態(tài)可視化圖表的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • 解決python flask中config配置管理的問題

    解決python flask中config配置管理的問題

    今天小編就為大家分享一篇解決python flask中config配置管理的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python爬蟲實(shí)戰(zhàn)之最簡單的網(wǎng)頁爬蟲教程

    python爬蟲實(shí)戰(zhàn)之最簡單的網(wǎng)頁爬蟲教程

    在我們?nèi)粘I暇W(wǎng)瀏覽網(wǎng)頁的時候,經(jīng)常會看到一些好看的圖片,我們就希望把這些圖片保存下載,或者用戶用來做桌面壁紙,或者用來做設(shè)計(jì)的素材。下面這篇文章就來給大家介紹了關(guān)于利用python實(shí)現(xiàn)最簡單的網(wǎng)頁爬蟲的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • Python如何實(shí)現(xiàn)感知器的邏輯電路

    Python如何實(shí)現(xiàn)感知器的邏輯電路

    這篇文章主要介紹了Python如何實(shí)現(xiàn)感知器的邏輯電路,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python判斷以什么結(jié)尾以什么開頭的實(shí)例

    Python判斷以什么結(jié)尾以什么開頭的實(shí)例

    今天小編就為大家分享一篇Python判斷以什么結(jié)尾以什么開頭的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Python利用pip安裝tar.gz格式的離線資源包

    Python利用pip安裝tar.gz格式的離線資源包

    這篇文章主要給大家介紹了關(guān)于Python利用pip安裝tar.gz格式的離線資源包的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python rsa 加密解密

    python rsa 加密解密

    本篇文章主要介紹了python rsa加密解密 (編解碼,base64編解碼)的相關(guān)知識。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-03-03
  • Django ORM數(shù)據(jù)庫操作處理全面指南

    Django ORM數(shù)據(jù)庫操作處理全面指南

    本文深度探討Django ORM的概念、基礎(chǔ)使用、進(jìn)階操作以及詳細(xì)解析在實(shí)際使用中如何處理數(shù)據(jù)庫操作,同時,我們還討論了模型深入理解,如何進(jìn)行CRUD操作,并且深化理解到數(shù)據(jù)庫遷移等高級主題
    2023-09-09

最新評論

大理市| 宁夏| 汉寿县| 砚山县| 石渠县| 阿图什市| 石门县| 麦盖提县| 沭阳县| 黄浦区| 金门县| 泰宁县| 蒙自县| 哈尔滨市| 新巴尔虎左旗| 邹平县| 桑植县| 逊克县| 曲靖市| 仲巴县| 刚察县| 大丰市| 新闻| 南乐县| 孟连| 马山县| 霍邱县| 伊宁县| 嘉祥县| 洞口县| 林芝县| 漳州市| 司法| 民权县| 宁陕县| 兴义市| 石台县| 聂拉木县| 苍南县| 浦江县| 京山县|