Skip to content

Instantly share code, notes, and snippets.

@Godisemo
Last active April 29, 2020 13:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Godisemo/e0c02716e168bdca499847cb2c6842de to your computer and use it in GitHub Desktop.
Save Godisemo/e0c02716e168bdca499847cb2c6842de to your computer and use it in GitHub Desktop.
Julia parser and writer for the IDX file format used by e.g. the MNIST dataset
const idx_types = [(UInt8, 0x08), (Int8, 0x09), (Int16, 0x0B), (Int32, 0x0C), (Float32, 0x0D), (Float64, 0x0E)]
const typefromcode = Dict(idx_types)
const codefromtype = Dict(map(reverse, idx_types))
function write_idx(stream::Union{IOStream,String}, data::Array{T,N}) where {T,N}
dims = convert(Array{Int32}, collect(size(data)))
write(stream, 0x00, 0x00, typefromcode[T], UInt8(N), hton.(dims), hton.(data))
end
read_idx(filename::String) = open(read_idx, filename)
function read_idx(stream::IOStream)
header = read(stream, 4)
dims = ntoh.(read!(stream, Array{Int32}(undef, header[4])))
data = ntoh.(read!(stream, Array{codefromtype[header[3]]}(undef, dims...)))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment