Python读取MNIST数据集

MNIST数据集下载地址:http://yann.lecun.com/exdb/mnist/

读取MINST数据集第一张图像并显示

# coding=utf-8
import numpy as np
import cv2
import os
from PIL import Image

def load_mnist(mnist_image_file, mnist_label_file):
    with open(mnist_image_file, 'rb') as f1:
        image_file = np.frombuffer(f1.read(), np.uint8, offset=16).reshape(-1,28*28)
    with open(mnist_label_file, 'rb') as f2:
        label_file = np.frombuffer(f2.read(), np.uint8, offset=8)
    img = Image.fromarray(image_file[0].reshape(28,28)) # First image in the training set.
    img.show() # Show the image

if __name__ == '__main__':
    train_image_file = './train-images.idx3-ubyte'
    train_label_file = './train-labels.idx1-ubyte'

    load_mnist(train_image_file, train_label_file)

读取结果:

Guess you like

Origin blog.csdn.net/flyconley/article/details/119808751