Maxpool layer
- class layers.maxpool_layer.Maxpool_layer(size, stride=None, pad=False, input_shape=None, **kwargs)[source]
Bases:
NumPyNet.layers.base.BaseLayer
Maxpool layer
- sizetuple or int
Size of the kernel to slide over the input image. If a tuple, it must contains two integers, (kx, ky). If a int, size = kx = ky.
- stridetuple or int (default = None)
Represents the horizontal and vertical stride of the kernel (sx, sy). If None or 0, stride is assigned the same values as size.
- input_shapetuple (default = None)
Input shape of the layer. The default value is used when the layer is part of a network.
- padbool, (default = False)
If False the image is cut to fit the size and stride dimensions, if True the image is padded following keras SAME padding, see references for details.
>>> import os >>> >>> import pylab as plt >>> from PIL import Image >>> >>> img_2_float = lambda im : ((im - im.min()) * (1. / (im.max() - im.min()) * 1.)).astype(float) >>> float_2_img = lambda im : ((im - im.min()) * (1. / (im.max() - im.min()) * 255.)).astype(np.uint8) >>> >>> filename = os.path.join(os.path.dirname('__file__'), '..', '..', 'data', 'dog.jpg') >>> inpt = np.asarray(Image.open(filename), dtype=float) >>> inpt.setflags(write=1) >>> inpt = img_2_float(inpt) >>> >>> inpt = np.expand_dims(inpt, axis=0) # Add the batch shape. >>> b, w, h, c = inpt.shape >>> >>> size = (3, 3) >>> stride = (2, 2) >>> pad = False >>> >>> layer = Maxpool_layer(input_shape=inpt.shape, size=size, stride=stride, padding=pad) >>> >>> # FORWARD >>> >>> layer.forward(inpt) >>> >>> forward_out = layer.output >>> >>> print(layer) >>> >>> # BACKWARD >>> >>> delta = np.zeros(inpt.shape, dtype=float) >>> layer.delta = np.ones(layer.out_shape, dtype=float) >>> layer.backward(delta) >>> >>> # Visualizations >>> >>> fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(10, 5)) >>> fig.subplots_adjust(left=0.1, right=0.95, top=0.95, bottom=0.15) >>> fig.suptitle('MaxPool Layer
- size{}, stride{}, padding{} ‘.format(size, stride, pad))
>>> >>> ax1.imshow(float_2_img(inpt[0])) >>> ax1.set_title('Original Image') >>> ax1.axis('off') >>> >>> ax2.imshow(float_2_img(forward_out[0])) >>> ax2.set_title('Forward') >>> ax2.axis('off') >>> >>> ax3.imshow(float_2_img(delta[0])) >>> ax3.set_title('Backward') >>> ax3.axis('off') >>> >>> fig.tight_layout() >>> plt.show()
- backward(delta)[source]
Backward function of maxpool layer: it access avery position where in the input image there’s a chosen maximum and add the correspondent self.delta value. Since we work with a ‘view’ of delta, the same pixel may appear more than one time, and an atomic acces to it’s value is needed to correctly modifiy it.
- Parameters
delta (array-like) – Global delta to be backpropagated with shape (batch, out_w, out_h, out_c).
- Return type
self
- forward(inpt)[source]
Forward function of the maxpool layer: It slides a kernel over every input image and return the maximum value of every sub-window. the function _asStride returns a view of the input arrary with shape (batch, out_w, out_h , c, kx, ky), where, for every image in the batch we have: out_w * out_h * c sub matrixes kx * ky, containing pixel values.
- Parameters
inpt (array-like) – Input batch of images, with shape (batch, input_w, input_h, input_c).
- Return type
self
- property out_shape
Get the output shape
- Returns
out_shape – Tuple as (batch, out_w, out_h, out_c)
- Return type
tuple