国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

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

2020-02-15 21:29:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

加載并保存圖像信息

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

import osimport matplotlibfrom keras.datasets import mnistimport numpy as npfrom torch.utils.data.dataset import Datasetfrom PIL import Imageimport scipy.miscroot_path = 'E:/coding_ex/pytorch/Alexnet/data/'base_path = 'baseset/'training_path = 'trainingset/'test_path = 'testset/'

這里將數(shù)據(jù)集分為三類(lèi),baseset為所有數(shù)據(jù)(trainingset+testset),trainingset是訓(xùn)練集,testset是測(cè)試集。直接通過(guò)keras.dataset加載mnist數(shù)據(jù)集,不能自動(dòng)下載的話(huà)可以手動(dòng)下載.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()

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

/img文件夾

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

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 商洛市| 凉城县| 泸定县| 稻城县| 凤阳县| 固镇县| 开鲁县| 兴业县| 荣昌县| 抚远县| 桓仁| 临高县| 子洲县| 东乌珠穆沁旗| 新邵县| 马公市| 固原市| 轮台县| 沁水县| 曲靖市| 电白县| 吉安市| 新巴尔虎右旗| 惠州市| 股票| 丘北县| 综艺| 禄劝| 潜山县| 嘉善县| 遂川县| 平顶山市| 鹰潭市| 登封市| 马公市| 方城县| 武强县| 万宁市| 买车| 房产| 邯郸县|