5
$\begingroup$

I have a symmetric image img. For simplicity, I would use the below image as my example:

img = Rasterize@Graphics[{Red, Rectangle[]}]

enter image description here

As you can see, img is an image of a pure red square with four thin white margins. Now I put it into a neural network with a pooling layer only (and the PartLayer which is used to extract the red color), and then extract the gradient.

pool = NetChain[{PoolingLayer[10, "Input" -> "Image"], PartLayer[1]}]
grad = pool[img, NetPortGradient["Input"]];
gIm = grad // NetDecoder["Image"]

enter image description here

As you can see, the gradient image has a "black-L" at the margin.

The input image is symmetric, while the pooling layer (and the PartLayer) have no parameters to be trained. That means everything should be symmetric, but the gradient is asymmetric. Why?

Many thanks!

$\endgroup$

1 Answer 1

5
$\begingroup$

It is black because it zeros out the values in the dimensions it collapses, for example:

In[1]:= pool = PoolingLayer[{2}]
In[6]:= pool@{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}
Out[6]= {{1., 1.}, {1., 1.}, {1., 1.}}

In[7]:= pool[{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}, 
 NetPortGradient["Input"]]
Out[7]= {{1., 1., 0.}, {1., 1., 0.}, {1., 1., 0.}}

So the black strips correspond to the 9 dropped columns and rows from the 10x10 pooling.

However, it looks like there may be a bug in the documentation, which shows this example:

enter image description here

But the result is not what we expect when executed (v11.3):

enter image description here

Without the ImageAdjust it looks like this:

enter image description here

$\endgroup$
1
  • $\begingroup$ Can anyone confirm that is a bug? $\endgroup$
    – M.R.
    Commented Feb 6, 2019 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.