pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法
本文介紹了pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法,分享給大家,具體如下:
1.下載Mnist 數(shù)據(jù)集
import os
# third-party library
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision
import matplotlib.pyplot as plt
# torch.manual_seed(1) # reproducible
DOWNLOAD_MNIST = False
# Mnist digits dataset
if not(os.path.exists('./mnist/')) or not os.listdir('./mnist/'):
# not mnist dir or mnist is empyt dir
DOWNLOAD_MNIST = True
train_data = torchvision.datasets.MNIST(
root='./mnist/',
train=True, # this is training data
transform=torchvision.transforms.ToTensor(), # Converts a PIL.Image or numpy.ndarray to
# torch.FloatTensor of shape (C x H x W) and normalize in the range [0.0, 1.0]
download=DOWNLOAD_MNIST,
)
下載下來(lái)的其實(shí)可以直接用了,但是我們這邊想把它們轉(zhuǎn)換成圖片和txt,這樣好看些,為后面用自己的圖片和txt作為準(zhǔn)備
2. 保存為圖片和txt
import os
from skimage import io
import torchvision.datasets.mnist as mnist
import numpy
root = "./mnist/raw/"
train_set = (
mnist.read_image_file(os.path.join(root, 'train-images-idx3-ubyte')),
mnist.read_label_file(os.path.join(root, 'train-labels-idx1-ubyte'))
)
test_set = (
mnist.read_image_file(os.path.join(root,'t10k-images-idx3-ubyte')),
mnist.read_label_file(os.path.join(root,'t10k-labels-idx1-ubyte'))
)
print("train set:", train_set[0].size())
print("test set:", test_set[0].size())
def convert_to_img(train=True):
if(train):
f = open(root + 'train.txt', 'w')
data_path = root + '/train/'
if(not os.path.exists(data_path)):
os.makedirs(data_path)
for i, (img, label) in enumerate(zip(train_set[0], train_set[1])):
img_path = data_path + str(i) + '.jpg'
io.imsave(img_path, img.numpy())
int_label = str(label).replace('tensor(', '')
int_label = int_label.replace(')', '')
f.write(img_path + ' ' + str(int_label) + '\n')
f.close()
else:
f = open(root + 'test.txt', 'w')
data_path = root + '/test/'
if (not os.path.exists(data_path)):
os.makedirs(data_path)
for i, (img, label) in enumerate(zip(test_set[0], test_set[1])):
img_path = data_path + str(i) + '.jpg'
io.imsave(img_path, img.numpy())
int_label = str(label).replace('tensor(', '')
int_label = int_label.replace(')', '')
f.write(img_path + ' ' + str(int_label) + '\n')
f.close()
convert_to_img(True)
convert_to_img(False)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- pytorch實(shí)現(xiàn)mnist數(shù)據(jù)集的圖像可視化及保存
- 關(guān)于Pytorch的MNIST數(shù)據(jù)集的預(yù)處理詳解
- pytorch:實(shí)現(xiàn)簡(jiǎn)單的GAN示例(MNIST數(shù)據(jù)集)
- 使用 PyTorch 實(shí)現(xiàn) MLP 并在 MNIST 數(shù)據(jù)集上驗(yàn)證方式
- 用Pytorch訓(xùn)練CNN(數(shù)據(jù)集MNIST,使用GPU的方法)
- 詳解PyTorch手寫(xiě)數(shù)字識(shí)別(MNIST數(shù)據(jù)集)
- Python PyTorch 如何獲取 MNIST 數(shù)據(jù)
相關(guān)文章
Python3使用xlrd、xlwt處理Excel方法數(shù)據(jù)
這篇文章主要介紹了Python3使用xlrd、xlwt處理Excel方法數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
最強(qiáng)Python可視化繪圖庫(kù)Plotly詳解用法
數(shù)據(jù)分析離不開(kāi)數(shù)據(jù)可視化。Plotly 是一款用來(lái)做數(shù)據(jù)分析和可視化的在線平臺(tái),功能非常強(qiáng)大,可以在線繪制很多圖形比如條形圖、散點(diǎn)圖、餅圖、直方圖等等2021-11-11
用Python監(jiān)控NASA TV直播畫(huà)面的實(shí)現(xiàn)步驟
本文分享一個(gè)名為"Spacestills"的開(kāi)源程序,它可以用于查看 NASA TV 的直播畫(huà)面(靜止幀)2021-05-05
詳解python實(shí)現(xiàn)讀取郵件數(shù)據(jù)并下載附件的實(shí)例
這篇文章主要介紹了詳解python讀取郵件數(shù)據(jù)并下載附件的實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)實(shí)例,幫助大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下2017-08-08
python:動(dòng)態(tài)路由的Flask程序代碼
今天小編就為大家分享一篇python:動(dòng)態(tài)路由的Flask程序代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
12個(gè)Pandas/NumPy中的加速函數(shù)使用總結(jié)
在本文中,數(shù)據(jù)和分析工程師?Kunal?Dhariwal?為我們介紹了?12?種?Numpy?和?Pandas?函數(shù),這些高效的函數(shù)會(huì)令數(shù)據(jù)分析更為容易、便捷2022-09-09
Python3多線程處理爬蟲(chóng)的實(shí)戰(zhàn)
本文主要介紹了Python3多線程處理爬蟲(chóng)的實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
python中while循環(huán)語(yǔ)句用法簡(jiǎn)單實(shí)例
這篇文章主要介紹了python中while循環(huán)語(yǔ)句用法,以一個(gè)簡(jiǎn)單實(shí)例形式分析了Python使用while循環(huán)語(yǔ)句使用方法,需要的朋友可以參考下2015-05-05
python神經(jīng)網(wǎng)絡(luò)編程實(shí)現(xiàn)手寫(xiě)數(shù)字識(shí)別
這篇文章主要為大家詳細(xì)介紹了python神經(jīng)網(wǎng)絡(luò)編程實(shí)現(xiàn)手寫(xiě)數(shù)字識(shí)別,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Python實(shí)現(xiàn)識(shí)別手寫(xiě)數(shù)字 簡(jiǎn)易圖片存儲(chǔ)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)識(shí)別手寫(xiě)數(shù)字,簡(jiǎn)易圖片存儲(chǔ)管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01

