Inserting noise section

is there possibly an error for the noise insertion section of the style gan components assignment. I in one section of the grader all of the checks are greater than except one.

assert torch.abs((inject_noise(fake_images) - fake_images).std(0)).mean() > 1e-4
assert torch.abs((inject_noise(fake_images) - fake_images).std(1)).mean() < 1e-4
assert torch.abs((inject_noise(fake_images) - fake_images).std(2)).mean() > 1e-4
assert torch.abs((inject_noise(fake_images) - fake_images).std(3)).mean() > 1e-4

Check that the per-channel change is roughly normal

1 Like

It’s been a while since I looked at this assignment, but I think the point is that not all 4 of the dimensions are handled the same way there. Look at how dimension 1 is treated in noise injection: the size of that dimension is 1, but the other dimensions are all non-trivial. What effect will that have in the resulting behavior? :nerd_face:

1 Like

ok, thanks. At least know what direction to look in now. Thanks!

1 Like

do you think randn is the right function to use to set the weights?

torch.randn([1, channels, 1, 1])

1 Like

Yes, that’s the way I did it. Mind you, I worked this assignment > 2 years ago, so I don’t know if they’ve subsequently updated the assignment in any way.

1 Like

I just got the latest version of the notebook and there is only one minor change since 2 years ago in the template code that they provide. So I think that weight initialization should still be correct.

1 Like

ok, thanks again. There are only two coding blocks in this section so I have no clue what I could be missing. Using the image shape as the noise shape.

1 Like

What error are you getting?

Note that the noise shape is not the same as the image shape, right? That was what I was referring to earlier about why the test for dimension 1 is different. Oh, ok, that was part of the code you need to write, so that is probably where your bug is. The shape of the noise is the same as the shape of the image in every dimension except dimension 1. Dimension 1 has only 1 element for noise.

4 Likes

That’s what I think I’m trying, but still failing at least one of the tests;

noise_shape = (1, image.shape[1], image.shape[2], image.shape[3])

1 Like

But indexing in python is 0-based, so dimension 1 is the second dimension, right?

2 Likes

Or to put it another way, which dimension is the “channel” dimension? It is index 1, which is the second dimension.

2 Likes

oh wow, I tried every dimension being one but that one. I was sure that one had to be the channel number, but it makes sense now. Thank you !

2 Likes

I used the above function for the weights.
I also indexed the dimensions 1-3 of the input image, following 1 for the n_samples.
The preceding checks are fine.
I have also restarted the kernel.
However it fails at the first assert of the block:
assert torch.abs((inject_noise(fake_images) - fake_images).std(0)).mean() > 1e-4

Can anyone advise?

Many thanks,
Mark

1 Like

Have you actually read through this thread? I’m guessing your noise_shape looks like the erroneous one shown earlier

1 Like

Sorry you are absolutely right. Thanks. The channels should be set to 1 for the noise, so that each channel is calculated differently, as people have said. The multiplication of the noise vector and the weights should be of the same size as the image. This was in the instructions, which make it very clear that this is what we are doing.

2 Likes

Haha, it’s funny that everyone gets stuck on the same things! So useful to read older posts! Saves a lot of debugging time and sifting through print statements. :smile:

1 Like