Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
randn repeating itself

Subject: randn repeating itself

From: Nicholas

Date: 28 Mar, 2013 05:17:11

Message: 1 of 12

Dear all,

I'm adding noise (Rician) with randn to a synthetic image for post processing. However, for validation reasons, I want to process the image with numerous different instances of the noise model applied to the image. So, what I did was create a for loop such that at each iteration a new/different instance of the noise was added to original image. The problem is that the noise is the exact same with each iteration. BUT the perplexing thing is that if I use a breakpoint inside the loop and 'continue' through it, the noise changes with each iteration (unlike if I were to just let it run). At any rate, the code looks something like this:

im = imread('lena.tif');
for i = 1:20
% add 10% noise
x = 0.1*randn(size(im)) + im;
y = 0.1*randn(size(im));
noisy = sqrt(x.^2 + y.^2);
% image processing %
end

So, in this example, noisy is returned as the exact same image 20 times. This seems like a seeding problem to me, but as far as I can tell randn is used properly within this context. Though the way its functioning seems that the seed is set back to 'default' with each iteration.

I also tried using "rng shuffle", but matlab throws an error because the random number is initialized with the legacy generator. Any advice?

Subject: randn repeating itself

From: anyone

Date: 28 Mar, 2013 18:46:04

Message: 2 of 12

On Thu, 28 Mar 2013 05:17:11 +0000, Nicholas wrote:
 
> I'm adding noise (Rician) with randn to a synthetic image for post
> processing. However, for validation reasons, I want to process the image
> with numerous different instances of the noise model applied to the
> image. So, what I did was create a for loop such that at each iteration
> a new/different instance of the noise was added to original image. The
> problem is that the noise is the exact same with each iteration. BUT
> the perplexing thing is that if I use a breakpoint inside the loop and
> 'continue' through it, the noise changes with each iteration (unlike if
> I were to just let it run). At any rate, the code looks something like
> this:
>
> im = imread('lena.tif');
> for i = 1:20
> % add 10% noise
> x = 0.1*randn(size(im)) + im;
> y = 0.1*randn(size(im));
> noisy = sqrt(x.^2 + y.^2);
> % image processing %
> end
>
> So, in this example, noisy is returned as the exact same image 20 times.

I doubt that... 'imread' returns uint8 class, while 'randn' returns 'double', and Matlab won't directly add the two. On top
of that, and assuming you swat the small bug, you don't save anything on each pass so only the last version of
'noisy' gets dumped to the workspace and that one has only one version of "noise".

> This seems like a seeding problem to me,

No, it isn't.
[...]

Subject: randn repeating itself

From: Peter Perkins

Date: 29 Mar, 2013 02:44:33

Message: 3 of 12

On 2013/03/28 1:17, Nicholas wrote:
> This seems like a seeding problem to me, but as far as I can tell randn
> is used properly within this context. Though the way its functioning
> seems that the seed is set back to 'default' with each iteration.
> I also tried using "rng shuffle", but matlab throws an error because the
> random number is initialized with the legacy generator. Any advice?

That error from the rng function indicates that something in the code
that you have not shown us is doing something along the lines of

    rand('state',0)

or

    randn('seed',0)

or something similar. This is bad on several levels, the most obvious of
which is that it's causing your image noise to be entirely predictable.
You should find the line of code and remove it.

> The problem is that the noise is the exact same with each iteration.
 > BUT the perplexing thing is that if I use a breakpoint inside the loop
 > and 'continue' through it, the noise changes with each iteration
 > (unlike if I were to just let it run).

That seems hard to imagine, but perhaps you could add the line

    rng % no semicolon

at the very beginning of your loop to try to diagnose what's going
wrong. Before you run the loop, type "rng default". Then see when the
legacy generator is being activated.

Subject: randn repeating itself

From: Nicholas

Date: 29 Mar, 2013 19:09:22

Message: 4 of 12

