转载:https://blog.csdn.net/opipa/article/details/51882875
标签注噪用到,FASHION MNIST 数据集
标签文件的读取方法:
import struct
from array import array
with open("train-labels-idx1-ubyte", "rb") as f:
magic, size = struct.unpack(">II", f.read(8))
labels = array("B", f.read())
print magic, size, labels
注意:第四行不能删,前面八个不是标签
这个是图片文件的读取方法(ipython下):
import struct
from array import array
with open("t10k-images-idx3-ubyte", "rb") as f:
magic, size, rows, cols = struct.unpack(">IIII", f.read(16))
print magic, size, rows, cols
image_data = array("B", f.read())
images = []
for i in range(size):
images.append([0] * rows * cols)
for i in range(size):
images[i][:] = image_data[i * rows * cols:(i + 1) * rows * cols]
显示前72幅图片:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline
for i,img in enumerate(images):
if i < 72:
plt.subplot(9,8,i+1)
img = np.array(img)
img = img.reshape(rows,cols)
img = Image.fromarray(img)
plt.imshow(img, cmap='gray')
plt.axis("off")
else:
break