Avgpool layer

class layers.avgpool_layer.Avgpool_layer(size, stride=None, pad=False, input_shape=None, **kwargs)[source]

Bases: NumPyNet.layers.base.BaseLayer

Avgpool 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)
>>> pad  = False
>>>
>>> size   = 3
>>> stride = 2
>>>
>>> # Model initialization
>>> layer = Avgpool_layer(input_shape=inpt.shape, size=size, stride=stride, padding=pad)
>>>
>>> # FORWARD
>>>
>>> layer.forward(inpt)
>>> forward_out = layer.output.copy()
>>>
>>> print(layer)
>>>
>>> # BACKWARD
>>>
>>> delta = np.random.uniform(low=0., high=1.,size=inpt.shape)
>>> 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('Average Pool 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(layer.output[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()
../../_images/average_3-2.png ../../_images/average_30-20.png
backward(delta)[source]
<<<<<<< HEAD

Backward function of the average_pool layer: the function modifies the net delta

Backward function of the averagepool layer: the function modifies the net delta

>>>>>>> bc4153f58a054d4e60bb14c993bf61a058458e8b

to be backpropagated.

deltaarray-like

Global delta to be backpropagated with shape (batch, out_w, out_h, out_c).

self

forward(inpt)[source]

Forward function of the average pool layer: it slide a kernel of size (kx,ky) = size and with step (st1, st2) = strides over every image in the batch. For every sub-matrix it computes the average value without considering NAN value (padding), and passes it to the output.

inpt : array-like

<<<<<<< HEAD

Input batch of image, with the shape (batch, input_w, input_h, input_c).

Input batch of images, with shape (batch, input_w, input_h, input_c).

>>>>>>> bc4153f58a054d4e60bb14c993bf61a058458e8b

self

property out_shape

Get the output shape

Returns

out_shape – Output shape as (batch, out_width, out_height, out_channels)

Return type

tuple