Jeruliu2018
Jeruliu2018

Reputation: 11

Mnist data image and label mismatch

I know this could be a stupid question but I really can’t figure out why. Below is the code that I tried to print a single image and label with same index from train data at

import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import      input_data
import tensorflow as tf

mndata = input_data.read_data_sets("MNIST_data/", one_hot=True)

images_train=mndata.train.images    # training set
labels_train=mndata.train.labels    
images_test=mndata.test.images     # testing set
labels_test=mndata.test.labels

##### Testing single image to network

train_index=4

image = images_train[train_index]
label = labels_train[train_index]

plt.imshow(image.reshape(28,28))
plt.show()  
print('label',label)

The image shown as one But label is [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]

Upvotes: 1

Views: 913

Answers (1)

S-Amiral KAB
S-Amiral KAB

Reputation: 11

in the file "train-labels.idx1-ubyte" you have an offset, look:

[offset] [type] [value] [description]

0000 32 bit integer 0x00000801(2049) magic number (MSB first)

0004 32 bit integer 10000 number of items

0008 unsigned byte ?? label

0009 unsigned byte ?? label

........

xxxx unsigned byte ?? label

Add an offset of 8 and it will be done!

sources: http://yann.lecun.com/exdb/mnist/

Upvotes: 1

Related Questions