Peter Perkins <Peter.Remove.Perkins.This@mathworks.com> wrote in message <kj2v6h$om4$1@newscl01ah.mathworks.com>...
> On 2013/03/28 1:17, Nicholas wrote:
> > This seems like a seeding problem to me, but as far as I can tell randn
> > is used properly within this context. Though the way its functioning
> > seems that the seed is set back to 'default' with each iteration.
> > I also tried using "rng shuffle", but matlab throws an error because the
> > random number is initialized with the legacy generator. Any advice?
>
> That error from the rng function indicates that something in the code
> that you have not shown us is doing something along the lines of
>
> rand('state',0)
>
> or
>
> randn('seed',0)
>
> or something similar. This is bad on several levels, the most obvious of
> which is that it's causing your image noise to be entirely predictable.
> You should find the line of code and remove it.
>
> > The problem is that the noise is the exact same with each iteration.
> > BUT the perplexing thing is that if I use a breakpoint inside the loop
> > and 'continue' through it, the noise changes with each iteration
> > (unlike if I were to just let it run).
>
> That seems hard to imagine, but perhaps you could add the line
>
> rng % no semicolon
>
> at the very beginning of your loop to try to diagnose what's going
> wrong. Before you run the loop, type "rng default". Then see when the
> legacy generator is being activated.
>

Thank you for the reply. I should start with two things. First, I make no calls to rng in the function (I tried 'shuffle', but took it out) and randn is used exactly how I show in my example code above. Second, the data type correction noted by the previous poster. The first line should read:

im = im2double(imread('lena.tif'));

At any rate, I developed a workaround (a sloppy one at that) where I create a bin of all the noise models that have been previously used, I then compare the current model against all the others to ensure that the current is unique to the bin list (if it's unique it is then added to the bin), if it's not I keep generating new models until I get a new one that is unique.

Consequently, what I found was that the number of attempts it takes to find a unique image corresponded to the i-th iteration in the for loop. So, say we're at i = 20 in the loop, then the first 19 attempts at creating a new image fails because they were already created and added to the bin at i = 1:19. This seems to indicate that rng is somehow being set to 'default' at the beginning of each pass of the loop. However, as I said above, I make no calls to rng, and randn is utilized exactly as shown above.

Also, I don't know if it matters, but this block of code is nested within another for loop. It seems odd, but would nesting change the functionality of rng/randn?

Subject: randn repeating itself

From: Peter Perkins

Date: 29 Mar, 2013 20:19:54

Message: 5 of 12

You said

>> I also tried using "rng shuffle", but matlab throws an error because the
>> random number is initialized with the legacy generator. Any advice?

I think you are referring to this error message:

    >> rng('shuffle')
    Error using rng (line 109)
    The current random number generator is the legacy generator.
    This is because you have executed a command such as
    rand('state',0), which activates MATLAB's legacy random number
    behavior. You may not use RNG to reseed the legacy
    random number generator.

    Use rng('default') to reinitialize the random number generator
    to its startup configuration, or call RNG using a specific
    generator type, such as rng(seed,'twister').

If that's really true, then there is a line in code that you are running
that is executing something like rand('state',0). You may not have
written that line, but it's in there somewhere. That's what you need to
find. To the best of my knowledge, there is no such line in any MATLAB
toolbox code.


On 3/29/2013 3:09 PM, Nicholas wrote:
> Peter Perkins <Peter.Remove.Perkins.This@mathworks.com> wrote in message
> <kj2v6h$om4$1@newscl01ah.mathworks.com>...
>> On 2013/03/28 1:17, Nicholas wrote:
>> > This seems like a seeding problem to me, but as far as I can tell randn
>> > is used properly within this context. Though the way its functioning
>> > seems that the seed is set back to 'default' with each iteration.
>> > I also tried using "rng shuffle", but matlab throws an error because
>> the
>> > random number is initialized with the legacy generator. Any advice?
>>
>> That error from the rng function indicates that something in the code
>> that you have not shown us is doing something along the lines of
>>
>> rand('state',0)
>>
>> or
>>
>> randn('seed',0)
>>
>> or something similar. This is bad on several levels, the most obvious
>> of which is that it's causing your image noise to be entirely
>> predictable. You should find the line of code and remove it.
>>
>> > The problem is that the noise is the exact same with each iteration.
>> > BUT the perplexing thing is that if I use a breakpoint inside the loop
>> > and 'continue' through it, the noise changes with each iteration
>> > (unlike if I were to just let it run).
>>
>> That seems hard to imagine, but perhaps you could add the line
>>
>> rng % no semicolon
>>
>> at the very beginning of your loop to try to diagnose what's going
>> wrong. Before you run the loop, type "rng default". Then see when the
>> legacy generator is being activated.
>>
>
> Thank you for the reply. I should start with two things. First, I make
> no calls to rng in the function (I tried 'shuffle', but took it out) and
> randn is used exactly how I show in my example code above. Second, the
> data type correction noted by the previous poster. The first line should
> read:
>
> im = im2double(imread('lena.tif'));
>
> At any rate, I developed a workaround (a sloppy one at that) where I
> create a bin of all the noise models that have been previously used, I
> then compare the current model against all the others to ensure that the
> current is unique to the bin list (if it's unique it is then added to
> the bin), if it's not I keep generating new models until I get a new one
> that is unique.
>
> Consequently, what I found was that the number of attempts it takes to
> find a unique image corresponded to the i-th iteration in the for loop.
> So, say we're at i = 20 in the loop, then the first 19 attempts at
> creating a new image fails because they were already created and added
> to the bin at i = 1:19. This seems to indicate that rng is somehow being
> set to 'default' at the beginning of each pass of the loop. However, as
> I said above, I make no calls to rng, and randn is utilized exactly as
> shown above.
>
> Also, I don't know if it matters, but this block of code is nested
> within another for loop. It seems odd, but would nesting change the
> functionality of rng/randn?

Subject: randn repeating itself

From: anyone

Date: 29 Mar, 2013 21:11:05

Message: 6 of 12

On Fri, 29 Mar 2013 19:09:22 +0000, Nicholas wrote:

[snipped]

> [...] The first line should read:
>
> im = im2double(imread('lena.tif'));
>
[...]

% First, pull-in a simple image file

im = im2double(imread('hubble.tif')); % array size - 960x1280x3

% run the script

for i = 1:20
x = 0.1*randn(size(im)) + im;
y = 0.1*randn(size(im));
noisy = sqrt(x.^2 + y.^2);
end

% run it again, save final pass in different name

...
noisy1 = sqrt(x.^2 + y.^2);
...

% lastly, post-process data in command window

>> diff = noisy - noisy1;
>> comp = find(abs(diff) < 1e-4);

Out of the original 960x1280x3 array, 'comp' returns only 2839 indices where the difference is less than 1e-4. Seems
to me, randn() is working as expected.

> Also, I don't know if it matters, but this block of code is nested
> within another for loop. It seems odd, but would nesting change the
> functionality of rng/randn?

No. Else, TMW have a major bug in their code.

Subject: randn repeating itself

From: Bruno Luong

Date: 29 Mar, 2013 21:36:11

Message: 7 of 12

"Nicholas" wrote in message <kj0jon$9ec$1@newscl01ah.mathworks.com>...

> im = imread('lena.tif');
> for i = 1:20
> % add 10% noise
> x = 0.1*randn(size(im)) + im;
> y = 0.1*randn(size(im));
> noisy = sqrt(x.^2 + y.^2);
> % image processing %
> end

Wasn't the variable im is of class uint8?

When you add mixed types, e.g., uint8 and double, matlab cast to lower type, i.e. uint8.

In order to get the resultant x different than im, you must have the noise | randn(x) | > 0.5.

The probability for that event to happens is:

P = erfc(0.5/0.1) = 1.5375e-12.

So yeah, you might run the loop 20 times, and chance that you still get the same image. Perfectly conform to the expectation.

Bruno

Subject: randn repeating itself

From: Nicholas

Date: 30 Mar, 2013 16:04:12

Message: 8 of 12

Peter Perkins <Peter.Remove.Perkins.This@mathworks.com> wrote in message <kj4t1a$rpn$1@newscl01ah.mathworks.com>...
> You said
>
> >> I also tried using "rng shuffle", but matlab throws an error because the
> >> random number is initialized with the legacy generator. Any advice?
>
> I think you are referring to this error message:
>
> >> rng('shuffle')
> Error using rng (line 109)
> The current random number generator is the legacy generator.
> This is because you have executed a command such as
> rand('state',0), which activates MATLAB's legacy random number
> behavior. You may not use RNG to reseed the legacy
> random number generator.
>
> Use rng('default') to reinitialize the random number generator
> to its startup configuration, or call RNG using a specific
> generator type, such as rng(seed,'twister').
>
> If that's really true, then there is a line in code that you are running
> that is executing something like rand('state',0). You may not have
> written that line, but it's in there somewhere. That's what you need to
> find. To the best of my knowledge, there is no such line in any MATLAB
> toolbox code.

Yes, that's the error I received! I probably should have posted that initially.

I'd have to agree with your assessment that something like rand('state',0) is being executed somewhere somehow. My guess is it's probably buried deep within one of the numerous functions I'm using - likely in all the image processing junk.

Does anyone know if it's possible to search through all of the called functions to see if and where a rand('state',0)-like command has been executed? I tried using Profiler, but rand didn't turn up in the function list of the Profile summary.

Subject: randn repeating itself

From: dpb

Date: 30 Mar, 2013 17:58:59

Message: 9 of 12

On 3/30/2013 11:04 AM, Nicholas wrote:
...

> I'd have to agree with your assessment that something like
> rand('state',0) is being executed somewhere somehow. My guess is it's
> probably buried deep within one of the numerous functions I'm using -
> likely in all the image processing junk.
> Does anyone know if it's possible to search through all of the called
> functions to see if and where a rand('state',0)-like command has been
> executed? I tried using Profiler, but rand didn't turn up in the
> function list of the Profile summary.

As Peter says (and I would think would be so), the culprit is not a ML
function w/ very high likelihood.

A) Isn't there a callback tree when the error is thrown?

