Class: Datasets::MNIST

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

Direct Known Subclasses

FashionMNIST, KuzushijiMNIST

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
40
# 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
  @metadata.licenses = licenses
  @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



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

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

  download(image_path, base_url + target_file(:image))
  download(label_path, base_url + target_file(:label))

  open_data(image_path, label_path, &block)
end