python MNIST手写识别数据调用API的方法
程序员文章站
2022-05-28 08:38:17
mnist数据集比较小,一般入门机器学习都会采用这个数据集来训练
下载地址:
有4个有用的文件:
train-images-idx3-ubyte: training...
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()
跑出的结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 如何使用flashget
下一篇: qq邮箱注册方法,qq邮箱新手指南
推荐阅读
-
利用python和百度地图API实现数据地图标注的方法
-
Python(TensorFlow框架)实现手写数字识别系统的方法
-
基于MNIST手写数字数据集的数字识别小程序
-
python下调用pytesseract识别某网站验证码的实现方法
-
Python的Flask框架应用调用Redis队列数据的方法
-
python MNIST手写识别数据调用API的方法
-
Python 调用 zabbix api的方法示例
-
python 用opencv调用训练好的模型进行识别的方法
-
手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)
-
Python调用SQLPlus来操作和解析Oracle数据库的方法