Python解析MNIST資料集

2021-09-19 19:25:44 字數 2186 閱讀 6149

#coding=utf-8

import numpy as np

import struct

import matplotlib.pyplot as plt

def parese_idx3(idx3_file):

"""idx3檔案解析方法

:param idx3_file: idx3檔案路徑

:return: 資料集

"""# 讀取二進位制資料

bin_data = open(idx3_file, 'rb').read()

# 解析檔案頭資訊 magic、imgs、height、width

# '>iiii'是說使用大端法讀取4個unsinged int32

offset = 0

fmt_header = '>iiii'

magic, imgs, height, width = struct.unpack_from(fmt_header, bin_data, offset)

print ('magic:%d, imgs: %d, heightxwidth: %dx%d' % (magic, imgs, height, width))

# 解析資料集

image_size = height * width

offset += struct.calcsize(fmt_header)

fmt_image = '>' + str(image_size) + 'b'

images = np.empty((imgs, height, width))

for i in range(imgs):

if (i + 1) % 10000 == 0:

print ('已解析 %d' % (i + 1) + '張');

images[i] = np.array(struct.unpack_from(fmt_image, bin_data, offset)).reshape((height, width))

offset += struct.calcsize(fmt_image)

return images

def parese_idx1(idx1_file):

"""idx1檔案解析方法

:param idx1_file: idx1檔案路徑

:return: 資料集

"""# 讀取二進位制資料

bin_data = open(idx1_file, 'rb').read()

# 解析檔案頭資訊 magic、imgs

offset = 0

fmt_header = '>ii'

magic, imgs = struct.unpack_from(fmt_header, bin_data, offset)

print ('magic:%d, imgs: %d' % (magic, imgs))

# 解析資料集

offset += struct.calcsize(fmt_header)

fmt_image = '>b'

labels = np.empty(imgs)

for i in range(imgs):

if (i + 1) % 10000 == 0:

print ('已解析 %d' % (i + 1) + '張')

lab1是img1的標籤資訊

C語言解析MNIST資料集

如下 bmp.h ifndef bmp h define bmp h pragma pack 1 typedef struct tagbitmapfileheader fileheader pragma pack 位圖資料資訊結構 pragma pack 1 typedef struct tagbi...

tensorflow的MNIST教程解析

原址 eval 函式 計算字串中有效的表示式 將字串轉化為相應的物件 解析字串有效的表示式.1.interactivesession 和session 的區別 tf.interactivesession 預設自己就是使用者要操作的session,而tf.session 沒有這個預設,因此用eval ...

MNIST資料製作

大端 較高的有效位元組存放在較低的儲存器位址,較低的有效位元組存放在較高的儲存器位址。小端 較高的有效位元組存放在較高的的儲存器位址,較低的有效位元組存放在較低的儲存器位址。如果將乙個32位的整數0x12345678存放到乙個整型變數 int 中,這個整型變數採用大端或者小端模式在記憶體中的儲存由下...