Procedure to load MNIST with python and output to png

Thing you want to do

We are collecting samples of learning / verification data for machine learning.

Premise

What is MNIST?

Image data of "handwritten numbers" from 0 to 9. Used for machine learning such as "identify and classify handwritten numbers with AI". http://yann.lecun.com/exdb/mnist/ You can download it for free from.

image.png

file organization

The contents of the file

When you unzip the gz file, it becomes a binary file like the one below.

t10k-images.idx3-ubyte

image.png

Even though it is image data, it is not in a format like .jpg, so It cannot be previewed as it is.

How to image and preview

For example, if you write python code and output it to png with numpy or PIL, you can display it as an ordinary image file.

image.png

Advance preparation

If wget and unzip are not included yet, install them. For ubuntu:

apt-get install -y wget
apt-get install unzip

File download

First, download gz with wget.

wget http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
wget http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz

Then unzip.

gunzip train-images-idx3-ubyte.gz
gunzip train-labels-idx1-ubyte.gz

Then

-rw-r--r--. 1 root root 47040016 Jul 21  2000 train-images-idx3-ubyte
-rw-r--r--. 1 root root    60008 Jul 21  2000 train-labels-idx1-ubyte

Is output. Then write the python code.

Implementation (Images imaging)

vi test.py

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.datasets import mnist

import os
import numpy as np
import matplotlib.pyplot as plt
import struct
from PIL import Image

trainImagesFile = open('./train-images-idx3-ubyte','rb')
trainLabelsFile = open('./train-labels-idx1-ubyte','rb')

f = trainImagesFile

magic_number = f.read( 4 )
magic_number = struct.unpack('>i', magic_number)[0]

number_of_images = f.read( 4 )
number_of_images = struct.unpack('>i', number_of_images)[0]

number_of_rows = f.read( 4 )
number_of_rows = struct.unpack('>i', number_of_rows)[0]

number_of_columns = f.read( 4 )
number_of_columns = struct.unpack('>i', number_of_columns)[0]

bytes_per_image = number_of_rows * number_of_columns

raw_img = f.read(bytes_per_image)
format = '%dB' % bytes_per_image
lin_img = struct.unpack(format, raw_img)
np_ary = np.asarray(lin_img).astype('uint8')
np_ary = np.reshape(np_ary, (28,28),order='C')

pil_img = Image.fromarray(np_ary)
pil_img.save("output.png ")

Run

python test.py

Output result

output.png

image.png

Commentary

Image data for learning train-images-idx3-ubyte The structure of is as follows.

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

TRAINING SET IMAGE FILE (train-images-idx3-ubyte):
[offset] [type]          [value]          [description]
0000     32 bit integer  0x00000803(2051) magic number
0004     32 bit integer  60000            number of images
0008     32 bit integer  28               number of rows
0012     32 bit integer  28               number of columns
0016     unsigned byte   ??               pixel
0017     unsigned byte   ??               pixel
........
xxxx     unsigned byte   ??               pixel

According to the above, the offset is read sequentially while shifting by 4.

magic_number = f.read( 4 )

The result is 2051.

number_of_images = f.read( 4 )

The result is 60000.

number_of_rows = f.read( 4 )

The result is 28.

number_of_columns = f.read( 4 )

The result is 28.

If you really want to see the value

print('--------------------')
print('magic_number');
print(magic_number);
print('--------------------')
print('number_of_images');
print(number_of_images);
print('--------------------')
print('number_of_rows');
print(number_of_rows);
print('--------------------')
print('number_of_columns');
print(number_of_columns);

You can check it by outputting as follows.

--------------------
magic_number
2051
--------------------
number_of_images
60000
--------------------
number_of_rows
28
--------------------
number_of_columns
28

And

[offset] [type]          [value]          [description]
0016     unsigned byte   ??               pixel

Since it is, images are included after offset 16

bytes_per_image = number_of_rows * number_of_columns
raw_img = f.read(bytes_per_image)

Can be read as. After that, I thrust it into numpy and save it in png format.

Output png continuously by loop processing

If you rotate the png output process in a loop as shown below, you can continuously image. After that, if you want to output 10 sheets, you can specify the number of loops as you like, like range (10) :.

for num in range(10):
    raw_img = f.read(bytes_per_image)
    format = '%dB' % bytes_per_image
    lin_img = struct.unpack(format, raw_img)
    np_ary = np.asarray(lin_img).astype('uint8')
    np_ary = np.reshape(np_ary, (28,28),order='C')
    pil_img = Image.fromarray(np_ary)
    pil_img.save("output" + str(num)  + ".png ")

The output result is below.

image.png

Comparison of numpy array and png

