# Visualizing convolution and deconvolution layers in mathematica

I found this series of visualizations of NN layers working principle fascinating (link):

Can you provide some tips how to build such animation in Mathematica?

• Just say 'show me the code', the likelihood of getting an answer will be the same and you will save us the time spend on thinking about efficiency. :) – Kuba Jun 14 '17 at 19:13
• @Kuba, I've noticed that 'show me the code' releases more time for thinking about closing and downvoting )) – iot Jun 14 '17 at 19:36

Okay lets do this:

first we'll build a ground structure. Some Variables to define our Rectangle sizes. Next a variable to store our frames and a Table which constructs them. At last a ListAnimate.

topTileLength = 2;
bottomTileLength = 4;
frames = Flatten[
Table[
Graphics3D[{Point[{x, y, 0}]},
PlotRange -> {{0, bottomTileLength}, {0, bottomTileLength}, {0,
2}}], (*example*)
{x, 1, topTileLength}, {y, 1, topTileLength}]
, 1];
ListAnimate[frames]


Okay so far so good. Now lets replace the example by real rectangles. We doing this by defining our own function

Rectangle3D[{xmin_,xmax_},{ymin_,ymax_},z_]:=Polygon[{{xmin,ymin,z},{xmax,ymin,z},{xmax,ymax,z},{xmin,ymax,z}}]

The list for our Graphics3D has to be flattened and adding a table to construct our Rectangles.

Graphics3D[Flatten@{
Table[Rectangle3D[{dx - 1, dx}, {dy - 1, dy}, 1.9], {dx, 1,
topTileLength}, {dy, 1, topTileLength}]
}, PlotRange -> {{0, bottomTileLength}, {0, bottomTileLength}, {0,
2}}]


Now lets add color for the specified condition, that we have an active field:

... Table[{If[dx==x&&dy==y,Blue,Red],Rectangle3D[...

We getting on our way. Now lets add the bottom layer and shift the top layer to the middle.

... midPointLength = (bottomTileLength - topTileLength)/2; ... Table[{If[dx == x && dy == y, Blue, Gray], Rectangle3D[{dx - 1 + midPointLength, dx + midPointLength}, {dy + midPointLength - 1, dy + midPointLength}, 1.9]}, {dx, 1, topTileLength}, {dy, 1, topTileLength}], Table[{If[False, Red, Gray], Rectangle3D[{dx - 1, dx}, {dy - 1, dy}, 0.1]}, {dx, 1, bottomTileLength}, {dy, 1, bottomTileLength}] ... 

The condition for the bottom layer is not that hard by also introducing a radius parameter for the convolution:

... If[ x + midPointLength - radius <= dx <= x + midPointLength + radius && y + midPointLength - radius <= dy <= y + midPointLength + radius, Red, Gray] ...

Yeay, results.

Now finally add some Lines to the plot and afterwards make the colors a little bit more beautiful and add a little opacity as the final note:

Full Code:

Rectangle3D[{xmin_, xmax_}, {ymin_, ymax_}, z_] :=
Polygon[{{xmin, ymin, z}, {xmax, ymin, z}, {xmax, ymax, z}, {xmin,
ymax, z}}]
topTileLength = 5;
bottomTileLength = 9;
midPointLength = Floor[(bottomTileLength - topTileLength)/2];
frames = Flatten[
Table[
Graphics3D[Flatten@{Opacity[0.8],
Table[{If[dx == x && dy == y, Darker[Cyan], Lighter[Cyan]],
Rectangle3D[{dx - 1 + midPointLength,
dx + midPointLength}, {dy + midPointLength - 1,
dy + midPointLength}, 1.9]}, {dx, 1, topTileLength}, {dy,
1, topTileLength}],
Table[{If[
x + midPointLength - radius <= dx <=
x + midPointLength + radius &&
y + midPointLength - radius <= dy <=
y + midPointLength + radius, Darker[Blue], Lighter[Blue]],
Rectangle3D[{dx - 1, dx}, {dy - 1, dy}, 0.1]}, {dx, 1,
bottomTileLength}, {dy, 1, bottomTileLength}],
Black, Thick,
Line[{{x - 1 + midPointLength, y - 1 + midPointLength,
1.9}, {x - 1 + midPointLength - radius,
y - 1 + midPointLength - radius, 0.1}}],
Line[{{x + midPointLength, y - 1 + midPointLength,
1.9}, {x + midPointLength + radius,
y - 1 + midPointLength - radius, 0.1}}],
Line[{{x + midPointLength, y + midPointLength,
1.9}, {x + midPointLength + radius,
y + midPointLength + radius, 0.1}}],
Line[{{x - 1 + midPointLength, y + midPointLength,
1.9}, {x - 1 + midPointLength - radius,
y + midPointLength + radius, 0.1}}]
},
PlotRange -> {{0, bottomTileLength}, {0, bottomTileLength}, {0,
2}}],
{x, 1, topTileLength}, {y, 1, topTileLength}]
, 1];
ListAnimate[frames]


Hope this helps.

• yes, it helps a lot! It will take me some time to get through the code. – iot Jun 14 '17 at 19:46
• @Julien Kluge very cool. Yes, that's a good answer. – LCarvalho Oct 26 '17 at 20:33