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

Improve numerical stability of variance computation in MVNLayer #3162

Merged
merged 1 commit into from
Oct 11, 2015

Conversation

kkhoot
Copy link
Contributor

@kkhoot kkhoot commented Oct 7, 2015

This PR is to avoid numerical precision error in MVNLayer when the input image has large intensity values and relatively small variance, which possibly causes negative values in calculating var(X) = E(X^2) - (EX)^2.

I found this error when dealing with some real data having almost white color intensities resulting in NaN loss or loss divergence.

Solution to this problem is to compute the mean intensity and subtract it first, and compute variance using var(X) = E((X-EX)^2) - E(X-EX)^2, which simply is E((X-EX)^2).

Updated: removed redundant code (subtracting mean) in the forward functions.

temp_.mutable_cpu_data());
// subtract mean
caffe_cpu_gemv<Dtype>(CblasNoTrans, num, dim, 1. / dim, bottom_data,
sum_multiplier_.cpu_data(), 0., mean_.mutable_cpu_data()); // EX
Copy link
Member

Choose a reason for hiding this comment

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

Please indent 4 spaces in continuing line, to be consistent with our code style.

@ronghanghu
Copy link
Member

@kkhoot Thanks for this PR!

Please fix the style issue commented above. Everything else looks good to me :)

@kkhoot
Copy link
Contributor Author

kkhoot commented Oct 11, 2015

@ronghanghu Thanks for reviewing this PR. :) The style issue has been fixed.
However many lines in the rest of MVNLayer code are having irregular indents (from 2 to 8 spaces). If you think it is better to fix all of them, I will fix them and push it again.

@ronghanghu
Copy link
Member

Let's just leave the rest code as-is, to provide a more readable diff in this PR :)

Thanks @kkhoot !

ronghanghu added a commit that referenced this pull request Oct 11, 2015
Improve numerical stability of variance computation in MVNLayer
@ronghanghu ronghanghu merged commit 0151742 into BVLC:master Oct 11, 2015
@kkhoot kkhoot deleted the fix_mvn branch October 22, 2015 15:25
@kkhoot kkhoot mentioned this pull request Nov 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants