参考文章:

http://www.csuldw.com/2016/02/25/2016-02-25-machine-learning-MNIST-dataset/

import numpy as np
import struct
import matplotlib.pyplot as plt
import os
filename = 'data_AI/MNIST/train-images.idx3-ubyte'
binfile = open(filename , 'rb')
buf = binfile.read() index = 0
magic, numImages , numRows , numColumns = struct.unpack_from('>IIII' , buf , index)
index += struct.calcsize('IIII' )
images = []
for i in range(numImages):
imgVal = struct.unpack_from('>784B', buf, index)
index += struct.calcsize('>784B')
imgVal = list(imgVal)
for j in range(len(imgVal)):
if imgVal[j] > 1:
imgVal[j] = 1 images.append(imgVal)
arrX = np.array(images) # 读取标签
binFile = open('data_AI/MNIST/train-labels.idx1-ubyte','rb')
buf = binFile.read()
binFile.close()
index = 0
magic, numItems= struct.unpack_from('>II', buf,index)
index += struct.calcsize('>II')
labels = []
for x in range(numItems):
im = struct.unpack_from('>1B',buf,index)
index += struct.calcsize('>1B')
labels.append(im[0])
arrY = np.array(labels)
print(np.shape(arrY)) # print(np.shape(trainX))
#以下内容是将图像保存到本地文件中
path_trainset = "data_AI/MNIST/imgs_train"
path_testset = "data_AI/MNIST/imgs_test"
if not os.path.exists(path_trainset):
os.mkdir(path_trainset)
if not os.path.exists(path_testset):
os.mkdir(path_testset)
for i in range(1):
img = np.array(arrX[i])
print(img)
img = img.reshape(28,28)
outfile = str(i) + "_" + str(arrY[i]) + ".png"
# outfile = str(i)+".png"
plt.figure()
plt.imshow(img, cmap = 'binary') #将图像黑白显示
plt.savefig(path_trainset + "/" + outfile)
print("save"+str(i)+"张")

将mnist数据集存储到本地文件的更多相关文章

  1. 16、爬取知乎大v张佳玮的文章“标题”、“摘要”、“链接”,并存储到本地文件

    爬取知乎大v张佳玮的文章“标题”.“摘要”.“链接”,并存储到本地文件 # 爬取知乎大v张佳玮的文章“标题”.“摘要”.“链接”,并存储到本地文件 # URL https://www.zhihu.co ...

  2. Python爬虫抓取糗百的图片,并存储在本地文件夹

    思路: 1.观察网页,找到img标签 2.通过requests和BS库来提取网页中的img标签 3.抓取img标签后,再把里面的src给提取出来,接下来就可以下载图片了 4.通过urllib的urll ...

  3. [原创]python爬虫之BeautifulSoup,爬取网页上所有图片标题并存储到本地文件

    from bs4 import BeautifulSoup import requests import re import os r = requests.get("https://re. ...

  4. tensorflow读取本地MNIST数据集

    tensorflow读取本地MNIST数据集 数据放入文件夹(不要解压gz): >>> import tensorflow as tf >>> from tenso ...

  5. 实践详细篇-Windows下使用VS2015编译的Caffe训练mnist数据集

    上一篇记录的是学习caffe前的环境准备以及如何创建好自己需要的caffe版本.这一篇记录的是如何使用编译好的caffe做训练mnist数据集,步骤编号延用上一篇 <实践详细篇-Windows下 ...

  6. Windows下mnist数据集caffemodel分类模型训练及测试

    1. MNIST数据集介绍 MNIST是一个手写数字数据库,样本收集的是美国中学生手写样本,比较符合实际情况,大体上样本是这样的: MNIST数据库有以下特性: 包含了60000个训练样本集和1000 ...

  7. C++读取MNIST数据集

    MNIST是一个标准的手写字符测试集. Mnist数据集对应四个文件: train-images-idx3-ubyte: training set images  train-labels-idx1- ...

  8. php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地

    php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地 yeah,搞定啦 php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页 ...

  9. 如果程序集是从 Web 上下载的,即使它存储于本地计算机,Windows 也会将其标记为 Web 文件,http://go.microsoft.com/fwlink/?LinkId=179545

    使用Silverlight,经常弄出很多莫名的XXX文件来于Web,神马信任程序集,就Build个程序都那么麻烦,应该在所有发布时注明一些最基本的配置说明,最BT莫过于连下载程序集的地方都找不到. 若 ...

随机推荐

  1. python并发编程之asyncio协程(三)

    协程实现了在单线程下的并发,每个协程共享线程的几乎所有的资源,除了协程自己私有的上下文栈:协程的切换属于程序级别的切换,对于操作系统来说是无感知的,因此切换速度更快.开销更小.效率更高,在有多IO操作 ...

  2. python 异常知识点

    raise from python 在3.0 之后引入了raise from 表达式: raise exception from otherexception 当使用该语法时,第二个表达式指定了另一个 ...

  3. 使用正则表达式匹配IP地址

    IP地址分为4段,以点号分隔.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配:   第一步:地址分析,正则初判 1.0-9 \d 进行匹配 2.10-99 [1-9]\d 进行匹 ...

  4. python代码这样写会更优雅

    1.链式比较操作 age = 18 if age > 18 and age < 60: print("young man") pythonic if 18 < a ...

  5. [MySQL]You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

    执行update语句,出现问题: 问题描述: You are using safe update mode and you tried to update a table without a WHER ...

  6. **[权限控制] 利用CI钩子实现权限认证

    http://codeigniter.org.cn/forums/thread-10877-1-1.html 一直没找到CI的权限认证扩展,以前好像找到过一个老外的扩展,不过不怎么好用,现在记不清了, ...

  7. rest api load test

    1. SoapUI + LoadUI 2. https://github.com/jeffbski/bench-rest 3. JMeter

  8. naive cube implementation in python

    这篇论文中提到的naive cube算法的实现,python写出来真的就和伪代码差不多=.= 输入大约长这样,依次是 index userid country state city topic cat ...

  9. day2 列表中常用的方法

    列表中有很多方法,下面来看看常用的方法,我们知道,字符串是以字符列表形式存储的.因此上面学习的字符串中的很多方法在列表中也有.     1.extend() extend()列表的扩展,把两个列表进行 ...

  10. Nuxt 2.0 需要将pug-loader改成pug-plain-loader

    Nuxt 2.0 需要将pug-loader改成pug-plain-loader npm i pug-plain-loader -D 解决问题!! 参考链接 https://my.oschina.ne ...