Class: Datasets::MNIST

Inherits:
Dataset show all
Defined in:
lib/datasets/mnist.rb

Direct Known Subclasses

FashionMNIST

Defined Under Namespace

Classes: Record

Constant Summary collapse

BASE_URL =
"http://yann.lecun.com/exdb/mnist/"

Instance Attribute Summary

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#clear_cache!, #to_table

Constructor Details

#initialize(type: :train) ⇒ MNIST

Returns a new instance of MNIST.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/datasets/mnist.rb', line 21

def initialize(type: :train)
  unless [:train, :test].include?(type)
    raise ArgumentError, "Please set type :train or :test: #{type.inspect}"
  end

  super()

  @metadata.id = "#{dataset_name.downcase}-#{type}"
  @metadata.name = "#{dataset_name}: #{type}"
  @metadata.url = self.class::BASE_URL
  @type = type

  case type
  when :train
    @metadata.description = "a training set of 60,000 examples"
  when :test
    @metadata.description = "a test set of 10,000 examples"
  end
end

Instance Method Details

#each(&block) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/datasets/mnist.rb', line 41

def each(&block)
  return to_enum(__method__) unless block_given?

  image_path = cache_dir_path + target_file(:image)
  label_path = cache_dir_path + target_file(:label)
  base_url = self.class::BASE_URL

  unless image_path.exist?
    download(image_path, base_url + target_file(:image))
  end

  unless label_path.exist?
    download(label_path, base_url + target_file(:label))
  end

  open_data(image_path, label_path, &block)
end