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

作者:caichao08 时间:2021-05-13 19:20:45 

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的方法

来源:https://blog.csdn.net/caichao08/article/details/78988389

标签:python,MNIST,手写识别
0
投稿

猜你喜欢

  • python日志记录模块实例及改进

    2021-03-14 12:49:09
  • 不要跳转或刷新 实现网页区域选择显示

    2010-07-02 16:25:00
  • PHP中让json_encode不自动转义斜杠“/”的方法

    2023-07-12 22:44:58
  • 精美漂亮的php分页类代码

    2023-11-22 08:28:09
  • VBScript WeekdayName 函数语法

    2008-01-25 19:25:00
  • Python根据当前日期取去年同星期日期

    2021-09-14 15:01:48
  • 浅谈js数组和splice的用法

    2023-08-23 00:19:42
  • SQL Server中如何优化磁带备份设备性能

    2009-01-07 14:23:00
  • 使用Python的SymPy库解决数学运算问题的方法

    2021-04-19 19:36:35
  • Django实现文件上传下载功能

    2023-06-13 20:09:46
  • python实现简单俄罗斯方块游戏

    2023-04-13 03:28:05
  • python MultipartEncoder传输zip文件实例

    2022-04-20 07:22:57
  • python 列表递归求和、计数、求最大元素的实例

    2023-01-07 13:28:07
  • asp检测文件编码方法

    2007-10-03 14:27:00
  • SQL SERVER 2005中的同步复制技术

    2009-01-05 13:44:00
  • 简单了解python 邮件模块的使用方法

    2021-07-11 00:08:14
  • php浅析反序列化结构

    2023-11-17 17:34:37
  • 运行SQL Server的计算机间移动数据库

    2009-01-20 13:07:00
  • Python Opencv使用ann神经网络识别手写数字功能

    2023-11-03 02:44:52
  • ASP 3.0中的新特性

    2008-02-27 13:28:00
  • asp之家 网络编程 m.aspxhome.com