Sha256: f1d700776b203bcc4106a407fac8e30f9635926368b0cabddfcc9c7bb8851b97

Contents?: true

Size: 1.61 KB

Versions: 4

Compression:

Stored size: 1.61 KB

Contents

require "dnn"
require "dnn/ext/cifar10/cifar10_ext"
require "open-uri"
require "zlib"
require "archive/tar/minitar"

URL_CIFAR10 = "https://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz"
CIFAR10_DIR = "cifar-10-batches-bin"

module DNN
  module CIFAR10
    class DNN_CIFAR10_LoadError < DNN_Error; end

    class DNN_CIFAR10_DownloadError < DNN_Error; end

    private_class_method :_cifar10_load

    def self.downloads
      return if Dir.exist?(__dir__ + "/" + CIFAR10_DIR)
      cifar10_binary_file_name = __dir__ + "/" + URL_CIFAR10.match(%r`.+/(.+)`)[1]
      puts "Now downloading..."
      open(URL_CIFAR10, "rb") do |f|
        File.binwrite(cifar10_binary_file_name, f.read)
        Zlib::GzipReader.open(cifar10_binary_file_name) do |gz|
          Archive::Tar::Minitar::unpack(gz, __dir__)
        end
      end
      File.delete(cifar10_binary_file_name)
      puts "The download has ended."
    rescue => ex
      raise DNN_CIFAR10_DownloadError.new(ex.message)
    end

    def self.load_train
      downloads
      bin = ""
      (1..5).each do |i|
        fname = __dir__ + "/#{CIFAR10_DIR}/data_batch_#{i}.bin"
        raise DNN_CIFAR10_LoadError.new(%`file "#{fname}" is not found.`) unless File.exist?(fname)
        bin << File.binread(fname)
      end
      _cifar10_load(bin, 50000)
    end

    def self.load_test
      downloads
      fname = __dir__ + "/#{CIFAR10_DIR}/test_batch.bin"
      raise DNN_CIFAR10_LoadError.new(%`file "#{fname}" is not found.`) unless File.exist?(fname)
      bin = File.binread(fname)
      _cifar10_load(bin, 10000)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-dnn-0.1.6 lib/dnn/lib/cifar10.rb
ruby-dnn-0.1.5 lib/dnn/lib/cifar10.rb
ruby-dnn-0.1.4 lib/dnn/lib/cifar10.rb
ruby-dnn-0.1.3 lib/dnn/lib/cifar10.rb