When np_ary, which is a numpy array, is displayed by print (), the array data is as follows.

 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   3  18  18  18 126 136  175  26 166 255 247 127   0   0   0   0]
 [  0   0   0   0   0   0   0   0  30  36  94 154 170 253 253 253 253 253  225 172 253 242 195  64   0   0   0   0]
 [  0   0   0   0   0   0   0  49 238 253 253 253 253 253 253 253 253 251   93  82  82  56  39   0   0   0   0   0]
 [  0   0   0   0   0   0   0  18 219 253 253 253 253 253 198 182 247 241    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0  80 156 107 253 253 205  11   0  43 154    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0  14   1 154 253  90   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0 139 253 190   2   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0  11 190 253  70   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0  35 241 225 160 108   1    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0  81 240 253 253 119   25   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  45 186 253 253  150  27   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  16  93 252  253 187   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 249  253 249  64   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  46 130 183 253  253 207   2   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0  39 148 229 253 253 253  250 182   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0  24 114 221 253 253 253 253 201   78   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0  23  66 213 253 253 253 253 198  81   2    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0  18 171 219 253 253 253 253 195  80   9   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0  55 172 226 253 253 253 253 244 133  11   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0 136 253 253 253 212 135 132  16   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0    0   0   0   0   0   0   0   0   0   0]


It can be seen that this is the position of each pixel constituting the image file and its color information.

image.png

About label data

The above implementation was to convert image data (Images) to png, In addition to this, it is also necessary to check the label data (Labels). The implementation for that is as follows.

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.datasets import mnist

import os
import numpy as np
import matplotlib.pyplot as plt
import struct
from PIL import Image

trainImagesFile = open('./train-images-idx3-ubyte','rb')
trainLabelsFile = open('./train-labels-idx1-ubyte','rb')

f = trainLabelsFile

magic_number = f.read( 4 )
magic_number = struct.unpack('>i', magic_number)[0]

number_of_images = f.read( 4 )
number_of_images = struct.unpack('>i', number_of_images)[0]

print("--------------------")
print("magic_number")
print(magic_number)
print("--------------------")
print("number_of_image")
print(number_of_images)
print("--------------------")

label_byte = f.read( 1 )
label_int = int.from_bytes(label_byte, byteorder='big')
print(label_int)

Output result

--------------------
magic_number
2049
--------------------
number_of_image
60000
--------------------
5

The structure of the label data is as follows.

train-labels-idx1-ubyte

TRAINING SET LABEL FILE (train-labels-idx1-ubyte):
[offset] [type]          [value]          [description]
0000     32 bit integer  0x00000801(2049) magic number (MSB first)
0004     32 bit integer  60000            number of items
0008     unsigned byte   ??               label
0009     unsigned byte   ??               label
........
xxxx     unsigned byte   ??               label
The labels values are 0 to 9.

In other words, if you read offset 8 and later one by one, you can read the label. If you implement it in a loop, follow below.


for num in range(10):
    label_byte = f.read( 1 )
    label_int = int.from_bytes(label_byte, byteorder='big')
    print(label_int)

The output result is below.

5
0
4
1
9
2
1
3
1
4

Compare with the output result of Images.

image.png

Each png image correctly indicates "what number is it?" With a label.

Recommended Posts

Procedure to load MNIST with python and output to png
Output to csv file with Python
Output color characters to pretty with python
Output Python log to console with GAE
Fractal to make and play with Python
[Python] Introduction to CNN with Pytorch MNIST
Convert HEIC files to PNG files with Python
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Convert DICOM to PNG with Ascending and Descending
Convert PDF to image (JPEG / PNG) with Python
Read JSON with Python and output as CSV
Convert svg file to png / ico with Python
I tried to output LLVM IR with Python
How to convert SVG to PDF and PNG [Python]
Output python log to both console and file
python input and output
I want to handle optimization with python and cplex
Try to operate DB with Python and visualize with d3
Read json file with Python, format it, and output json
[Python-pptx] Output PowerPoint font information to csv with python
Something to enjoy with Prim Pro (X-Play) and Python
It is easy to execute SQL with Python and output the result in Excel
Programming with Python and Tkinter
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
Try Python output with Haxe 3.2
Connect to Wikipedia with Python
Post to slack with Python 3
Output to syslog with Loguru
Switch python to 2.7 with alternatives
Write to csv with Python
python with pyenv and venv
Works with Python and R
Easy to use Nifty Cloud API with botocore and python
screen and split screen with python and ssh login to remote server
[Python] How to play with class variables with decorator and metaclass
Installation procedure for Python and Ansible with a specific version
Send experiment results (text and images) to slack with Python
Try to bring up a subwindow with PyQt5 and Python
How to do Bulk Update with PyMySQL and notes [Python]
[Let's play with Python] Image processing to monochrome and dots
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
Get additional data to LDAP with python (Writer and Reader)
How to log in to AtCoder with Python and submit automatically
Get the source of the page to load infinitely with python.
Communicate with FX-5204PS with Python and PyUSB
Convert to a string while outputting standard output with Python subprocess
Extract bigquery dataset and table list with python and output as CSV
Operate Jupyter with REST API to extract and save Python code
Made it possible to convert PNG to JPG with Pillow of Python
Python: How to use async with
Robot running with Arduino and python
Python 3.6 on Windows ... and to Xamarin.
Install Python 2.7.9 and Python 3.4.x with pip.
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
Link to get started with python
[Python] font family and font with matplotlib
[Introduction to Python3 Day 1] Programming and Python