溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pytorch如何實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像可視化及保存

發(fā)布時(shí)間:2021-05-24 13:50:33 來源:億速云 閱讀:565 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下pytorch如何實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像可視化及保存,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

如何將pytorch中mnist數(shù)據(jù)集的圖像可視化及保存

導(dǎo)出一些庫

import torch
import torchvision 
import torch.utils.data as Data 
import scipy.misc
import os
import matplotlib.pyplot as plt   
BATCH_SIZE = 50  
DOWNLOAD_MNIST = True

數(shù)據(jù)集的準(zhǔn)備

#訓(xùn)練集測(cè)試集的準(zhǔn)備

train_data = torchvision.datasets.MNIST(root='./mnist/', train=True,transform=torchvision.transforms.ToTensor(),              
  download=DOWNLOAD_MNIST, )
test_data = torchvision.datasets.MNIST(root='./mnist/', train=False)

將訓(xùn)練及測(cè)試集利用dataloader進(jìn)行迭代

train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE, shuffle=True)
test_x = Variable(torch.unsqueeze(test_data.test_data, dim=1), requires_grad=True).type(torch.FloatTensor)[:20]/255 
test_y = test_data.test_labels[:20]#前兩千張
 #具體查看圖像形式為:
 
a_data, a_label = train_data[0]
print(type(a_data))#tensor 類型
#print(a_data)
print(a_label)

#把原始圖片保存至MNIST_data/raw/下
save_dir="mnist/raw/"
if os.path.exists(save_dir)is False:
 os.makedirs(save_dir)
 
for i in range(20):
 image_array,_=train_data[i]#打印第i個(gè)
 image_array=image_array.resize(28,28)
 filename=save_dir + 'mnist_train_%d.jpg' % i#保存文件的格式
 print(filename)
 print(train_data.train_labels[i])#打印出標(biāo)簽
 scipy.misc.toimage(image_array,cmin=0.0,cmax=1.0).save(filename)#保存圖像

pytorch如何實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像可視化及保存

pytorch如何實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像可視化及保存

pytorch的優(yōu)點(diǎn)

1.PyTorch是相當(dāng)簡(jiǎn)潔且高效快速的框架;2.設(shè)計(jì)追求最少的封裝;3.設(shè)計(jì)符合人類思維,它讓用戶盡可能地專注于實(shí)現(xiàn)自己的想法;4.與google的Tensorflow類似,F(xiàn)AIR的支持足以確保PyTorch獲得持續(xù)的開發(fā)更新;5.PyTorch作者親自維護(hù)的論壇 供用戶交流和求教問題6.入門簡(jiǎn)單

看完了這篇文章,相信你對(duì)“pytorch如何實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像可視化及保存”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI