MNIST手写体数据库介绍

官方主页:

http://yann.lecun.com/exdb/mnist/

Google实验室的Corinna Cortes和纽约大学柯朗研究所的YannLeCun建有一个手写数字数据库,训练库有60,000张手写数字图像,测试库有10,000张。

训练库train-images.idx3-ubyte,文件大小47040016,每个图像784=28×28,784×60000 =47040000

测试库t10k-images.idx3-ubyte,文件大小7840016,784×10000=7840000,

两个图片数据库都多出来16个字节。

另外两个标记文件都多出来8个字节。

多出来的这些东西是什么呢?

图像数据库中多出来16个字节为  magic number = 0x00000803

                                                           Imagenumber = 0x0000ea60/60000 0x00002710/10000

                                                           Imageheight = 28

                                                           Imagewidth = 28

标签文件中多出来8个字节为magic number = 0x00000801 和image_number

这些标记信息位于文件的开头,后面便是实际数据了。

手写效果如图:


一段python解析代码:

#! /usr/bin/env python
import struct
import numpy as np
#import matplotlib.pyplot as plt
import Image
import sys

input_path = sys.argv[1] #mnist数据库解压后的所在路径
output_path = sys.argv[2] #生成的图片所在的路径

# =====read labels=====
label_file = input_path + '/train-labels.idx1-ubyte'
label_fp = open(label_file, 'rb')
label_buf = label_fp.read()

label_index=0
label_magic, label_numImages = struct.unpack_from('>II', label_buf, label_index)
label_index += struct.calcsize('>II')
labels = struct.unpack_from('>60000B', label_buf, label_index)
"""
有的时候需要用Python处理二进制数据,比如,存取文件,socket操作时.
这时候,可以使用python的struct模块来完成.可以用 struct来处理C语言中的结构体.
uppack_from有三个参数,第一个是fmt, 第二个是buf, 第三个是index
fmt 中 > 可以简单的理解为解析出,不知道packinto时会不会使用 <
II 是按照两个整形数据来解析

calcsize 表示计算两个整形变量的长度

>60000B 表示按照60000个B 二进制数据来解析
"""
# =====read train images=====
label_map = {}
train_file = input_path + '/train-images.idx3-ubyte'
train_fp = open(train_file, 'rb')
buf = train_fp.read()

index=0
magic,numImages,numRows,numColumns=struct.unpack_from('>IIII',buf,index)
index+=struct.calcsize('>IIII')
k = 0
for image in range(0,numImages):
    label = labels[k]
    if(label_map.has_key(label)):
        ids = label_map[label] + 1
        label_map[label] += 1

    else:
        label_map[label] = 0
        ids = 0
    k += 1
    if(label_map[label] > 50):
            continue
    im=struct.unpack_from('>784B',buf,index)
    index+=struct.calcsize('>784B')

    im=np.array(im,dtype='uint8')
    im=im.reshape(28,28)
    #fig=plt.figure()
    #plotwindow=fig.add_subplot(111)
    #plt.imshow(im,cmap='gray')
    #plt.show()
    im=Image.fromarray(im)
    im.save(output_path + '/%s_%s.bmp'%(label, ids),'bmp')


解码数据库的另外两个连接:

http://blog.csdn.net/fengbingchun/article/details/49611549

http://blog.csdn.net/zc02051126/article/details/51557166

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值