Неполадка при составлении MNIST цифр - PullRequest
2 голосов
/ 06 мая 2019

Я пытаюсь загрузить и визуализировать цифры MNIST, но я получаю цифры со смещенным пикселем

import matplotlib.pyplot as plt
import numpy as np

mnist_data  = open('data/mnist/train-images-idx3-ubyte', 'rb')

image_size = 28
num_images = 4

buf = mnist_data.read(num_images * image_size * image_size)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size)

_, axarr1 = plt.subplots(2,2)
axarr1[0, 0].imshow(data[0])
axarr1[0, 1].imshow(data[1])
axarr1[1, 0].imshow(data[2])
axarr1[1, 1].imshow(data[3])

MNIST

Может кто-нибудь сказать мне, почемукод происходит нормально, спасибо

1 Ответ

1 голос
/ 06 мая 2019

Вы не говорите, где вы получили данные MNIST, но, , если они отформатированы как исходный набор данных , вы, кажется, забыли извлечь заголовок, прежде чем пытаться получить доступ к данным:

image_size = 28
num_images = 4

mnist_data = open('train-images-idx3-ubyte', 'rb')

mnist_data.seek(16) # skip over the first 16 bytes that correspond to the header
buf = mnist_data.read(num_images * image_size * image_size)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size)

_, axarr1 = plt.subplots(2,2)
axarr1[0, 0].imshow(data[0])
axarr1[0, 1].imshow(data[1])
axarr1[1, 0].imshow(data[2])
axarr1[1, 1].imshow(data[3])

enter image description here

...