python MNIST手写识别数据调用API的方法


Posted in Python onAugust 08, 2018

MNIST数据集比较小,一般入门机器学习都会采用这个数据集来训练

下载地址:yann.lecun.com/exdb/mnist/

有4个有用的文件:
train-images-idx3-ubyte: training set images
train-labels-idx1-ubyte: training set labels
t10k-images-idx3-ubyte: test set images
t10k-labels-idx1-ubyte: test set labels

The training set contains 60000 examples, and the test set 10000 examples. 数据集存储是用binary file存储的,黑白图片。

下面给出load数据集的代码:

import os
import struct
import numpy as np
import matplotlib.pyplot as plt

def load_mnist():
  '''
  Load mnist data
  http://yann.lecun.com/exdb/mnist/

  60000 training examples
  10000 test sets

  Arguments:
    kind: 'train' or 'test', string charater input with a default value 'train'

  Return:
    xxx_images: n*m array, n is the sample count, m is the feature number which is 28*28
    xxx_labels: class labels for each image, (0-9)
  '''

  root_path = '/home/cc/deep_learning/data_sets/mnist'

  train_labels_path = os.path.join(root_path, 'train-labels.idx1-ubyte')
  train_images_path = os.path.join(root_path, 'train-images.idx3-ubyte')

  test_labels_path = os.path.join(root_path, 't10k-labels.idx1-ubyte')
  test_images_path = os.path.join(root_path, 't10k-images.idx3-ubyte')

  with open(train_labels_path, 'rb') as lpath:
    # '>' denotes bigedian
    # 'I' denotes unsigned char
    magic, n = struct.unpack('>II', lpath.read(8))
    #loaded = np.fromfile(lpath, dtype = np.uint8)
    train_labels = np.fromfile(lpath, dtype = np.uint8).astype(np.float)

  with open(train_images_path, 'rb') as ipath:
    magic, num, rows, cols = struct.unpack('>IIII', ipath.read(16))
    loaded = np.fromfile(train_images_path, dtype = np.uint8)
    # images start from the 16th bytes
    train_images = loaded[16:].reshape(len(train_labels), 784).astype(np.float)

  with open(test_labels_path, 'rb') as lpath:
    # '>' denotes bigedian
    # 'I' denotes unsigned char
    magic, n = struct.unpack('>II', lpath.read(8))
    #loaded = np.fromfile(lpath, dtype = np.uint8)
    test_labels = np.fromfile(lpath, dtype = np.uint8).astype(np.float)

  with open(test_images_path, 'rb') as ipath:
    magic, num, rows, cols = struct.unpack('>IIII', ipath.read(16))
    loaded = np.fromfile(test_images_path, dtype = np.uint8)
    # images start from the 16th bytes
    test_images = loaded[16:].reshape(len(test_labels), 784)  

  return train_images, train_labels, test_images, test_labels

再看看图片集是什么样的:

def test_mnist_data():
  '''
  Just to check the data

  Argument:
    none

  Return:
    none
  '''
  train_images, train_labels, test_images, test_labels = load_mnist()
  fig, ax = plt.subplots(nrows = 2, ncols = 5, sharex = True, sharey = True)
  ax =ax.flatten()
  for i in range(10):
    img = train_images[i][:].reshape(28, 28)
    ax[i].imshow(img, cmap = 'Greys', interpolation = 'nearest')
    print('corresponding labels = %d' %train_labels[i])

if __name__ == '__main__':
  test_mnist_data()

跑出的结果如下:

python MNIST手写识别数据调用API的方法

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

Python 相关文章推荐
利用Python的Django框架生成PDF文件的教程
Jul 22 Python
深入理解python中的浅拷贝和深拷贝
May 30 Python
python使用opencv进行人脸识别
Apr 07 Python
Python日志模块logging基本用法分析
Aug 23 Python
Python wxPython库使用wx.ListBox创建列表框示例
Sep 03 Python
对python 生成拼接xml报文的示例详解
Dec 28 Python
tensorflow 获取所有variable或tensor的name示例
Jan 04 Python
python实现将列表中各个值快速赋值给多个变量
Apr 02 Python
tensorflow 2.1.0 安装与实战教程(CASIA FACE v5)
Jun 30 Python
python em算法的实现
Oct 03 Python
asyncio异步编程之Task对象详解
Mar 13 Python
pandas时间序列之pd.to_datetime()的实现
Jun 16 Python
python实现屏保计时器的示例代码
Aug 08 #Python
详解Python 装饰器执行顺序迷思
Aug 08 #Python
python Flask 装饰器顺序问题解决
Aug 08 #Python
Python BS4库的安装与使用详解
Aug 08 #Python
python特性语法之遍历、公共方法、引用
Aug 08 #Python
用Python shell简化开发
Aug 08 #Python
在Python中使用gRPC的方法示例
Aug 08 #Python
You might like
让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享
2011/08/08 PHP
thinkphp 验证码 的使用小结
2017/05/07 PHP
Laravel下生成验证码的类
2017/11/15 PHP
浅析PHP7的多进程及实例源码
2019/04/14 PHP
jQuery 中使用JSON的实现代码
2011/12/01 Javascript
jtable列中自定义button示例代码
2013/11/21 Javascript
jquery中的ajax方法怎样通过JSONP进行远程调用
2014/05/04 Javascript
iframe如何动态创建及释放其所占内存
2014/09/03 Javascript
JS实现网页滚动条感应鼠标变色的方法
2015/02/26 Javascript
jQuery遍历页面所有CheckBox查看是否被选中的方法
2015/04/14 Javascript
移动端JQ插件hammer使用详解
2015/07/03 Javascript
详谈Ajax请求中的async:false/true的作用(ajax 在外部调用问题)
2017/02/10 Javascript
前端图片懒加载(lazyload)的实现方法(提高用户体验)
2017/08/21 Javascript
jQury Ajax使用Token验证身份实例代码
2017/09/22 Javascript
javascript自定义事件功能与用法实例分析
2017/11/08 Javascript
vue项目中添加单元测试的方法
2018/07/21 Javascript
微信小程序实现吸顶效果
2020/01/08 Javascript
Vue-router中hash模式与history模式的区别详解
2020/12/15 Vue.js
[01:29:31]VP VS VG Supermajor小组赛胜者组第二轮 BO3第一场 6.2
2018/06/03 DOTA
Python中Random和Math模块学习笔记
2015/05/18 Python
python django 实现验证码的功能实例代码
2017/05/18 Python
Python获取当前公网ip并自动断开宽带连接实例代码
2018/01/12 Python
python发送邮件脚本
2018/05/22 Python
python线程join方法原理解析
2020/02/11 Python
基于pytorch中的Sequential用法说明
2020/06/24 Python
Gap中国官网:美式休闲风服饰
2017/02/05 全球购物
Sofft鞋官网:世界知名鞋类品牌
2017/03/28 全球购物
jurlique茱莉蔻英国官网:澳洲天然护肤品
2018/08/03 全球购物
Servlet如何得到服务器的信息
2015/12/22 面试题
董事长职责范文
2013/11/08 职场文书
门卫岗位职责
2013/11/15 职场文书
人力资源经理自我评价
2014/01/04 职场文书
生物制药专业自我鉴定
2014/02/19 职场文书
地道战观后感500字
2015/06/04 职场文书
浅谈Nginx 中的两种限流方式
2021/03/31 Servers
Python基础教程,Python入门教程(超详细)
2021/06/24 Python