Read MNIST label information
This is a sample program that reads MNIST label information in Perl. MNIST is a sample image that can be used in deep learning.
This is a sample to read THE MNIST DATABASE of handwritten digits.
use strict;
use warnings;
use FindBin;
# MNIST Read label information
my $mnist_label_file = "$FindBin::Bin / data / train-labels-idx1-ubyte";
open my $mnist_label_fh,'<', $mnist_label_file
or die "Can't open file $mnist_label_file: $!";
#Magic number
my $label_buffer;
read($mnist_label_fh, $label_buffer, 4);
my $magic_number = unpack('N1', $label_buffer);
if ($magic_number! = 0x00000801) {
die "Invalid magic number expected". 0x00000801. "actual $magic_number";
}
# Number of labels
read($mnist_label_fh, $label_buffer, 4);
my $items_count = unpack('N1', $label_buffer);
#Read label
my $label_numbers = [];
for (my $i = 0; $i <$items_count; $i ++) {
read $mnist_label_fh, $label_buffer, 1;
my $label_number = unpack('C1', $label_buffer);
push @$label_numbers, $label_number;
}
# Label information
my $label_info = {};
$label_info->{items_count} = $items_count;
$label_info->{label_numbers} = $label_numbers;
A brief explanation of the program that reads MNIST label information
Place "train-labels-idx1-ubyte" under "data".
Open the file and read it with the read function.
Check the magic number. Since it is a big endian 32-bit integer, specify "N1" for unpack.
Get the number of labels. Since it is a big endian 32-bit integer, specify "N1" for unpack.
I will read the label. Since one piece of information on the label is an unsigned 8-bit integer, specify "C1".
The label information for training "train-labels-idx1-ubyte" is read, but the label information for verification "t10k-labels-idx1-ubyte" can also be read in the same way.
Perl AI Deep Learning Tutorial