#coding: utf-8
from tensorflow.examples.tutorials.mnist import input_data
import scipy.misc
import os
import numpy as np# 读取MNIST数据集。如果不存在会事先下载。
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)# 我们把原始图片保存在MNIST_data/raw/文件夹下
# 如果没有这个文件夹会自动创建
train_set_save_dir = 'MNIST_data/raw/train/'
test_set_save_dir = 'MNIST_data/raw/test/' if os.path.exists(train_set_save_dir) is False:os.makedirs(train_set_save_dir)if os.path.exists(test_set_save_dir) is False:os.makedirs(test_set_save_dir)n_classes = 10
train_set_dirs = []
test_set_dirs = []
train_file = []
test_file = []for i in range(0, n_classes):train_dir = train_set_save_dir + str(i) + "/"train_set_dirs.append(train_dir)test_dir = test_set_save_dir + str(i) + "/"test_set_dirs.append(test_dir)train_file.append([])test_file.append([])if os.path.exists(train_dir) is False:os.makedirs(train_dir)if os.path.exists(test_dir) is False:os.makedirs(test_dir)print(mnist.train.images.shape)
print(mnist.test.images.shape)
print(mnist.train.labels.shape)
print(mnist.test.labels.shape)print(mnist.train.labels[0:10])
print(np.argmax(mnist.train.labels[0:10,:], axis=1))trainImageCounts = mnist.train.images.shape[0]
testImageCounts = mnist.test.images.shape[0]#exit()# 保存训练集图片
for i in range(0, trainImageCounts):# classIndex = np.argmax(mnist.train.labels[i], axis=0)train_file[classIndex].append('mnist_train_%d.jpg' % i)#continue# 保存文件的格式为 mnist_train_0.jpg, mnist_train_1.jpg, ... ,mnist_train_19.jpg#filename = train_set_save_dir + 'mnist_train_%d.jpg' % ifilename = train_set_dirs[classIndex] + 'mnist_train_%d.jpg' % i# 请注意,mnist.train.images[i, :]就表示第i张图片(序号从0开始)image_array = mnist.train.images[i, :]# TensorFlow中的MNIST图片是一个784维的向量,我们重新把它还原为28x28维的图像。image_array = image_array.reshape(28, 28)# 将image_array保存为图片# 先用scipy.misc.toimage转换为图像,再调用save直接保存。scipy.misc.toimage(image_array, cmin=0.0, cmax=1.0).save(filename)# 保存测试集图片
for i in range(0, testImageCounts):# classIndex = np.argmax(mnist.test.labels[i], axis=0)test_file[classIndex].append('mnist_test_%d.jpg' % i)#continue# 保存文件的格式为 mnist_train_0.jpg, mnist_train_1.jpg, ... ,mnist_train_19.jpg#filename = test_set_save_dir + 'mnist_test_%d.jpg' % ifilename = test_set_dirs[classIndex] + 'mnist_test_%d.jpg' % i# 请注意,mnist.test.images[i, :]就表示第i张图片(序号从0开始)image_array = mnist.test.images[i, :]# TensorFlow中的MNIST图片是一个784维的向量,我们重新把它还原为28x28维的图像。image_array = image_array.reshape(28, 28)# 将image_array保存为图片# 先用scipy.misc.toimage转换为图像,再调用save直接保存。scipy.misc.toimage(image_array, cmin=0.0, cmax=1.0).save(filename)for i in range(0, n_classes):trainLabelFile = open(train_set_dirs[i] + "list.txt", "w")for picFlie in train_file[i]:trainLabelFile.write(picFlie + "\n")trainLabelFile.close()testLabelFile = open(test_set_dirs[i] + "list.txt", "w")for picFlie in test_file[i]:testLabelFile.write(picFlie + "\n")testLabelFile.close()print('Please check: %s ' % train_set_save_dir)
print('Please check: %s ' % test_set_save_dir)

