Last active
October 3, 2021 00:54
-
-
Save Splines/0075bd9e37d08ac058680b631e7cc988 to your computer and use it in GitHub Desktop.
Parse the MNIST images header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# --- Images Header | |
# 4x 32-bit big-endian integer | |
images_header = np.fromfile(images_file, dtype='>i4', count=4) | |
images_magic_number = images_header[0] | |
images_count = images_header[1] | |
row_count = images_header[2] | |
col_count = images_header[3] | |
pixel_count = row_count*col_count | |
print(f'magic number\t {images_magic_number}') | |
print(f'#images\t\t {images_count}') | |
print(f'#rows\t\t {row_count}') | |
print(f'#cols\t\t {col_count}') | |
print(f'#pixels\t\t {pixel_count} ({row_count}*{col_count})') | |
# --- Images Data | |
images_buffer = images_file.read(pixel_count * images_count) | |
images = np.frombuffer(images_buffer, dtype=np.uint8) | |
images = images.reshape(images_count, pixel_count) | |
# --- Plot first image | |
img = images[0].reshape((28, 28)) | |
plt.imshow(img, cmap='gray') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment