Class: Daimond::Data::MNIST
- Inherits:
-
Object
- Object
- Daimond::Data::MNIST
- Defined in:
- lib/daimond/data/mnist.rb
Constant Summary collapse
- URL_BASE =
'https://ossci-datasets.s3.amazonaws.com/mnist/'- FILES =
{ train_images: 'train-images-idx3-ubyte.gz', train_labels: 'train-labels-idx1-ubyte.gz', test_images: 't10k-images-idx3-ubyte.gz', test_labels: 't10k-labels-idx1-ubyte.gz' }
Class Method Summary collapse
- .download(file, path: 'data/mnist') ⇒ Object
- .load_images(file) ⇒ Object
- .load_labels(file) ⇒ Object
Class Method Details
.download(file, path: 'data/mnist') ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/daimond/data/mnist.rb', line 16 def self.download(file, path: 'data/mnist') ::FileUtils.mkdir_p(path) # <-- Здесь изменение: создаёт и data, и data/mnist filepath = File.join(path, file) unless File.exist?(filepath) puts "Downloading #{file}..." URI.open("#{URL_BASE}#{file}") do |f| File.open(filepath, 'wb') { |out| out.write(f.read) } end end filepath end |
.load_images(file) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/daimond/data/mnist.rb', line 29 def self.load_images(file) filepath = download(file) Zlib::GzipReader.open(filepath) do |f| magic = f.read(4).unpack('N').first n_images = f.read(4).unpack('N').first n_rows = f.read(4).unpack('N').first n_cols = f.read(4).unpack('N').first images = f.read(n_images * n_rows * n_cols).unpack('C*') # Нормализация в [0, 1] и reshape в [n_images, 784] images.each_slice(n_rows * n_cols).map do |img| img.map { |pixel| pixel / 255.0 } end end end |
.load_labels(file) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/daimond/data/mnist.rb', line 45 def self.load_labels(file) filepath = download(file) Zlib::GzipReader.open(filepath) do |f| magic = f.read(4).unpack('N').first n_labels = f.read(4).unpack('N').first labels = f.read(n_labels).unpack('C*') labels end end |