import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
from matplotlib import patches
Convolution Operation Stride
Interactive tutorial on convolution operation stride with practical implementations and visualizations
= np.random.choice(range(10), (5, 5))
inp = np.array([
filter_conv 1, 0, -1],
[1, 0, -1],
[1, 0, -1]
[ ])
='Greys') plt.imshow(inp, cmap
= plt.subplots(ncols=3, figsize=(12, 4))
fig, ax
=True, cbar=None, ax=ax[0], cmap='Purples')
sns.heatmap(inp, annot=True, cbar=None, ax=ax[1], cmap='Purples')
sns.heatmap(filter_conv, annot= ax[0]
g = patches.Rectangle((0,0),3,3,linewidth=5,edgecolor='grey',facecolor='black', alpha=0.5)
rect
# Add the patch to the Axes
g.add_patch(rect)
0].set_title("Input")
ax[1].set_title("Filter") ax[
Text(0.5, 1.0, 'Filter')
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.animation
#####################
# Array preparation
#####################
#input array
= 6
n = 0
p = 2
s = 3
f = np.random.randint(0, 5, size=(n, n))
a # kernel
= np.tile([1, 0, -1], f).reshape(f, f)
kernel #f = kernel.shape[0]
def create_animation(a, kernel, p, s, fname, frate, figsize=(8, 4)):
if p:
# visualization array (2 bigger in each direction)
= np.zeros((a.shape[0]+2*p, a.shape[1]+2*p), dtype=int)
va -p,p:-p] = a
va[p:= np.zeros((a.shape[0]+2*p, a.shape[1]+2*p))
va_color -p,p:-p] = 0.5
va_color[p:else:
= a
va = np.zeros_like(a)
va_color = a.shape[0]
n = np.floor_divide(n+2*p-f, s)+1
o_shape #output array
= np.zeros((o_shape, o_shape))
res
#####################
# Create inital plot
#####################
= plt.figure(figsize=figsize)
fig
def add_axes_inches(fig, rect):
= fig.get_size_inches()
w,h return fig.add_axes([rect[0]/w, rect[1]/h, rect[2]/w, rect[3]/h])
= 3.
axwidth = axwidth/va.shape[1]
cellsize = cellsize*va.shape[0]
axheight
= add_axes_inches(fig, [cellsize, cellsize, axwidth, axheight])
ax_va = add_axes_inches(fig, [cellsize*2+axwidth,
ax_kernel 2+res.shape[0])*cellsize-kernel.shape[0]*cellsize,
(1]*cellsize,
kernel.shape[0]*cellsize])
kernel.shape[= add_axes_inches(fig, [cellsize*3+axwidth+kernel.shape[1]*cellsize,
ax_res 2*cellsize,
1]*cellsize,
res.shape[0]*cellsize])
res.shape["Kernel", size=12)
ax_kernel.set_title(
= ax_va.imshow(va_color, vmin=0., vmax=1.3, cmap="Blues")
im_va "Image size: {}X{}\n Padding: {} and Strides: {}".format(n, n, p, s))
ax_va.set_title(for i in range(va.shape[0]):
for j in range(va.shape[1]):
="center", ha="center")
ax_va.text(j,i, va[i,j], va
=-1, vmax=1, cmap="Pastel1")
ax_kernel.imshow(np.zeros_like(kernel), vminfor i in range(kernel.shape[0]):
for j in range(kernel.shape[1]):
="center", ha="center")
ax_kernel.text(j,i, kernel[i,j], va
= ax_res.imshow(res, vmin=0, vmax=1.3, cmap="Greens")
im_res = []
res_texts for i in range(res.shape[0]):
= []
row for j in range(res.shape[1]):
"", va="center", ha="center"))
row.append(ax_res.text(j,i,
res_texts.append(row)
"Output size: {}X{}".format(n+2*p-f+1, n+2*p-f+1))
ax_res.set_title(
for ax in [ax_va, ax_kernel, ax_res]:
=False, bottom=False, labelleft=False, labelbottom=False)
ax.tick_params(left1,0))
ax.yaxis.set_major_locator(mticker.IndexLocator(1,0))
ax.xaxis.set_major_locator(mticker.IndexLocator(="k")
ax.grid(color
###############
# Animation
###############
def init():
for row in res_texts:
for text in row:
"")
text.set_text(
def animate(ij):
=ij
i,j= kernel.shape[1]//2
o # calculate result
= (kernel*va[1+s*i-o:1+s*i+o+1, 1+s*j-o:1+s*j+o+1]).sum()
res_ij
res_texts[i][j].set_text(res_ij)# make colors
= va_color.copy()
c 1+s*i-o:1+s*i+o+1, 1+s*j-o:1+s*j+o+1] = 1.
c[
im_va.set_array(c)
= res.copy()
r = 1
r[i,j]
im_res.set_array(r)
= np.indices(res.shape)
i,j = matplotlib.animation.FuncAnimation(fig, animate, init_func=init,
ani =zip(i.flat, j.flat), interval=frate)
frames="imagemagick") ani.save(fname, writer
'demo.gif', 400) create_animation(a, kernel, p, s,
from keras.datasets import mnist
= 28, 28
img_rows, img_cols
# the data, split between train and test sets
= mnist.load_data() (x_train, y_train), (x_test, y_test)
0], kernel, 0, 1, 'mnist.gif', 2, (20, 4)) create_animation(x_train[
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.animation
#####################
# Array preparation
#####################
#input array
= 6
n = 0
p = 2
s = 3
f = np.random.randint(0, 5, size=(n, n))
a # kernel
= np.tile([1, 0, -1], f).reshape(f, f)
kernel #f = kernel.shape[0]
def create_static(a, kernel, p, s, fname, frate, figsize=(8, 4)):
if p:
# visualization array (2 bigger in each direction)
= np.zeros((a.shape[0]+2*p, a.shape[1]+2*p), dtype=int)
va -p,p:-p] = a
va[p:= np.zeros((a.shape[0]+2*p, a.shape[1]+2*p))
va_color -p,p:-p] = 0.5
va_color[p:else:
= a
va = np.zeros_like(a)
va_color = a.shape[0]
n = np.floor_divide(n+2*p-f, s)+1
o_shape #output array
= np.zeros((o_shape, o_shape))
res
#####################
# Create inital plot
#####################
= plt.figure(figsize=figsize)
fig
def add_axes_inches(fig, rect):
= fig.get_size_inches()
w,h return fig.add_axes([rect[0]/w, rect[1]/h, rect[2]/w, rect[3]/h])
= 3.
axwidth = axwidth/va.shape[1]
cellsize = cellsize*va.shape[0]
axheight
= add_axes_inches(fig, [cellsize, cellsize, axwidth, axheight])
ax_va = add_axes_inches(fig, [cellsize*2+axwidth,
ax_kernel 2+res.shape[0])*cellsize-kernel.shape[0]*cellsize,
(1]*cellsize,
kernel.shape[0]*cellsize])
kernel.shape[= add_axes_inches(fig, [cellsize*3+axwidth+kernel.shape[1]*cellsize,
ax_res 2*cellsize,
1]*cellsize,
res.shape[0]*cellsize])
res.shape["Kernel", size=12)
ax_kernel.set_title(
= ax_va.imshow(va_color, vmin=0., vmax=1.3, cmap="Blues")
im_va "Image size: {}X{}\n Padding: {} and Strides: {}".format(n, n, p, s))
ax_va.set_title(for i in range(va.shape[0]):
for j in range(va.shape[1]):
="center", ha="center")
ax_va.text(j,i, va[i,j], va
=-1, vmax=1, cmap="Pastel1")
ax_kernel.imshow(np.zeros_like(kernel), vminfor i in range(kernel.shape[0]):
for j in range(kernel.shape[1]):
="center", ha="center")
ax_kernel.text(j,i, kernel[i,j], va
= ax_res.imshow(res, vmin=0, vmax=1.3, cmap="Greens")
im_res = []
res_texts for i in range(res.shape[0]):
= []
row for j in range(res.shape[1]):
"", va="center", ha="center"))
row.append(ax_res.text(j,i,
res_texts.append(row)
"Output size: {}X{}".format(n+2*p-f+1, n+2*p-f+1))
ax_res.set_title(
for ax in [ax_va, ax_kernel, ax_res]:
=False, bottom=False, labelleft=False, labelbottom=False)
ax.tick_params(left1,0))
ax.yaxis.set_major_locator(mticker.IndexLocator(1,0))
ax.xaxis.set_major_locator(mticker.IndexLocator(="k")
ax.grid(color
###############
# Animation
###############
def init():
for row in res_texts:
for text in row:
"")
text.set_text(
def animate(ij):
=ij
i,j= kernel.shape[1]//2
o # calculate result
= (kernel*va[1+s*i-o:1+s*i+o+1, 1+s*j-o:1+s*j+o+1]).sum()
res_ij
res_texts[i][j].set_text(res_ij)# make colors
= va_color.copy()
c 1+s*i-o:1+s*i+o+1, 1+s*j-o:1+s*j+o+1] = 1.
c[
im_va.set_array(c)
= res.copy()
r = 1
r[i,j]
im_res.set_array(r)
= np.indices(res.shape)
i,j
=zip(i.flat, j.flat)
frames
animate(frames) fig.savefig(fname)
from keras import backend as K
# input image dimensions
= 28, 28
img_rows, img_cols
# the data, split between train and test sets
= mnist.load_data()
(x_train, y_train), (x_test, y_test)
if K.image_data_format() == 'channels_first':
= x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_train = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
x_test = (1, img_rows, img_cols)
input_shape else:
= x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_train = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
x_test = (img_rows, img_cols, 1) input_shape
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
= keras.Sequential()
model_vertical_edge =1, kernel_size=(3, 3), activation='linear', input_shape=(28, 28, 1))) model_vertical_edge.add(layers.Conv2D(filters
= keras.Sequential()
model_vertical_edge_relu =1, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1))) model_vertical_edge_relu.add(layers.Conv2D(filters
= model_vertical_edge.layers[0].get_weights()
T = filter_conv
filter_conv 0] = filter_conv.reshape(T[0].shape)
T[0].set_weights(T) model_vertical_edge.layers[
2:3]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_vertical_edge.predict(x_train["4-vertical-edge-linear.pdf", transparent=True) plt.savefig(
= model_vertical_edge_relu.layers[0].get_weights()
T = filter_conv
filter_conv 0] = filter_conv.reshape(T[0].shape)
T[0].set_weights(T) model_vertical_edge_relu.layers[
2:3]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_vertical_edge_relu.predict(x_train["4-vertical-edge-relu.pdf", transparent=True) plt.savefig(
2:3]/255).reshape(28, 28),cmap='Greys')
sns.heatmap((x_train["mnist-4.pdf", transparent=True) plt.savefig(
= keras.Sequential()
model_horizontal_edge_relu =1, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1))) model_horizontal_edge_relu.add(layers.Conv2D(filters
= model_horizontal_edge_relu.layers[0].get_weights()
T 0] = filter_conv.T.reshape(T[0].shape)
T[0].set_weights(T) model_horizontal_edge_relu.layers[
2:3]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_horizontal_edge_relu.predict(x_train["4-horizontal-edge-relu.pdf", transparent=True) plt.savefig(
0:1]/255).reshape(28, 28),cmap='Greys')
sns.heatmap((x_train["mnist-5.pdf", transparent=True) plt.savefig(
0:1]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_horizontal_edge_relu.predict(x_train["5-horizontal-edge-relu.pdf", transparent=True) plt.savefig(
0:1]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_vertical_edge_relu.predict(x_train["5-vertical-edge-relu.pdf", transparent=True) plt.savefig(
5:6]/255).reshape(28, 28),cmap='Greys')
sns.heatmap((x_train["mnist-2.pdf", transparent=True) plt.savefig(
5:6]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_horizontal_edge_relu.predict(x_train["2-horizontal-edge-relu.pdf", transparent=True) plt.savefig(
5:6]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_vertical_edge_relu.predict(x_train["2-vertical-edge-relu.pdf", transparent=True) plt.savefig(
6:7]/255).reshape(28, 28),cmap='Greys')
sns.heatmap((x_train["mnist-1.pdf", transparent=True) plt.savefig(
6:7]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_horizontal_edge_relu.predict(x_train["1-horizontal-edge-relu.pdf", transparent=True) plt.savefig(
6:7]/255).reshape(26, 26),cmap='Greys')
sns.heatmap(model_vertical_edge_relu.predict(x_train["1-vertical-edge-relu.pdf", transparent=True) plt.savefig(
CIFAR
from keras.datasets import cifar10
= cifar10.load_data() (x_train, y_train), (x_test, y_test)
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 91s 1us/step
= Sequential()
model 32, (3, 3), padding='same',
model.add(Conv2D(=x_train.shape[1:]))
input_shape'relu')) model.add(Activation(
= keras.Sequential()
model_horizontal_edge_relu =1, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3))) model_horizontal_edge_relu.add(layers.Conv2D(filters
0].get_weights()[0].shape model_horizontal_edge_relu.layers[
(3, 3, 3, 1)
= np.empty((3, 3, 3))
filter_3d_horizontal = filter_conv.T filter_3d_horizontal[:]
filter_3d_horizontal
array([[[ 1., 1., 1.],
[ 0., 0., 0.],
[-1., -1., -1.]],
[[ 1., 1., 1.],
[ 0., 0., 0.],
[-1., -1., -1.]],
[[ 1., 1., 1.],
[ 0., 0., 0.],
[-1., -1., -1.]]])
= model_horizontal_edge_relu.layers[0].get_weights()
T 0] = filter_3d_horizontal.reshape(T[0].shape)
T[0].set_weights(T) model_horizontal_edge_relu.layers[
4])
plt.imshow(x_train[4])
plt.title(y_train["cifar-10-car.pdf", transparent=True) plt.savefig(
4][:, :, 0], cmap='Reds')
plt.imshow(x_train["cifar-10-car-red.pdf", transparent=True) plt.savefig(
4][:, :, 1], cmap='Greens')
plt.imshow(x_train["cifar-10-car-green.pdf", transparent=True) plt.savefig(
4][:, :, 2], cmap='Blues')
plt.imshow(x_train["cifar-10-car-blue.pdf", transparent=True) plt.savefig(
6:7]).reshape(30, 30),cmap='Greys') sns.heatmap(model_horizontal_edge_relu.predict(x_train[
1:] x_train.shape[
(32, 32, 3)
4:5]).shape model_horizontal_edge_relu.predict(x_train[
(1, 30, 30, 1)
= keras.Sequential()
model_vertical_edge_relu =1, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3))) model_vertical_edge_relu.add(layers.Conv2D(filters
= np.empty((3, 3, 3))
filter_3d_vertical = filter_conv
filter_3d_vertical[:] = filter_3d_vertical
filter_3d_vertical
= model_vertical_edge_relu.layers[0].get_weights()
T 0] = filter_3d_vertical.reshape(T[0].shape)
T[0].set_weights(T) model_vertical_edge_relu.layers[
+1)/2) plt.imshow((filter_3d_horizontal
+1)/2).T) plt.imshow(((filter_3d_vertical
filter_3d_vertical
array([[[ 1., 0., -1.],
[ 1., 0., -1.],
[ 1., 0., -1.]],
[[ 1., 0., -1.],
[ 1., 0., -1.],
[ 1., 0., -1.]],
[[ 1., 0., -1.],
[ 1., 0., -1.],
[ 1., 0., -1.]]])
+1)/2 (filter_3d_vertical
array([[[1. , 0.5, 0. ],
[1. , 0.5, 0. ],
[1. , 0.5, 0. ]],
[[1. , 0.5, 0. ],
[1. , 0.5, 0. ],
[1. , 0.5, 0. ]],
[[1. , 0.5, 0. ],
[1. , 0.5, 0. ],
[1. , 0.5, 0. ]]])
6:7]).reshape(30, 30),cmap='Greys') sns.heatmap(model_vertical_edge_relu.predict(x_train[
0].get_weights()[0][0].shape model_vertical_edge_relu.layers[
(3, 3, 1)
import scipy
= x_train[6:7].reshape(32, 32, 3)
img from skimage import color
= color.rgb2gray(img)
img = np.array([[0,-1,0],[-1,5,-1],[0,-1,0]])
sharpen_kernel = scipy.signal.convolve2d(img, sharpen_kernel, 'valid') image_sharpen
from skimage import io
= io.imread("../neural-networks/assets/cnn/beach.jpg") beach
beach.shape
(1704, 2272, 3)
= io.imread("../neural-networks/assets/cnn/buildings.jpg") buildings
buildings.shape
(1704, 2272, 3)
plt.imshow(beach)'OFF') plt.axis(
0], cmap='Reds')
plt.imshow(beach[:, :, 'off') plt.axis(
1], cmap='Greens') plt.imshow(beach[:, :,
2], cmap='Blues') plt.imshow(beach[:, :,
= np.array([[1,0,-1],[1,0,-1],[1,0,-1]])
vertical_kernel = scipy.signal.convolve2d(beach[:, :, 0], vertical_kernel, 'valid')
image_out_beach_red ='Greys') plt.imshow(image_out_beach_red, cmap
= np.array([[1,0,-1],[1,0,-1],[1,0,-1]])
vertical_kernel = scipy.signal.convolve2d(beach[:, :, 1], vertical_kernel, 'valid')
image_out_beach_green ='Greens')
plt.imshow(image_out_beach_green, cmap'off') plt.axis(
= np.array([[1,0,-1],[1,0,-1],[1,0,-1]])
vertical_kernel = scipy.signal.convolve2d(beach[:, :, 2], vertical_kernel, 'valid')
image_out_beach_blue ='Blues')
plt.imshow(image_out_beach_blue, cmap'off') plt.axis(
= scipy.signal.convolve2d(buildings[:, :, 2], vertical_kernel, 'valid')
image_out_buildings_blue ='Greys')
plt.imshow(image_out_buildings_blue, cmap'off') plt.axis(
= vertical_kernel.T
horizontal_kernel horizontal_kernel
array([[ 1, 1, 1],
[ 0, 0, 0],
[-1, -1, -1]])
= scipy.signal.convolve2d(buildings[:, :, 2], horizontal_kernel, 'valid')
image_out_buildings_blue_horizontal ='Greys')
plt.imshow(image_out_buildings_blue_horizontal, cmap'off') plt.axis(