B) Have you tried dbstop on error for the specific error message?

If all else fails, presuming you have at least some organization in the
project files, when looking at new code or needing somesuch a thing as
you're talking about and the regular tools aren't working, I have been
known to use grep to find the location of stuff in code as well as data
files...

grep -iS rand('state' *.m | more

It'll find you anywhere the above code snippet exists if it does in any
m file in the target directory and below (-S) and altho not needed I use
case-insensitive as a habit (-i)

Don't search for the '0', specifically, it's possible a variable was
used and that would miss it at the cost of perhaps a few hits that
aren't pertinent. Then again, w/ something going on as you have, it's
probably worth finding out where the state is being mucked with
irregardless.

--

Subject: randn repeating itself

From: Yair Altman

Date: 30 Mar, 2013 20:50:20

Message: 10 of 12

"Nicholas" wrote in message <kj72ds$1dh$1@newscl01ah.mathworks.com>...
[snip]
> Does anyone know if it's possible to search through all of the called functions to see if and where a rand('state',0)-like command has been executed? I tried using Profiler, but rand didn't turn up in the function list of the Profile summary.


Matlab's Profiler doesn't normally show built-in functions like rand(), unless you explicitly ask for it:

profile -detail builtin on
...
profile off; profile report

More: http://UndocumentedMatlab.com/blog/undocumented-profiler-options-part-4/

Yair Altman
http://UndocumentedMatlab.com
 

Subject: randn repeating itself

From: james bejon

Date: 30 Mar, 2013 22:08:06

Message: 11 of 12

"Yair Altman" wrote in message <kj7j6c$f0i$1@newscl01ah.mathworks.com>...
> "Nicholas" wrote in message <kj72ds$1dh$1@newscl01ah.mathworks.com>...
> [snip]
> > Does anyone know if it's possible to search through all of the called functions to see if and where a rand('state',0)-like command has been executed? I tried using Profiler, but rand didn't turn up in the function list of the Profile summary.
>
>
> Matlab's Profiler doesn't normally show built-in functions like rand(), unless you explicitly ask for it:
>
> profile -detail builtin on
> ...
> profile off; profile report
>
> More: http://UndocumentedMatlab.com/blog/undocumented-profiler-options-part-4/
>
> Yair Altman
> http://UndocumentedMatlab.com
>

That's really useful Yair. Thanks!

Subject: randn repeating itself

From: Nicholas

Date: 31 Mar, 2013 22:14:09

Message: 12 of 12

"james bejon" wrote in message <kj7no5$qfd$1@newscl01ah.mathworks.com>...
> "Yair Altman" wrote in message <kj7j6c$f0i$1@newscl01ah.mathworks.com>...
> > "Nicholas" wrote in message <kj72ds$1dh$1@newscl01ah.mathworks.com>...
> > [snip]
> > > Does anyone know if it's possible to search through all of the called functions to see if and where a rand('state',0)-like command has been executed? I tried using Profiler, but rand didn't turn up in the function list of the Profile summary.
> >
> >
> > Matlab's Profiler doesn't normally show built-in functions like rand(), unless you explicitly ask for it:
> >
> > profile -detail builtin on
> > ...
> > profile off; profile report
> >
> > More: http://UndocumentedMatlab.com/blog/undocumented-profiler-options-part-4/
> >
> > Yair Altman
> > http://UndocumentedMatlab.com
> >
>
> That's really useful Yair. Thanks!

That found the culprit! The command randn('state',0) was indeed being used several functions deep during an initialization process. Thanks to all the responders (especially Peter and Yair)!

*Note: I didn't try the grep utility, since the Profiler found it, but it seems like it would be a viable option to find code snippets and probably would have been successful in my case. Thanks again!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us