Skip to content

mnist: use mirror only when original server is not available #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 9, 2024
Merged
18 changes: 15 additions & 3 deletions lib/datasets/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,21 @@ def cache_path
@cache_path ||= CachePath.new(@metadata.id)
end

def download(output_path, url, &block)
downloader = Downloader.new(url)
downloader.download(output_path, &block)
def download(output_path, *urls, &block)
urls.each do |url|
downloader = Downloader.new(url)
downloader.download(output_path, &block)
return
rescue Net::HTTPClientException => error
if urls.last != url
message = "site is not available: " +
"#{error.class}: #{error.message}: " +
"Attempting to download from an alternative site"
$stderr.puts(message)
else
raise error
end
end
end

def extract_bz2(bz2)
Expand Down
8 changes: 6 additions & 2 deletions lib/datasets/fashion-mnist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

module Datasets
class FashionMNIST < MNIST
BASE_URL = "http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/"

private
def base_urls
[
"http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/",
]
end

def dataset_name
"Fashion-MNIST"
end
Expand Down
8 changes: 6 additions & 2 deletions lib/datasets/kuzushiji-mnist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

module Datasets
class KuzushijiMNIST < MNIST
BASE_URL = "http://codh.rois.ac.jp/kmnist/dataset/kmnist/"

private
def base_urls
[
"http://codh.rois.ac.jp/kmnist/dataset/kmnist/",
]
end

def dataset_name
"Kuzushiji-MNIST"
end
Expand Down
18 changes: 12 additions & 6 deletions lib/datasets/mnist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

module Datasets
class MNIST < Dataset
BASE_URL = "http://yann.lecun.com/exdb/mnist/"

class Record < Struct.new(:data, :label)
def pixels
data.unpack("C*")
Expand All @@ -27,7 +25,7 @@ def initialize(type: :train)

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

Expand All @@ -44,15 +42,23 @@ def each(&block)

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))
download(image_path,
*base_urls.collect { |base_url| base_url + target_file(:image) })
download(label_path,
*base_urls.collect { |base_url| base_url + target_file(:label) })

open_data(image_path, label_path, &block)
end

private
def base_urls
[
"http://yann.lecun.com/exdb/mnist/",
"https://ossci-datasets.s3.amazonaws.com/mnist/",
]
end

def licenses
[]
end
Expand Down
Loading