Skip to content
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

Fix and improve multiple places about random number generation and shuffling #848

Merged
merged 1 commit into from
Aug 4, 2014
Merged

Conversation

netheril96
Copy link
Contributor

  • cluster_seedgen() now uses system entropy source /dev/urandom whenever possible.
  • Fix Caffe::RNG::operator=(). The original version will result in two shared_ptr pointing to the same object with different reference counts and ultimately use-after-free or double-free problem.
  • O(N) instead of O(N^2) algorithm for ImageDataLayer::ShuffleImages().
  • convert_imageset will now properly seed the random number generator before shuffling, no longer producing the same shuffled result each run.

@netheril96 netheril96 mentioned this pull request Aug 4, 2014
@bhack
Copy link
Contributor

bhack commented Aug 4, 2014

There is some activity for add Caffe support on windows. See #15.
Random_device is cross platfrom but it is available only in c++11
Edit:
see also this

return static_cast<caffe::rng_t*>(Caffe::rng_stream().generator());
}

// Fisher–Yates algorithm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Fisher–Yates algorithm covered by std::random_shuffle?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bhack:
std::random_shuffle internally uses rand() as the random number generator, whose randomness quality is horrible. Besides, the original call to std::random_shuffle is not preceded by srand(), resulting in the same order every time.

C++11 actually has a std::shuffle, but sadly we cannot use it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::random_shuffle doesn't receive the random number generator as last (optional) parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bhack

See http://stackoverflow.com/questions/19219726/what-is-the-difference-between-shuffle-and-random-shuffle-c

You cannot pass in caffe::rng_t a.k.a boost::mt19937 as the third parameter to std::random_shuffle. Instead, you need to write a functor class, instantiate it, and then pass to std::random_shuffle, which involves probably more code than reimplementing Fisher–Yates algorithm.

@wendlerc
Copy link

wendlerc commented Aug 4, 2014

So is there no more shuffling going on during training?

@netheril96
Copy link
Contributor Author

@bhack
To achieve cross platform compatibility without c++11, one can use boost::random_device. But it involves new dependency so I'm not sure if it is a good idea.

The original code cannot compile under Windows either, since getpid() is a POSIX function.

@netheril96
Copy link
Contributor Author

@Mezn

I'm not sure what you mean. This changes only the implementation of ShuffleImages(), not how many times it is called.

@wendlerc
Copy link

wendlerc commented Aug 4, 2014

@netheril96 It was a general question, does caffe shuffle the dataset during training after each epoch or not? Maybe I posted my question onto the wrong issue sorry.

@netheril96
Copy link
Contributor Author

@Mezn I don't know the answer to your question. I didn't exactly look at other parts of ImageDataLayer.

@shelhamer
Copy link
Member

@Mezn off-topic for this thread, but no: Caffe does not shuffle between
epochs for you. You can do so manually, but it doesn't seem to make
much difference for ImageNet scale data. I believe Yangqing commented on
this in the past if you do an issue search for shuffling.

On Monday, August 4, 2014, netheril96 notifications@github.com wrote:

@Mezn https://github.com/mezN I don't know the answer to your question.
I didn't exactly look at other parts of ImageDataLayer.


Reply to this email directly or view it on GitHub
#848 (comment).

Evan Shelhamer

@jeffdonahue
Copy link
Contributor

@Mezn and @shelhamer, the ImageDataLayer does do a shuffle after every epoch if the shuffle option is enabled (https://github.com/BVLC/caffe/blob/master/src/caffe/layers/image_data_layer.cpp#L126) -- this PR doesn't affect that.

@netheril96 thanks for fixing my hacky shuffle and everything else.

To me this looks good to merge other than possibly the /dev/urandom issue -- I think it's ok since we have these cross-platform compatibility issues throughout the code currently, and the fallback to the old cluster_seedgen seems good enough to me.

@wendlerc
Copy link

wendlerc commented Aug 4, 2014

thanks!

@shelhamer
Copy link
Member

Re: shuffle I misspoke. ImageDataLayer does reshuffle across epochs while
the DataLayer for leveldb/ml db does not.

For merge this looks good to me. Agreed with @jeffdonahue about platform
compatibility.

Thanks for reviewing since you last worked on RNG Jeff!

On Monday, August 4, 2014, Jeff Donahue notifications@github.com wrote:

@mexn and @shelhamer https://github.com/shelhamer, the ImageDataLayer
does do a shuffle after every epoch if the shuffle option is enabled (
https://github.com/BVLC/caffe/blob/master/src/caffe/layers/image_data_layer.cpp#L126)
-- this PR doesn't affect that.

@netheril96 https://github.com/netheril96 thanks for fixing my hacky
shuffle and everything else.

To me this looks good to merge other than possibly the /dev/urandom issue
-- I think it's ok since we have these cross-platform compatibility issues
throughout the code currently, and the fallback to the old cluster_seedgen
seems good enough to me.


Reply to this email directly or view it on GitHub
#848 (comment).

jeffdonahue added a commit that referenced this pull request Aug 4, 2014
Fix and improve multiple places about random number generation and shuffling
@jeffdonahue jeffdonahue merged commit 7bf2268 into BVLC:dev Aug 4, 2014
mitmul pushed a commit to mitmul/caffe that referenced this pull request Sep 30, 2014
Fix and improve multiple places about random number generation and shuffling
RazvanRanca pushed a commit to RazvanRanca/caffe that referenced this pull request Nov 4, 2014
Fix and improve multiple places about random number generation and shuffling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants