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

pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法

 更新時(shí)間:2018年05月20日 16:58:24   作者:瓦力冫  
這篇文章主要介紹了pytorch 把MNIST數(shù)據(jù)集轉(zhuǎn)換成圖片和txt的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

本文介紹了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python中的random.choices函數(shù)用法詳解

    Python中的random.choices函數(shù)用法詳解

    這篇文章主要給大家介紹了關(guān)于Python中random.choices函數(shù)用法的相關(guān)資料,random.random()?的功能是隨機(jī)返回一個(gè)?0-1范圍內(nèi)的浮點(diǎn)數(shù),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • Django處理Ajax發(fā)送的Get請(qǐng)求代碼詳解

    Django處理Ajax發(fā)送的Get請(qǐng)求代碼詳解

    在本篇文章里小編給大家整理了關(guān)于Django處理Ajax發(fā)送的Get請(qǐng)求代碼知識(shí)點(diǎn),有需要的朋友們參考學(xué)習(xí)下。
    2019-07-07
  • python 彈窗提示警告框MessageBox的實(shí)例

    python 彈窗提示警告框MessageBox的實(shí)例

    今天小編就為大家分享一篇python 彈窗提示警告框MessageBox的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • M1芯片安裝python3.9.1的實(shí)現(xiàn)

    M1芯片安裝python3.9.1的實(shí)現(xiàn)

    這篇文章主要介紹了M1芯片安裝python3.9.1的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python 刪除空值且合并excel的操作

    python 刪除空值且合并excel的操作

    這篇文章主要介紹了python 刪除空值且合并excel的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • Python使用PyAudio制作錄音工具的實(shí)現(xiàn)代碼

    Python使用PyAudio制作錄音工具的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Python使用PyAudio制作錄音工具,音頻錄制與視頻錄制相似,也是以數(shù)據(jù)幀的方式錄制保存,這次使用強(qiáng)大的第三方包PyAudio和內(nèi)置的wave模塊編寫,需要的朋友可以參考下
    2022-04-04
  • Python辦公自動(dòng)化之將任意文件轉(zhuǎn)為PDF格式

    Python辦公自動(dòng)化之將任意文件轉(zhuǎn)為PDF格式

    這種把某個(gè)文件轉(zhuǎn)為pdf枯燥無(wú)聊的工作,既沒(méi)有什么技術(shù)含量又累. 今天辰哥就教大家將任意文件批量轉(zhuǎn)為PDF,這里以日常辦公的word、excel、ppt為例,這三種格式的文件轉(zhuǎn)為PDF.需要的朋友可以參考下
    2021-06-06
  • python 讀取豎線分隔符的文本方法

    python 讀取豎線分隔符的文本方法

    今天小編就為大家分享一篇python 讀取豎線分隔符的文本方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析

    Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析

    這篇文章主要介紹了Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Python 解析xml文件的示例

    Python 解析xml文件的示例

    這篇文章主要介紹了Python 解析xml文件的示例,幫助大家更好的利用python處理文件,感興趣的朋友可以了解下
    2020-09-09

最新評(píng)論

榆社县| 塔城市| 云安县| 新建县| 寻甸| 冀州市| 南宁市| 通山县| 桂林市| 大邑县| 黑水县| 贡觉县| 渭源县| 丰台区| 昌黎县| 高尔夫| 巴中市| 舞钢市| 闽清县| 金湖县| 巴林右旗| 乡宁县| 资讯 | 衡水市| 乃东县| 武冈市| 仁化县| 龙海市| 台东县| 红原县| 元氏县| 井研县| 乐亭县| 寻乌县| 游戏| 阳西县| 灵石县| 莱芜市| 桃园县| 黑水县| 通渭县|