读取mnist二进制文件

from __future__ import  division
import math
import random
import pprint
import scipy.misc
import numpy as np
from time import gmtime,strftime
from six.moves import xrange
import matplotlib.pyplot as plt
import os,gzip

import tensorflow as tf
import tensorflow.contrib.slim as slim

#读取mnist数据集
def load_mnist(data_dir):
    def extract_data(filename,num_data,head_size,data_size):
        #读取压缩文件
        with gzip.open(filename) as bytestream:
            bytestream.read(head_size)
            buf = bytestream.read(data_size * num_data)
            data = np.frombuffer(buf,dtype=np.uint8).astype(np.float)
        return data
    data = extract_data(data_dir+'/train-images-idx3-ubyte.gz',60000,16,28 * 28)
    trX = data.reshape(60000,28,28,1)
    data = extract_data(data_dir+'/train-labels-idx1-ubyte.gz',60000,8,1)
    trY = data.reshape(60000)
    data = extract_data(data_dir+'/t10k-images-idx3-ubyte.gz',10000,16,28 * 28)
    teX = data.reshape(10000,28,28,1)
    data = extract_data(data_dir+'/t10k-labels-idx1-ubyte.gz',10000,8,1)
    teY = data.reshape(10000)
    
    trY = np.asarray(trY)
    teY = np.asarray(teY)
    
    #分别将训练集和测试集数字和标签合在一起,标签y目前为0-9数字
    x = np.concatenate((trX,teX),axis=0)
    y = np.concatenate((trY,teY),axis=0).astype(np.int)
    
    seed = 547
    np.random.seed(seed)
    np.random.shuffle(x)
    np.random.seed(seed)
    np.random.shuffle(y)
    
    #将标签变为one_hot形式
    y_vec = np.zeros((len(y),10),dtype=np.float)
    for i,label in enumerate(y):
        y_vec[i,y[i]] = 1.0
        
    return x/255.,y_vec
train,label = load_mnist('data')
print(train.shape)
print(label.shape)

 

你可能感兴趣的:(Python)