Skip to content

Instantly share code, notes, and snippets.

@vbop9834
Last active March 10, 2017 17:11
Show Gist options
  • Save vbop9834/c78e570e755f259edad8677cc48b79a8 to your computer and use it in GitHub Desktop.
Save vbop9834/c78e570e755f259edad8677cc48b79a8 to your computer and use it in GitHub Desktop.
Elixir functions for parsing Mnist binary data - http://yann.lecun.com/exdb/mnist/
defmodule MnistDataReader do
def read_training_image_data(path) do
{:ok, data} = File.read path
parse_training_image_data data
end
def read_training_label_data(path) do
{:ok, data} = File.read path
parse_training_label_data data
end
defp parse_training_label_data(binary_label_data) do
#Read only the first 100 labels for experimentation
<<
_magic_number :: size(32),
_number_of_items :: size(32),
labels :: binary-size(100),
_remaining_binary :: binary
>> = binary_label_data
labels = :binary.bin_to_list labels
labels
end
defp parse_training_image_data(binary_data) do
<<
_magic_number :: size(32),
_number_of_images :: size(32),
number_of_rows :: size(32),
number_of_columns :: size(32),
images_data :: binary
>> = binary_data
#Read only the first 100 images for experimentation
image_size = number_of_rows*number_of_columns
image_binary_size = image_size*100
<<
images_data :: binary-size(image_binary_size),
_remaining_binary :: binary
>> = images_data
raw_images_list = :binary.bin_to_list images_data
images = Enum.chunk(raw_images_list, (number_of_rows*number_of_columns))
images
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment