OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Sat, 09 Feb 2019 20:56:22 -0600Does BackgroundSubtractorKNN respect a learning rate of 0 correctly?http://answers.opencv.org/question/207700/does-backgroundsubtractorknn-respect-a-learning-rate-of-0-correctly/I'm getting a strange result where it looks like **BackgroundSubtractorKNN** algorithm may still be learning when the learning rate parameter is set to zero in the apply function.
Looking at the source code for **apply()** I see that:
> learningRate = learningRate >= 0 &&
> nframes > 1 ? learningRate :
> 1./std::min( 2*nframes, history );
>
> Kshort=(int)(log(0.7)/log(1-learningRate))+1;
> Kmid=(int)(log(0.4)/log(1-learningRate))-Kshort+1;
> Klong=(int)(log(0.1)/log(1-learningRate))-Kshort-Kmid+1;
This strikes me odd as the log of 1 being zero, therefore division by zero occurs.
Can anyone confirm or correct me?
Wed, 23 Jan 2019 04:25:55 -0600http://answers.opencv.org/question/207700/does-backgroundsubtractorknn-respect-a-learning-rate-of-0-correctly/Answer by V for <p>I'm getting a strange result where it looks like <strong>BackgroundSubtractorKNN</strong> algorithm may still be learning when the learning rate parameter is set to zero in the apply function. </p>
<p>Looking at the source code for <strong>apply()</strong> I see that:</p>
<blockquote>
<p>learningRate = learningRate >= 0 &&
nframes > 1 ? learningRate :
1./std::min( 2*nframes, history );</p>
<p>Kshort=(int)(log(0.7)/log(1-learningRate))+1;</p>
<p>Kmid=(int)(log(0.4)/log(1-learningRate))-Kshort+1;</p>
<p>Klong=(int)(log(0.1)/log(1-learningRate))-Kshort-Kmid+1;</p>
</blockquote>
<p>This strikes me odd as the log of 1 being zero, therefore division by zero occurs.
Can anyone confirm or correct me?</p>
http://answers.opencv.org/question/207700/does-backgroundsubtractorknn-respect-a-learning-rate-of-0-correctly/?answer=208584#post-id-208584Yes, there is a bug and it does result in learning when the learning rate is set to zero. Float division by zero is undefined behavior, but after casting to integer it can go to -2^32 for example. The model update intervals may then be set between 0 and a negative number, which I think results in complete learning of the next frame because if you use apply() twice on the same image with learning rate zero it seems you will always get an empty mask.
If you don't want to patch it a temporary solution is to set the learning rate to a very small number.Sat, 09 Feb 2019 20:56:22 -0600http://answers.opencv.org/question/207700/does-backgroundsubtractorknn-respect-a-learning-rate-of-0-correctly/?answer=208584#post-id-208584