Skip to content

Commit

Permalink
FIX cache_or_load_file method And load mnist
Browse files Browse the repository at this point in the history
  • Loading branch information
hatappi committed Sep 30, 2017
1 parent 5f92a43 commit cc7685f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
19 changes: 13 additions & 6 deletions lib/chainer/dataset/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ def self.get_dataset_directory(dataset_name, create_directory: true)
path
end

def self.cache_or_load_file(path, data)
return PStore.new(path).transaction { |t| t['data'] } if File.exist?(path)
def self.cache_or_load_file(path, &creator)
raise 'Please set dataset creator on block' if creator.nil?

pstore = PStore.new(path)
pstore.transaction{|t|
t["data"] = data
}
return PStore.new(path).transaction { |t| t['data'] } if File.exist?(path)

data = creator.call
PStore.new(path).transaction do |t|
t['data'] = data
end
data
rescue TypeError => e
puts e.message
FileUtils.rm_f(path)
cache_or_load_file(path) do
creator.call
end
end
end
end
Expand Down
5 changes: 3 additions & 2 deletions lib/chainer/datasets/mnist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def self.retrieve_mnist_test
def self.retrieve_mnist(name, urls)
root = Chainer::Dataset::Download.get_dataset_directory('pfnet/chainer/mnist')
path = File.expand_path(name, root)
creator = make_npz(path, urls)
Chainer::Dataset::Download.cache_or_load_file(path, creator)
Chainer::Dataset::Download.cache_or_load_file(path) do
make_npz(path, urls)
end
end

def self.make_npz(path, urls)
Expand Down

0 comments on commit cc7685f

Please sign in to comment.