当前位置: 首页 > 编程笔记 >

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

商池暝
2023-03-14
本文向大家介绍pytorch 把MNIST数据集转换成图片和txt的方法,包括了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)

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

 类似资料:
  • 本文向大家介绍pytorch 数据集图片显示方法,包括了pytorch 数据集图片显示方法的使用技巧和注意事项,需要的朋友参考一下 图片显示 pytorch 载入的数据集是元组tuple 形式,里面包括了数据及标签(train_data,label),其中的train_data数据可以转换为torch.Tensor形式,方便后面计算使用。 同样给一些刚入门的同学在使用载入的数据显示图片的时候带来一

  • 本文向大家介绍Pytorch使用MNIST数据集实现CGAN和生成指定的数字方式,包括了Pytorch使用MNIST数据集实现CGAN和生成指定的数字方式的使用技巧和注意事项,需要的朋友参考一下 CGAN的全拼是Conditional Generative Adversarial Networks,条件生成对抗网络,在初始GAN的基础上增加了图片的相应信息。 这里用传统的卷积方式实现CGAN。 和

  • 本文向大家介绍关于Pytorch的MNIST数据集的预处理详解,包括了关于Pytorch的MNIST数据集的预处理详解的使用技巧和注意事项,需要的朋友参考一下 关于Pytorch的MNIST数据集的预处理详解 MNIST的准确率达到99.7% 用于MNIST的卷积神经网络(CNN)的实现,具有各种技术,例如数据增强,丢失,伪随机化等。 操作系统:ubuntu18.04 显卡:GTX1080ti p

  • 本文向大家介绍pytorch下大型数据集(大型图片)的导入方式,包括了pytorch下大型数据集(大型图片)的导入方式的使用技巧和注意事项,需要的朋友参考一下 使用torch.utils.data.Dataset类 处理图片数据时, 1. 我们需要定义三个基本的函数,以下是基本流程 这里,我将 读取图片 的步骤 放到 __getitem__ ,是因为 这样放的话,对内存的要求会降低很多,我们只是将

  • 本文向大家介绍Pytorch使用MNIST数据集实现基础GAN和DCGAN详解,包括了Pytorch使用MNIST数据集实现基础GAN和DCGAN详解的使用技巧和注意事项,需要的朋友参考一下 原始生成对抗网络Generative Adversarial Networks GAN包含生成器Generator和判别器Discriminator,数据有真实数据groundtruth,还有需要网络生成的“

  • 在介绍softmax回归的实现前我们先引入一个多类图像分类数据集。它将在后面的章节中被多次使用,以方便我们观察比较算法之间在模型精度和计算效率上的区别。图像分类数据集中最常用的是手写数字识别数据集MNIST [1]。但大部分模型在MNIST上的分类精度都超过了95%。为了更直观地观察算法之间的差异,我们将使用一个图像内容更加复杂的数据集Fashion-MNIST [2]。 获取数据集 首先导入本节