python MNIST手写识别数据调用API的方法
作者:caichao08 发布时间:2021-05-13 19:20:45
标签:python,MNIST,手写识别
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()
跑出的结果如下:
来源:https://blog.csdn.net/caichao08/article/details/78988389
0
投稿
猜你喜欢
- 只是做笔记,没什么!! 代码如下:--创建测试表 CREATE TABLE [dbo].[Student]( [ID] [int
- 用法: 按住鼠标左键拖拽一个框后释放洗洗睡了<!DOCTYPE html public "-//W3C//DTD XHTML
- MySQL 数据(字段)类型在创建表的时候,要明确定义字段对应的数据类型。MySQL 主要的数据类型分为数值类型、字符串(文本)类型、时间日
- python设置环境变量有什么用?1、设置环境变量可以在cmd命令行中找到python命令;2、设置环境变量可以在安装exe的第三包的时候找
- 本文介绍了目前6种比较常用的进度条,让大家都能直观地看到脚本运行最新的进展情况1.普通进度条在代码迭代运行中可以自己进行统计计算,并使用格式
- * 页爬取是爬虫学习中的一个难点。本文将以知名插画网站pixiv为例,简要介绍 * 页爬取的方法。写在前面本代码的功能是输入画师的pixi
- 本文介绍TSV文件类型及其应用,同时介绍Golang语句读取TSV文件并转为struct的实现过程。认识TSV文件也许你之前不了解TSV文件
- 为庆祝jQuery的四周年生日,jQuery官方团队正式发布了jQuery 1.4版本。在这个版本中,jQuery官方团队做了大量的编码、测
- 前言对Python游戏有所了解的朋友都知道,在2D的游戏制作中,经常会用到一个模块pygame,他能帮助我们实现很多方便使用的功能,例如绘制
- 目前,我们要在网页中使用圆角效果,总是通过切图然后嵌套很多div,用背景来实现圆角效果。对于前端开发工程师来说,圆角的确是一个让人又爱又恨的
- 是否曾经有过这样的经历:把一个元素置于另一个元素之上,而希望下面的那个元素成为可点击的?现在,利用css的pointer-events属性即
- 1.依次安装python2和python3,并添加到系统环境变量中2.找到python3的安装目录,一般在C:\Users\Administ
- python matplotlib画图使用colorbar工具自定义颜色 colorbar(draw colorbar without an
- 阅读上一篇:FrontPage XP设计教程2——网页的编辑 制作一个漂亮的网页,离不开网页整体布局的设计,网页布局设计的合理与否,直接影响
- 目录实现思路使用BackgroundSubtractorMOG2进行背景分割使用人像识别填充面部信息使用形态学填充分割出来的前景将人像与目标
- 引言在这篇文章中,我将介绍如何使用YOLOv5构建一个佩戴安全帽检测和识别系统。这个系统可以实时检测图像上人物是否有未佩戴安全帽,并及时进行
- 一、Pandoc转换1.1 问题由于我们markdown编辑器比较特殊,一般情况下,我们不太好看,如果转换成pdf的话,我们就不需要可以的去
- QL Server事件探查器(Profiler)可以帮助数据库管理员跟踪SQL Server数据库所执行的特定事件,监视数据库的行为;并将这
- 前言突然想起来之前讲SQL注入时忘记讲一下这个宽字节注入了,因为这个知识点还是挺重要的,所以本文就带大家了解一下宽字节注入的原理以及应用方法
- 1,建路由;2,写方法;def fanyi(request): import requests import jso