pytorch 把MNIST数据集转换成图片和txt的方法

 更新时间:2018年05月20日 16:58:24   作者:瓦力冫  
这篇文章主要介绍了pytorch 把MNIST数据集转换成图片和txt的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文介绍了pytorch 把MNIST数据集转换成图片和txt的方法,分享给大家,具体如下:

1.下载Mnist 数据集

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,
)

下载下来的其实可以直接用了,但是我们这边想把它们转换成图片和txt,这样好看些,为后面用自己的图片和txt作为准备

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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python关于拓扑排序知识点讲解

    Python关于拓扑排序知识点讲解

    在本篇文章里小编给大家分享了一篇关于Python关于拓扑排序知识点讲解内容,有兴趣的朋友们可以学习下。
    2021-01-01
  • 在python中使用正则表达式查找可嵌套字符串组

    在python中使用正则表达式查找可嵌套字符串组

    这篇文章主要介绍了在python中使用正则表达式查找可嵌套字符串组的相关资料,需要的朋友可以参考下
    2017-10-10
  • 详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案

    详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案

    这篇文章主要介绍了详解Python中的GIL(全局解释器锁)详解及解决GIL的几种方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • python绘制分组对比柱状图

    python绘制分组对比柱状图

    这篇文章主要为大家详细介绍了python绘制分组对比柱状图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • tensorflow中的数据类型dtype用法说明

    tensorflow中的数据类型dtype用法说明

    这篇文章主要介绍了tensorflow中的数据类型dtype用法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • Python中Numpy包的安装与使用方法简明教程

    Python中Numpy包的安装与使用方法简明教程

    这篇文章主要介绍了Python中Numpy包的安装与使用方法,结合简单实例形式分析了Python使用pip命令在线与离线whl包安装,以及使用numpy打印随机数矩阵的操作技巧,需要的朋友可以参考下
    2018-07-07
  • Python实现网络聊天室的示例代码(支持多人聊天与私聊)

    Python实现网络聊天室的示例代码(支持多人聊天与私聊)

    这篇文章主要介绍了Python实现网络聊天室的示例代码(支持多人聊天与私聊),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Python数据类型学习笔记

    Python数据类型学习笔记

    这篇文章主要针对Python数据类型为大家进行了详细介绍,整理一篇关于Python数据类型的学习笔记,感兴趣的小伙伴们可以参考一下
    2016-01-01
  • Python 函数绘图及函数图像微分与积分

    Python 函数绘图及函数图像微分与积分

    今天小编就为大家分享一篇Python 函数绘图及函数图像微分与积分,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11
  • 如何查看python关键字

    如何查看python关键字

    在本篇文章里小编给大家整理的是一篇关于python关键字的查看方法和实例内容,有兴趣的朋友们可以学习下。
    2021-01-01

最新评论