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
24 changes: 21 additions & 3 deletions lib/datasets/mnist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Datasets
class MNIST < Dataset
BASE_URL = "http://yann.lecun.com/exdb/mnist/"
FALLBACK_URL = "https://ossci-datasets.s3.amazonaws.com/mnist/"

class Record < Struct.new(:data, :label)
def pixels
Expand Down Expand Up @@ -45,9 +46,26 @@ 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))
fallback_url = self.class::FALLBACK_URL

begin
download(image_path, base_url + target_file(:image))
rescue Net::HTTPClientException => error
if error.response.code == "403"
download(image_path, fallback_url + target_file(:image))
else
raise error
end
end
begin
download(label_path, base_url + target_file(:label))
rescue Net::HTTPClientException => error
if error.response.code == "403"
download(label_path, fallback_url + target_file(:label))
else
raise error
end
end

open_data(image_path, label_path, &block)
end
Expand Down
Loading