mnist数据集保存为图片相关推荐

  1. [深度学习-实践]GAN基于手写体Mnist数据集生成新图片

    系列文章目录 深度学习GAN(一)之简单介绍 深度学习GAN(二)之基于CIFAR10数据集的例子 深度学习GAN(三)之基于手写体Mnist数据集的例子 深度学习GAN(四)之PIX2PIX GAN ...

  2. 用python将照片做成数据集_python实战项目,struct模块的使用,将MNIST数据集转换为bmp图片...

    最近学习 tensroflow,用到了入门级的经典数据集 MNIST,MNIST 包含几万张 28x28 像素大小的手写数字.但是它的存储是以字节流的形式存储的,几万张图片存储在一个文件里.一直对其很 ...

  3. MNIST数据集转为.jpg图片格式

    从mnist官网下载下来的mnist手写数据集是二进制文件流格式的,不能直接查看,如果需要查看,需要将二进制文件转化为jpg格式,可以用各种编程语言实现,如MATLAB.Python.C++等,本文是 ...

  4. python 将MNIST数据集转为jpg图片格式

    下载的数据集格式是字节存储的,有时需要转为图片格式,以下以测试集为例子,说明python转换代码. IDX数据格式 这四个文件采用了IDX的文件格式,一种平铺直叙的方式: magic number s ...

  5. 使用Python将MNIST数据集转化为图片

    1,对于训练集数据: import numpy as np import structfrom PIL import Image import osdata_file = 'somePath/trai ...

  6. tensorflow(七)实现mnist数据集上图片的训练和测试

    本文使用tensorflow实现在mnist数据集上的图片训练和测试过程,使用了简单的两层神经网络,代码中涉及到的内容,均以备注的形式标出. 关于文中的数据集,大家如果没有下载下来,可以到我的网盘去下 ...

  7. 将MNIST数据集转换成.jpg图片

    MNIST数据集简介 # MNIST 数据集合共包含70000张手写数字图片 # 其中60000张用作训练集 # 10000张用作预测集 # 数据集包含了0-9共10类手写数字图片,每张 # 图片都做 ...

  8. mnist数据集图片提取出来

    # -*- coding: UTF-8 -*-# 把mnist数据集转成图片做测试,图片更为通用import cv2 import os from keras.datasets import mnis ...

  9. GAN生成对抗网络基本概念及基于mnist数据集的代码实现

    本文主要总结了GAN(Generative Adversarial Networks) 生成对抗网络的基本原理并通过mnist数据集展示GAN网络的应用. GAN网络是由两个目标相对立的网络构成的,在 ...

最新文章

  1. codility上的问题 (22)
  2. 几行代码就搞定高端大气的云系统架构图
  3. Javascript:必须知道的Javascript知识点之“字面量和对应类型”
  4. Appium+Python3 并发启动测试设备
  5. 怀孕参加计算机考试有辐射吗,电脑辐射对孕妇的影响大不大 会对胎儿造成什么影响...
  6. Java程序员如何做到Java架构师
  7. Java系统变量之System.getenv()和System.getProperty()
  8. (DFS)zoj1008-Gnome Tetravex
  9. 电脑底部任务栏点不动_15个小技巧,让我的Windows电脑更好用了!
  10. mysql 事务日志备份_SQL Server恢复模式与事务日志备份
  11. 五班二组高级软件测试进度报告
  12. 100行代码搞定Python做OCR识别身份证,文字各种字体!
  13. 在IT呆了好久了,给大家科普下这个行业的行话
  14. 谷歌图片的爬虫库(附加必应图片爬虫)--针对近期谷歌变了
  15. 计算机网络安全 的论文,计算机网络安全论文6000字
  16. jdk+apache+jboss +mod_jk+openssl--从零开始搭建Linux测试环境
  17. C#项目之 GMap.net 标记点及 绘制多点之间的距离
  18. 【自学Python】Python布尔型(bool)
  19. MBI5020 LED驱动
  20. java 笛卡尔积_笛卡儿积的java实现

热门文章

  1. UI设计培训学习中必须掌握的设计原则
  2. hashMap传入参数,table长度为多少
  3. mysql干货——数据库字符集和校对规则详解
  4. Confluence 6 Home 和其他重要的目录
  5. 如何恢复,迁移,添加, 删除 Voting Disks
  6. 读Zepto源码之操作DOM
  7. 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT-2 运行可执行文件
  8. 转载:python原生态的输入窗口抖动+输入特效
  9. Transform-style和Perspective属性
  10. HQL中的Like查询需要注意的地方