module Mnist

Constants

VERSION

Public Class Methods

load_images(filename) click to toggle source
# File lib/mnist-learn.rb, line 158
def self.load_images(filename)
  Loader.new(filename).load_images
end
load_labels(filename) click to toggle source
# File lib/mnist-learn.rb, line 162
def self.load_labels(filename)
  Loader.new(filename).load_labels
end
read_data_sets(path, one_hot: false) click to toggle source
# File lib/mnist-learn.rb, line 166
def self.read_data_sets(path, one_hot: false)
  unless Dir.exist?(path)
    FileUtils.mkdir_p path
  end

  base_url = "yann.lecun.com"
  filenames = [
    "train-images-idx3-ubyte.gz",
    "train-labels-idx1-ubyte.gz",
    "t10k-images-idx3-ubyte.gz",
    "t10k-labels-idx1-ubyte.gz"
  ]
  Net::HTTP.start(base_url) do |http|
    filenames.each do |name|
      unless File.exists?(File.join(path, name))
        f = File.open(File.join(path, name), "wb")
        begin
          http.request_get('/exdb/mnist/' + name) do |resp|
            resp.read_body do |segment|
              f.write(segment)
            end
          end
        ensure
          f.close
        end
      end
    end
  end

  filenames.each do |name|
    next if File.exists?(File.join(path, File.basename(name, '.gz')))
    puts "extracting #{name} ..."
    Zlib::GzipReader.open(File.join(path, name)) do |zipfile|
      outfile = File.open(File.join(path, File.basename(name, '.gz')), 'wb')
      outfile.write(zipfile.read)
    end
  end
  MnistReader.new(path, one_hot)
end