Module: DNN::MNIST

Defined in:
lib/dnn/lib/mnist.rb

Defined Under Namespace

Classes: DNN_MNIST_LoadError

Constant Summary collapse

URL_TRAIN_IMAGES =
"http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz"
URL_TRAIN_LABELS =
"http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz"
URL_TEST_IMAGES =
"http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz"
URL_TEST_LABELS =
"http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz"

Class Method Summary collapse

Class Method Details

.downloadsObject



14
15
16
17
18
19
20
21
# File 'lib/dnn/lib/mnist.rb', line 14

def self.downloads
  return if Dir.exist?(mnist_dir)
  Dir.mkdir(mnist_dir)
  Downloader.download(URL_TRAIN_IMAGES, mnist_dir)
  Downloader.download(URL_TRAIN_LABELS, mnist_dir)
  Downloader.download(URL_TEST_IMAGES, mnist_dir)
  Downloader.download(URL_TEST_LABELS, mnist_dir)
end

.load_images(file_name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/dnn/lib/mnist.rb', line 55

def self.load_images(file_name)
  images = nil
  Zlib::GzipReader.open(file_name) do |f|
    magic, num_images = f.read(8).unpack("N2")
    rows, cols = f.read(8).unpack("N2")
    images = Numo::UInt8.from_binary(f.read)
    images = images.reshape(num_images, cols, rows)
  end
  images
end

.load_labels(file_name) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/dnn/lib/mnist.rb', line 66

def self.load_labels(file_name)
  labels = nil
  Zlib::GzipReader.open(file_name) do |f|
    magic, num_labels = f.read(8).unpack("N2")
    labels = Numo::UInt8.from_binary(f.read)
  end
  labels
end

.load_testObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dnn/lib/mnist.rb', line 38

def self.load_test
  downloads
  test_images_file_name = url_to_file_name(URL_TEST_IMAGES)
  test_labels_file_name = url_to_file_name(URL_TEST_LABELS)
  unless File.exist?(test_images_file_name)
    raise DNN_MNIST_LoadError.new(%`file "#{train_images_file_name}" is not found.`)
  end
  unless File.exist?(test_labels_file_name)
    raise DNN_MNIST_LoadError.new(%`file "#{train_labels_file_name}" is not found.`)
  end
  images = load_images(test_images_file_name)
  labels = load_labels(test_labels_file_name)
  [images, labels]
end

.load_trainObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dnn/lib/mnist.rb', line 23

def self.load_train
  downloads
  train_images_file_name = url_to_file_name(URL_TRAIN_IMAGES)
  train_labels_file_name = url_to_file_name(URL_TRAIN_LABELS)
  unless File.exist?(train_images_file_name)
    raise DNN_MNIST_LoadError.new(%`file "#{train_images_file_name}" is not found.`)
  end
  unless File.exist?(train_labels_file_name)
    raise DNN_MNIST_LoadError.new(%`file "#{train_labels_file_name}" is not found.`)
  end
  images = load_images(train_images_file_name)
  labels = load_labels(train_labels_file_name)
  [images, labels]
end

.mnist_dirObject



75
76
77
# File 'lib/dnn/lib/mnist.rb', line 75

def self.mnist_dir
  __dir__ + "/mnist"
end

.url_to_file_name(url) ⇒ Object



79
80
81
# File 'lib/dnn/lib/mnist.rb', line 79

def self.url_to_file_name(url)
  mnist_dir + "/" + url.match(%r`.+/(.+)$`)[1]
end